DepartmentMapper.xml 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.sf.mapper.DepartmentMapper">
  6. <!-- 二级缓存-->
  7. <!-- <cache-->
  8. <!-- eviction="FIFO"-->
  9. <!-- flushInterval="60000"-->
  10. <!-- size="512"-->
  11. <!-- readOnly="true"/>-->
  12. <!-- mybatis - 集成的第三方缓存ehcache-->
  13. <cache type="org.mybatis.caches.ehcache.EhcacheCache">
  14. <!-- 缓存创建以后,最后一次访问缓存,间隔多久缓存失效(单位s) -->
  15. <property name="timeToIdleSeconds" value="3600"/>
  16. <!-- 缓存创建日期起到失效时的时间间隔(单位s) -->
  17. <property name="timeToLiveSeconds" value="3600"/>
  18. <!-- 堆内存中缓存对象数(0:没有限制) -->
  19. <property name="maxEntriesLocalHeap" value="1000"/>
  20. <!-- 磁盘中的对象数,默认为0不限制 -->
  21. <property name="maxEntriesLocalDisk" value="1000000"/>
  22. <!-- 内存存储与释放策略(FIFO:先进先出,LFU:一直以来最少被使用的,LRU:最近最少使用的) -->
  23. <property name="memoryStoreEvictionPolicy" value="LRU"/>
  24. </cache>
  25. <select id="getDepartmentInfo" parameterType="integer" resultType="department">
  26. select * from department where dept_id = #{deptId}
  27. </select>
  28. <!-- getDepartmentInfoWithEmploye方法中的 map1 对应我们<resultMap>标签中的 id属性 type 映射结果的全类名
  29. column:对应我们数据库中的字段 property:对应我们java对象中的属性名
  30. <id>:映射字段中的id属性
  31. <result>:映射字段中的非id属性
  32. collection:一对多映射标签
  33. ofType="employee" :一对多的属性名的类型的全类名
  34. -->
  35. <resultMap id="map1" type="com.sf.entity.Department">
  36. <id column="dept_id" property="deptId"></id>
  37. <result column="dept_name" property="deptName"></result>
  38. <collection property="employees" ofType="employee">
  39. <id column="emp_id" property="empId"></id>
  40. <result column="emp_name" property="empName"></result>
  41. <result column="salary" property="salary"></result>
  42. </collection>
  43. </resultMap>
  44. <select id="getDepartmentInfoWithEmployee" parameterType="integer" resultMap="map1">
  45. select * from department t1 , employee t2 where t1.dept_id = t2.dept_id and t1.dept_id = #{deptId}
  46. </select>
  47. <!--
  48. <collection>标签中的 column="dept_id" : 我们关联表的外键
  49. select : 是查询员工信息的接口
  50. -->
  51. <resultMap id="deptMap2" type="department">
  52. <id column="dept_id" property="deptId"></id>
  53. <result column="dept_name" property="deptName"></result>
  54. <collection property="employees" ofType="employee" column="dept_id" select="com.sf.mapper.DepartmentMapper.getEmployeeInfoByDeptId">
  55. <id column="emp_id" property="empId"></id>
  56. <result column="emp_name" property="empName"></result>
  57. <result column="salary" property="salary"></result>
  58. </collection>
  59. </resultMap>
  60. <!-- 分为俩步骤:
  61. 1.根据dept_id查询部门信息
  62. 2.根据的dept_id查询员工的信息
  63. -->
  64. <select id="getDepartmentInfoWithEmployee2" parameterType="integer" resultMap="deptMap2">
  65. select * from department where dept_id = #{deptId}
  66. </select>
  67. <select id="getEmployeeInfoByDeptId" parameterType="integer" resultType="employee">
  68. select * from employee where dept_id = #{deptId}
  69. </select>
  70. </mapper>