DeptMapper.xml 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.lc.mapper.DeptMapper">
  6. <!-- deptByIdCollection -->
  7. <resultMap id="deptByIdCollection" type="com.lc.pojo.Dept">
  8. <id property="id" column="did"></id>
  9. <result property="name" column="name" ></result>
  10. <result property="comment" column="comment" ></result>
  11. <!-- 集合 -->
  12. <collection property="userList" ofType="com.lc.pojo.User" >
  13. <id property="id" column="id" ></id>
  14. <result property="username" column="username" ></result>
  15. <result property="password" column="password" ></result>
  16. <result property="address" column="address" ></result>
  17. <result property="gender" column="gender" ></result>
  18. <result property="deptId" column="dept_id" ></result>
  19. </collection>
  20. </resultMap>
  21. <select id="getDeptByIdCollection" resultMap="deptByIdCollection">
  22. SELECT
  23. u.*, d.id did , d.`name` name , d.`comment` comment
  24. FROM
  25. t_user u INNER JOIN t_dept d ON u.dept_id = d.id
  26. WHERE
  27. d.id = #{id}
  28. </select>
  29. <select id="getDeptByIdStep" resultType="dept">
  30. select * from t_dept where id = #{id}
  31. </select>
  32. <!--
  33. 一对多 分布查询
  34. -->
  35. <resultMap id="deptByIdStep" type="com.lc.pojo.Dept">
  36. <id property="id" column="id"></id>
  37. <result property="name" column="name" ></result>
  38. <result property="comment" column="comment" ></result>
  39. <!--
  40. select="指向mapper.xml文件中的id"
  41. column
  42. fetchType="lazy" 设置懒加载
  43. -->
  44. <collection
  45. property="userList"
  46. ofType="com.lc.pojo.User"
  47. fetchType="lazy"
  48. select="com.lc.mapper.UserMapper.getUserByIdStepCollection"
  49. column="id"
  50. >
  51. </collection>
  52. </resultMap>
  53. <select id="getDeptByIdStepCollection" resultMap="deptByIdStep">
  54. select * from t_dept where id = #{id}
  55. </select>
  56. </mapper>