1、select返回List

mybatis会把结果集自动的封装成List

  1. <!-- public List<Employee> getEmpsByLastNameLike(String lastName); -->
  2. <!--resultType:如果返回的是一个集合,要写集合中元素的类型 -->
  3. <select id="getEmpsByLastNameLike" resultType="com.bw.mybatis.bean.Employee">
  4. select * from tbl_employee where last_name like #{lastName}
  5. </select>

2、select返回Map

多条记录封装一个map:Map:键是这条记录的主键,值是记录封装后的javaBean

  1. //@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key
  2. @MapKey("id")
  3. public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);
  4. <select id="getEmpByLastNameLikeReturnMap" resultType="com.bw.mybatis.bean.Employee">
  5. select * from tbl_employee where last_name like #{lastName}
  6. </select>
  7. //返回一条记录的map;key就是列名,值就是对应的值
  8. public Map<String, Object> getEmpByIdReturnMap(Integer id);
  9. <select id="getEmpByIdReturnMap" resultType="map">
  10. select * from tbl_employee where id=#{id}
  11. </select>

3、嵌套对象查询

  1. public class Employee {
  2. private Integer id;
  3. private String lastName;
  4. private String email;
  5. private String gender;
  6. private Department dept;
  7. }
  8. public class Department {
  9. private Integer id;
  10. private String departmentName;
  11. }

查询查询Employee的同时查询员工对应的部门

1、级联属性封装结果集

  1. <!--
  2. 联合查询:级联属性封装结果集
  3. -->
  4. <resultMap type="com.bw.mybatis.bean.Employee" id="MyDifEmp">
  5. <id column="id" property="id"/>
  6. <result column="last_name" property="lastName"/>
  7. <result column="gender" property="gender"/>
  8. <result column="did" property="dept.id"/>
  9. <result column="dept_name" property="dept.departmentName"/>
  10. </resultMap>
  11. <!-- public Employee getEmpAndDept(Integer id);-->
  12. <select id="getEmpAndDept" resultMap="MyDifEmp">
  13. SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
  14. d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
  15. WHERE e.d_id=d.id AND e.id=#{id}
  16. </select>

2、association指定联合的javaBean对象

  1. <resultMap type="com.bw.mybatis.bean.Employee" id="MyDifEmp2">
  2. <id column="id" property="id"/>
  3. <result column="last_name" property="lastName"/>
  4. <result column="gender" property="gender"/>
  5. <!-- association可以指定联合的javaBean对象
  6. property="dept":指定哪个属性是联合的对象
  7. javaType:指定这个属性对象的类型[不能省略]
  8. -->
  9. <association property="dept" javaType="com.bw.mybatis.bean.Department">
  10. <id column="did" property="id"/>
  11. <result column="dept_name" property="departmentName"/>
  12. </association>
  13. </resultMap>

3、association分步查询

  1. <resultMap type="com.bw.mybatis.bean.Employee" id="MyEmpByStep">
  2. <id column="id" property="id"/>
  3. <result column="last_name" property="lastName"/>
  4. <result column="email" property="email"/>
  5. <result column="gender" property="gender"/>
  6. <!-- association定义关联对象的封装规则
  7. select:表明当前属性是调用select指定的方法查出的结果
  8. column:指定将哪一列的值传给这个方法
  9. 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
  10. -->
  11. <association property="dept"
  12. select="com.bw.mybatis.dao.DepartmentMapper.getDeptById"
  13. column="d_id">
  14. </association>
  15. </resultMap>
  16. <!-- public Employee getEmpByIdStep(Integer id);-->
  17. <select id="getEmpByIdStep" resultMap="MyEmpByStep">
  18. select * from tbl_employee where id=#{id}
  19. </select>
  20. <!--public Department getDeptById(Integer id); -->
  21. <select id="getDeptById" resultType="com.bw.mybatis.bean.Department">
  22. select id,dept_name departmentName from tbl_dept where id=#{id}
  23. </select>

可以设置以下两个属性实现延迟加载

在加载employee的时候先不查询department的信息,department会在第一次使用的时候查询出来。

  1. <setting name="lazyLoadingEnabled" value="true"/>
  2. <setting name="aggressiveLazyLoading" value="false"/>

4、Collection定义关联集合查询

  1. public class Department {
  2. private Integer id;
  3. private String departmentName;
  4. private List<Employee> emps;
  5. }

查询department的时候把employee也查询出来

  1. <resultMap type="com.bw.mybatis.bean.Department" id="MyDept">
  2. <id column="did" property="id"/>
  3. <result column="dept_name" property="departmentName"/>
  4. <!--
  5. collection定义关联集合类型的属性的封装规则
  6. ofType:指定集合里面元素的类型
  7. -->
  8. <collection property="emps" ofType="com.bw.mybatis.bean.Employee">
  9. <!-- 定义这个集合中元素的封装规则 -->
  10. <id column="eid" property="id"/>
  11. <result column="last_name" property="lastName"/>
  12. <result column="email" property="email"/>
  13. <result column="gender" property="gender"/>
  14. </collection>
  15. </resultMap>
  16. <!-- public Department getDeptByIdPlus(Integer id); -->
  17. <select id="getDeptByIdPlus" resultMap="MyDept">
  18. SELECT d.id did,d.dept_name dept_name,
  19. e.id eid,e.last_name last_name,e.email email,e.gender gender
  20. FROM tbl_dept d
  21. LEFT JOIN tbl_employee e
  22. ON d.id=e.d_id
  23. WHERE d.id=#{id}
  24. </select>

5、Collection分步查询

  1. <!-- fetchType="lazy":表示使用延迟加载;
  2. - lazy:延迟
  3. - eager:立即
  4. 如果需要传递多个值,可以把多个值封装成一个map,column="{key1=column1,key2=column2}"
  5. 下面的column可以写成column="{deptId=id}"
  6. -->
  7. <resultMap type="com.bw.mybatis.bean.Department" id="MyDeptStep">
  8. <id column="id" property="id"/>
  9. <id column="dept_name" property="departmentName"/>
  10. <collection property="emps"
  11. select="com.bw.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
  12. column="{id}" fetchType="lazy"></collection>
  13. </resultMap>
  14. <!-- public Department getDeptByIdStep(Integer id); -->
  15. <select id="getDeptByIdStep" resultMap="MyDeptStep">
  16. select id,dept_name from tbl_dept where id=#{id}
  17. </select>
  18. <!-- public List<Employee> getEmpsByDeptId(Integer deptId); -->
  19. <select id="getEmpsByDeptId" resultType="com.bw.mybatis.bean.Employee">
  20. select * from tbl_employee where d_id=#{deptId}
  21. </select>