重新学习MyBatis(三)

resultMap,association,collection,discriminator的使用:

conf/com/it/mybatis/dao/DepartmentMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.it.mybatis.dao.DepartmentMapper">
    <!--public Department getDeptById(Integer id);  -->
    <select id="getDeptById" resultType="com.it.mybatis.bean.Department">
        select id,dept_name departmentName from tbl_dept where id=#{id}
    </select>
    
    
    
    <!-- 
    public class Department {
            private Integer id;
            private String departmentName;
            private List<Employee> emps;
      did  dept_name  ||  eid  last_name  email   gender  
     -->
     
    <!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则  -->
    <resultMap type="com.it.mybatis.bean.Department" id="MyDept">
        <id column="did" property="id"/>
        <result column="dept_name" property="departmentName"/>
        <!-- 
            collection定义关联集合类型的属性的封装规则 
            ofType:指定集合里面元素的类型
        -->
        <collection property="emps" ofType="com.it.mybatis.bean.Employee">
            <!-- 定义这个集合中元素的封装规则 -->
            <id column="eid" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
        </collection>
    </resultMap>
    <!-- public Department getDeptByIdPlus(Integer id); -->
    <select id="getDeptByIdPlus" resultMap="MyDept">
        SELECT d.id did,d.dept_name dept_name,
                e.id eid,e.last_name last_name,e.email email,e.gender gender
        FROM tbl_dept d
        LEFT JOIN tbl_employee e
        ON d.id=e.d_id
        WHERE d.id=#{id}
    </select>
    
    <!-- collection:分段查询 -->
    <resultMap type="com.it.mybatis.bean.Department" id="MyDeptStep">
        <id column="id" property="id"/>
        <id column="dept_name" property="departmentName"/>
        <collection property="emps" 
            select="com.it.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
            column="{deptId=id}" fetchType="lazy"></collection>
    </resultMap>
    <!-- public Department getDeptByIdStep(Integer id); -->
    <select id="getDeptByIdStep" resultMap="MyDeptStep">
        select id,dept_name from tbl_dept where id=#{id}
    </select>
    
    <!-- 扩展:多列的值传递过去:
            将多列的值封装map传递;
            column="{key1=column1,key2=column2}"
        fetchType="lazy":表示使用延迟加载;
                - lazy:延迟
                - eager:立即
     -->
</mapper>

conf/com/it/mybatis/dao/EmployeeMapperPlus.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.it.mybatis.dao.EmployeeMapperPlus">
  6 
  7     <!--自定义某个javaBean的封装规则
  8     type:自定义规则的Java类型
  9     id:唯一id方便引用
 10       -->
 11     <resultMap type="com.it.mybatis.bean.Employee" id="MySimpleEmp">
 12         <!--指定主键列的封装规则
 13         id定义主键会底层有优化;
 14         column:指定哪一列
 15         property:指定对应的javaBean属性
 16           -->
 17         <id column="id" property="id"/>
 18         <!-- 定义普通列封装规则 -->
 19         <result column="last_name" property="lastName"/>
 20         <!-- 其他不指定的列会自动封装:我们只要写resultMap就把全部的映射规则都写上。 -->
 21         <result column="email" property="email"/>
 22         <result column="gender" property="gender"/>
 23     </resultMap>
 24     
 25     <!-- resultMap:自定义结果集映射规则;  -->
 26     <!-- public Employee getEmpById(Integer id); -->
 27     <select id="getEmpById"  resultMap="MySimpleEmp">
 28         select * from tbl_employee where id=#{id}
 29     </select>
 30     
 31     <!-- 
 32     场景一:
 33         查询Employee的同时查询员工对应的部门
 34         Employee===Department
 35         一个员工有与之对应的部门信息;
 36         id  last_name  gender    d_id     did  dept_name (private Department dept;)
 37      -->
 38      
 39      
 40     <!--
 41         联合查询:级联属性封装结果集
 42       -->
 43     <resultMap type="com.it.mybatis.bean.Employee" id="MyDifEmp">
 44         <id column="id" property="id"/>
 45         <result column="last_name" property="lastName"/>
 46         <result column="gender" property="gender"/>
 47         <result column="did" property="dept.id"/>
 48         <result column="dept_name" property="dept.departmentName"/>
 49     </resultMap>
 50 
 51 
 52     <!-- 
 53         使用association定义关联的单个对象的封装规则;
 54      -->
 55     <resultMap type="com.it.mybatis.bean.Employee" id="MyDifEmp2">
 56         <id column="id" property="id"/>
 57         <result column="last_name" property="lastName"/>
 58         <result column="gender" property="gender"/>
 59         
 60         <!--  association可以指定联合的javaBean对象
 61         property="dept":指定哪个属性是联合的对象
 62         javaType:指定这个属性对象的类型[不能省略]
 63         -->
 64         <association property="dept" javaType="com.it.mybatis.bean.Department">
 65             <id column="did" property="id"/>
 66             <result column="dept_name" property="departmentName"/>
 67         </association>
 68     </resultMap>
 69     <!--  public Employee getEmpAndDept(Integer id);-->
 70     <select id="getEmpAndDept" resultMap="MyDifEmp">
 71         SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
 72         d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
 73         WHERE e.d_id=d.id AND e.id=#{id}
 74     </select>
 75     
 76     <!-- 使用association进行分步查询:
 77         1、先按照员工id查询员工信息
 78         2、根据查询员工信息中的d_id值去部门表查出部门信息
 79         3、部门设置到员工中;
 80      -->
 81      
 82      <!--  id  last_name  email   gender    d_id   -->
 83      <resultMap type="com.it.mybatis.bean.Employee" id="MyEmpByStep">
 84          <id column="id" property="id"/>
 85          <result column="last_name" property="lastName"/>
 86          <result column="email" property="email"/>
 87          <result column="gender" property="gender"/>
 88          <!-- association定义关联对象的封装规则
 89              select:表明当前属性是调用select指定的方法查出的结果
 90              column:指定将哪一列的值传给这个方法
 91              
 92              流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
 93           -->
 94          <association property="dept" 
 95              select="com.it.mybatis.dao.DepartmentMapper.getDeptById"
 96              column="d_id">
 97          </association>
 98      </resultMap>
 99      <!--  public Employee getEmpByIdStep(Integer id);-->
100      <select id="getEmpByIdStep" resultMap="MyEmpByStep">
101          select * from tbl_employee where id=#{id}
102          <if test="_parameter!=null">
103              and 1=1
104          </if>
105      </select>
106      
107      <!-- 可以使用延迟加载(懒加载);(按需加载)
108          Employee==>Dept:
109              我们每次查询Employee对象的时候,都将一起查询出来。
110              部门信息在我们使用的时候再去查询;
111              分段查询的基础之上加上两个配置:
112       -->
113     <!-- ==================association============================ -->
114     
115     <!-- 
116     场景二:
117         查询部门的时候将部门对应的所有员工信息也查询出来:注释在DepartmentMapper.xml中
118      -->
119     <!-- public List<Employee> getEmpsByDeptId(Integer deptId); -->
120     <select id="getEmpsByDeptId" resultType="com.it.mybatis.bean.Employee">
121         select * from tbl_employee where d_id=#{deptId}
122     </select>
123     
124     
125     
126     <!-- =======================鉴别器============================ -->
127     <!-- <discriminator javaType=""></discriminator>
128         鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
129         封装Employee:
130             如果查出的是女生:就把部门信息查询出来,否则不查询;
131             如果是男生,把last_name这一列的值赋值给email;
132      -->
133      <resultMap type="com.it.mybatis.bean.Employee" id="MyEmpDis">
134          <id column="id" property="id"/>
135          <result column="last_name" property="lastName"/>
136          <result column="email" property="email"/>
137          <result column="gender" property="gender"/>
138          <!--
139              column:指定判定的列名
140              javaType:列值对应的java类型  -->
141          <discriminator javaType="string" column="gender">
142              <!--女生  resultType:指定封装的结果类型;不能缺少。/resultMap-->
143              <case value="0" resultType="com.it.mybatis.bean.Employee">
144                  <association property="dept" 
145                      select="com.it.mybatis.dao.DepartmentMapper.getDeptById"
146                      column="d_id">
147                  </association>
148              </case>
149              <!--男生 ;如果是男生,把last_name这一列的值赋值给email; -->
150              <case value="1" resultType="com.it.mybatis.bean.Employee">
151                  <id column="id" property="id"/>
152                  <result column="last_name" property="lastName"/>
153                  <result column="last_name" property="email"/>
154                  <result column="gender" property="gender"/>
155              </case>
156          </discriminator>
157      </resultMap>
158 </mapper>

 

posted on 2020-05-10 15:32  betterLearing  阅读(127)  评论(0编辑  收藏  举报