码迷,mamicode.com
首页 > 其他好文 > 详细

转自张胜鸿的程序人生(QQ9017197)MyBatis一对多demo!!!

时间:2014-09-26 13:37:09      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:http   io   os   java   ar   for   数据   sp   cti   

stringintegerclassencodinginterfacejoin 在学习MyBatis3的过程中,文档上面一直在强调一个id的东西!在做这个实验的时候,也因为没有理解清楚id含义而导致一对多的“多”中也只有一条数据。id和result的唯一不同是id表示的结果将是当比较对象实例时用到的标识属性。这帮助来改进整体表现,特别是缓存和嵌入结果映射。所以不同数据的id应该唯一区别,不然倒置数据结果集只有一条数据。 TeacherMapper.xml [xhtml] view plaincopyprint? 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 17. Teacher [java] view plaincopyprint? 01.package com.nantian.mybatis.one2one; 02. 03.import java.util.List; 04. 05.import com.nantian.mybatis.one2many.Student; 06. 07.public class Teacher { 08. private int id; 09. private String name; 10. private List students; 11. 12. public int getId() { 13. return id; 14. } 15. 16. public void setId(int id) { 17. this.id = id; 18. } 19. 20. public String getName() { 21. return name; 22. } 23. 24. public void setName(String name) { 25. this.name = name; 26. } 27. 28. public List getStudents() { 29. return students; 30. } 31. 32. public void setStudents(List students) { 33. this.students = students; 34. } 35.} TeacherMapper [java] view plaincopyprint? 01.package com.nantian.mybatis.one2many; 02. 03.import com.nantian.mybatis.one2one.Teacher; 04. 05.public interface TeacherMapper { 06. public Teacher one2many(int id); 07.} Student [java] view plaincopyprint? 01.package com.nantian.mybatis.one2many; 02. 03.public class Student { 04. private int id; 05. private String name; 06. 07. public int getId() { 08. return id; 09. } 10. 11. public void setId(int id) { 12. this.id = id; 13. } 14. 15. public String getName() { 16. return name; 17. } 18. 19. public void setName(String name) { 20. this.name = name; 21. } 22.} T [java] view plaincopyprint? 01.package com.nantian.mybatis.one2many; 02. 03.import java.util.List; 04. 05.import org.apache.ibatis.session.SqlSession; 06. 07.import com.nantian.mybatis.data.DBUtil; 08.import com.nantian.mybatis.one2one.Teacher; 09. 10.public class T { 11. 12. public static void main(String[] args) { 13. SqlSession sqlSession = DBUtil.getInstance().getSqlSessionFactory() 14. .openSession(); 15. try { 16. TeacherMapper teacherMapper = sqlSession 17. .getMapper(TeacherMapper.class); 18. Teacher teacher = teacherMapper.one2many(100); 19. List students = teacher.getStudents(); 20. System.out.println(teacher.getId()); 21. System.out.println(teacher.getName()); 22. for (Student student : students) { 23. System.out.println(student.getId()); 24. System.out.println(student.getName()); 25. } 26. } finally { 27. sqlSession.close(); 28. } 29. } 30. 31.}

转自张胜鸿的程序人生(QQ9017197)MyBatis一对多demo!!!

标签:http   io   os   java   ar   for   数据   sp   cti   

原文地址:http://www.cnblogs.com/12huiliu/p/3994595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!