(知识篇)Mybatis02使用(嵌套结果/嵌套查询)

(知识篇)Mybatis02使用(嵌套结果/嵌套查询)

2016年12月25日 18:20:44

阅读数:3289

/**

* MyBatis的嵌套结果/嵌套查询(网上有说这是叫延迟加载)

* 1、一对一:

* 1.1 一对一嵌套结果:实际就是将两个表join起来(参考userMapper.xml的selectUserById方法)

* 得出多行数据,其中,需要用resultMap返回,

* 配置resultMap

*

* type:返回的类型的全类名/配置别名 

* autoMapping:自动对应字段,如果不写,则需要手动映射起来

* 方法是加入/

* 1.2 通过 标签,配置返回的一对一的bean(入User中对应一个Teacher),association中则需要配置Teacher的class

* 例如:

* 1.3一对一嵌套查询:实际是做两次查询(参考userMapper.xml的selectSingleUserById)

* 和嵌套结果类似:只不过配置association时,需要传入外键,和填写select的语句的id(参考selectSingleUser方法)

* 例如:

* 其中需要提及一下的是column 为表的外键的列名

* 2、一对多

* 2.1 一对多嵌套结果:跟一对一嵌套结果类似,区别在于(参考teacherMapper.xml的getTeacherByJoin方法):

* 一对一嵌套结果使用,一对多中使用标签

* 一对一中使用javaType指定类型,一堆多中使用ofType指定类型;

* resultMap中必须指定id 的标签,否则selectone中返回结果会报错

* 2.2一对多嵌套查询:同样跟一对一嵌套结果类似

* 注意一下collection标签、ofType、指定id标签对应的column

* @param args

*/

 

 

测试类:

 

 
  1. package com.mybatis.test;

  2.  
  3. import java.io.InputStream;

  4.  
  5. import org.apache.ibatis.session.SqlSession;

  6. import org.apache.ibatis.session.SqlSessionFactory;

  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;

  8.  
  9. import com.mybatis.vo.Teacher;

  10. import com.mybatis.vo.User;

  11.  
  12. public class Test {

  13.  
  14. /**

  15. * MyBatis的嵌套结果/嵌套查询(网上有说这是叫延迟加载)

  16. *

  17. * 1、一对一:

  18. * 1.1 一对一嵌套结果:实际就是将两个表join起来(参考userMapper.xml的selectUserById方法)

  19. * 得出多行数据,其中,需要用resultMap返回,

  20. * 配置resultMap

  21. *

  22. * type:返回的类型的全类名/配置别名

  23. * autoMapping:自动对应字段,如果不写,则需要手动映射起来

  24. * 方法是加入/

  25. *

  26. * 1.2 通过 标签,配置返回的一对一的bean(入User中对应一个Teacher),association中则需要配置Teacher的class

  27. * 例如:

  28. *

  29. *

  30. * 1.3一对一嵌套查询:实际是做两次查询(参考userMapper.xml的selectSingleUserById)

  31. * 和嵌套结果类似:只不过配置association时,需要传入外键,和填写select的语句的id(参考selectSingleUser方法)

  32. * 例如:

  33. * 其中需要提及一下的是column 为表的外键的列名

  34. *

  35. *

  36. * 2、一对多

  37. * 2.1 一对多嵌套结果:跟一对一嵌套结果类似,区别在于(参考teacherMapper.xml的getTeacherByJoin方法):

  38. * 一对一嵌套结果使用,一对多中使用标签

  39. * 一对一中使用javaType指定类型,一堆多中使用ofType指定类型;

  40. * resultMap中必须指定id 的标签,否则selectone中返回结果会报错

  41. * 2.2一对多嵌套查询:同样跟一对一嵌套结果类似

  42. * 注意一下collection标签、ofType、指定id标签对应的column

  43. *

  44. *

  45. * @param args

  46. */

  47.  
  48.  
  49. public static void main(String[] args) {

  50.  
  51.  
  52. InputStream is = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");

  53.  
  54. System.out.println(is);

  55.  
  56. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);

  57.  
  58. System.out.println(sessionFactory);

  59.  
  60. SqlSession session = sessionFactory.openSession();

  61.  
  62. System.out.println(session);

  63.  
  64.  
  65. String userNameSpace = "com.mybatis.vo.User";

  66. /*一对一嵌套结果*/

  67. String selectOneByJoin = userNameSpace+".selectUserById";

  68. User user = session.selectOne(selectOneByJoin,3);

  69. System.out.println(user);

  70.  
  71.  
  72. /*一对一嵌套查询*/

  73. String selectOneBySelect = userNameSpace+".selectSingleUserById";

  74. User user2 = session.selectOne(selectOneBySelect,1);

  75. System.out.println(user2);

  76.  
  77.  
  78.  
  79. String teacherNameSpace = "com.mybatis.vo.Teacher";

  80. /*一对多嵌套结果*/

  81. String getTeacherByJoin = teacherNameSpace+".getTeacherByJoin";

  82. Teacher teacher = session.selectOne(getTeacherByJoin,1);

  83. System.out.println(teacher);

  84.  
  85.  
  86. /*一对多嵌套查询*/

  87. String getTeacherById = teacherNameSpace+".getTeacherById";

  88. Teacher teacher2 = session.selectOne(getTeacherById,2);

  89. System.out.println(teacher2);

  90.  
  91. }

  92. }


User类:

 

 

 
  1. package com.mybatis.vo;

  2.  
  3. public class User {

  4. private int id;

  5. private String userName;

  6. private int sex;

  7. private Teacher teacher;

  8.  
  9. public User() {

  10. super();

  11. // TODO Auto-generated constructor stub

  12. }

  13.  
  14. public User(int id, String userName, int sex, Teacher teacher) {

  15. super();

  16. this.id = id;

  17. this.userName = userName;

  18. this.sex = sex;

  19. this.teacher = teacher;

  20. }

  21.  
  22. public int getId() {

  23. return id;

  24. }

  25.  
  26. public void setId(int id) {

  27. this.id = id;

  28. }

  29.  
  30. public String getUserName() {

  31. return userName;

  32. }

  33.  
  34. public void setUserName(String userName) {

  35. this.userName = userName;

  36. }

  37.  
  38. public int getSex() {

  39. return sex;

  40. }

  41.  
  42. public void setSex(int sex) {

  43. this.sex = sex;

  44. }

  45.  
  46. public Teacher getTeacher() {

  47. return teacher;

  48. }

  49.  
  50. public void setTeacher(Teacher teacher) {

  51. this.teacher = teacher;

  52. }

  53.  
  54. @Override

  55. public String toString() {

  56. return "User [id=" + id + ", userName=" + userName + ", sex=" + sex + ", teacher=" + teacher + "]";

  57. }

  58.  
  59. }


userMapper.xml

 

 

 
  1. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

  2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  3.  
  4.  
  5.  
  6.  
  7.  
  8.  


Teacher类:

 

 

 
  1. package com.mybatis.vo;

  2.  
  3. import java.util.List;

  4.  
  5. public class Teacher {

  6. private int id;

  7. private String teacherName;

  8. private List users;

  9.  
  10. public Teacher() {

  11. super();

  12. // TODO Auto-generated constructor stub

  13. }

  14.  
  15. public Teacher(int id, String teacherName, List users) {

  16. super();

  17. this.id = id;

  18. this.teacherName = teacherName;

  19. this.users = users;

  20. }

  21.  
  22. public int getId() {

  23. return id;

  24. }

  25.  
  26. public void setId(int id) {

  27. this.id = id;

  28. }

  29.  
  30. public String getTeacherName() {

  31. return teacherName;

  32. }

  33.  
  34. public void setTeacherName(String teacherName) {

  35. this.teacherName = teacherName;

  36. }

  37.  
  38. public List getUsers() {

  39. return users;

  40. }

  41.  
  42. public void setUsers(List users) {

  43. this.users = users;

  44. }

  45.  
  46. @Override

  47. public String toString() {

  48. return "Teacher [id=" + id + ", teacherName=" + teacherName + ", users=" + users + "]";

  49. }

  50.  
  51. }


teacherMapper.xml

 

 

 
  1. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

  2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  3.  
  4.  
  5.  
  6.  
  7.  


mybatis-config.xml

 

 

 

你可能感兴趣的:(mybatis)