我是靠谱客的博主 纯情皮卡丘,最近开发中收集的这篇文章主要介绍(知识篇)Mybatis02使用(嵌套结果/嵌套查询)(知识篇)Mybatis02使用(嵌套结果/嵌套查询),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

2016年12月25日 18:20:44

阅读数:3289

/**

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

* 1、一对一:

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

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

* 配置resultMap

* <resultMap type="User" id="selectUserMap" autoMapping="true"></resultMap>

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

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

* 方法是加入<id column="xxx" property="xxx" />/<result column="xxx" property="xxx"/>

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

* 例如:<association property="teacher" javaType="Teacher" autoMapping="true"><id column="teacherId" property="id"/><association/>

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

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

* 例如:<association javaType="Teacher" property="teacher" column="teacherId" select="selectSingleUser"  autoMapping="true"></association>

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

* 2、一对多

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

* 一对一嵌套结果使用<association>,一对多中使用<collection>标签

* 一对一中使用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. * <resultMap type="User" id="selectUserMap" autoMapping="true"></resultMap>

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

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

  24. * 方法是加入<id column="xxx" property="xxx" />/<result column="xxx" property="xxx"/>

  25. *

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

  27. * 例如:<association property="teacher" javaType="Teacher" autoMapping="true"><id column="teacherId" property="id"/><association/>

  28. *

  29. *

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

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

  32. * 例如:<association javaType="Teacher" property="teacher" column="teacherId" select="selectSingleUser" autoMapping="true"></association>

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

  34. *

  35. *

  36. * 2、一对多

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

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

  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. <?xml version="1.0" encoding="UTF-8" ?>

  2. <!DOCTYPE mapper

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

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

  5. <mapper namespace="com.mybatis.vo.User">

  6.  
  7. <!-- 一对一嵌套结果 -->

  8. <select id="selectUserById" parameterType="int" resultMap="selectUserMap">

  9. SELECT user.id,userName,sex,teacher.id AS teacherId,teacherName FROM USER

  10. LEFT OUTER JOIN Teacher ON Teacher.id = user.teacherId

  11. where user.id = #{id}

  12. </select>

  13. <resultMap type="User" id="selectUserMap" autoMapping="true">

  14. <!-- <id property="id" column="id"/>

  15. <result property="userName" column="userName"/>

  16. <result property="sex" column="sex"/> -->

  17. <association property="teacher" javaType="Teacher" autoMapping="true">

  18. <id column="teacherId" property="id"/>

  19. <!-- <result column="teacherName" property="teacherName"/> -->

  20. </association>

  21. </resultMap>

  22.  
  23.  
  24.  
  25. <!-- 一对一嵌套查询 -->

  26. <select id="selectSingleUserById" parameterType="int" resultMap="selectSingleUserMap" >

  27. select * from User where id = #{id}

  28. </select>

  29. <select id="selectSingleUser" parameterType="int" resultType="Teacher">

  30. select * from Teacher where id = #{id}

  31. </select>

  32. <resultMap type="User" id="selectSingleUserMap" autoMapping="true">

  33. <association javaType="Teacher" property="teacher" column="teacherId" select="selectSingleUser" autoMapping="true"></association>

  34. </resultMap>

  35.  
  36.  
  37. </mapper>


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<User> users;

  9.  
  10. public Teacher() {

  11. super();

  12. // TODO Auto-generated constructor stub

  13. }

  14.  
  15. public Teacher(int id, String teacherName, List<User> 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<User> getUsers() {

  39. return users;

  40. }

  41.  
  42. public void setUsers(List<User> 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. <?xml version="1.0" encoding="UTF-8" ?>

  2. <!DOCTYPE mapper

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

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

  5. <mapper namespace="com.mybatis.vo.Teacher">

  6.  
  7. <!-- 嵌套结果 -->

  8. <select id="getTeacherByJoin" parameterType="int" resultMap="getTeacherByJoinMap">

  9. SELECT teacher.*,user.id AS userId,user.UserName,user.Sex FROM Teacher

  10. LEFT OUTER JOIN USER ON user.TeacherID = teacher.id where teacher.id = #{id}

  11. </select>

  12. <resultMap type="Teacher" id="getTeacherByJoinMap" autoMapping="true">

  13. <id column="id" property="id"/>

  14. <collection property="users" ofType="User" autoMapping="true">

  15. <id property="id" column="userId"/>

  16. </collection>

  17. </resultMap>

  18.  
  19.  
  20.  
  21. <!-- 一对多嵌套查询 -->

  22. <select id="getTeacherById" parameterType="int" resultMap="getTeacherByIdMap">

  23. select * from Teacher where id = #{id}

  24. </select>

  25. <select id="getUserByTeacherId" parameterType="int" resultType="User">

  26. select * from User where teacherId = #{teacherId}

  27. </select>

  28. <resultMap type="Teacher" id="getTeacherByIdMap" autoMapping="true">

  29. <id column="id" property="id"/>

  30. <collection property="users" column="id" select="getUserByTeacherId" autoMapping="true"></collection>

  31. </resultMap>

  32.  
  33. </mapper>


mybatis-config.xml

 

 

 
  1. <?xml version="1.0" encoding="UTF-8" ?>

  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

  3. <configuration>

  4. <typeAliases>

  5. <typeAlias type="com.mybatis.vo.User" alias="User" />

  6. <typeAlias type="com.mybatis.vo.Teacher" alias="Teacher" />

  7. </typeAliases>

  8. <environments default="development">

  9. <environment id="development">

  10. <transactionManager type="JDBC" />

  11. <dataSource type="POOLED">

  12. <property name="driver" value="com.mysql.jdbc.Driver" />

  13. <property name="url" value="jdbc:mysql://192.168.1.92:3306/test" />

  14. <property name="username" value="root" />

  15. <property name="password" value="root" />

  16. </dataSource>

  17. </environment>

  18. </environments>

  19. <mappers>

  20. <!-- <mapper resource="org/mybatis/example/BlogMapper.xml" /> -->

  21. <mapper resource="com/mybatis/vo/userMapper.xml" />

  22. <mapper resource="com/mybatis/vo/teacherMapper.xml" />

  23. </mappers>

  24. </configuration>

最后

以上就是纯情皮卡丘为你收集整理的(知识篇)Mybatis02使用(嵌套结果/嵌套查询)(知识篇)Mybatis02使用(嵌套结果/嵌套查询)的全部内容,希望文章能够帮你解决(知识篇)Mybatis02使用(嵌套结果/嵌套查询)(知识篇)Mybatis02使用(嵌套结果/嵌套查询)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部