MyBatis映射文件(二)

目录

一、select元素

1.参数介绍

2.resultType

(1)返回实体对象

(2)返回List集合

(3)返回Map集合,object>

(4)返回Map<主键类型,实体类>集合

3.setting设置自动映射

4.自定义resultMap映射

5.association

(1)联合查询

(2)association-嵌套结果集

(3)association-分步查询&延迟加载

(4)Collection-集合类型&嵌套结果集

(5)Collection-分布查询&延迟加载

(6)discriminator鉴别器


一、select元素

1.参数介绍

select元素用来定义查询语句

​ id:唯一标识符

​            用来引用这条语句,需要和接口的方法名一致

​ parametertype:参数类型

                      ​ 可以不传,MyBatis会根据TypeHandler自动推断

​ resultType:返回值类型

                      ​ 别名或全类名,如果返回的是集合,定义集合中元素的类型。不能和resultMap同时使用。

2.resultType

(1)返回实体对象

 <!-- public Employee getEmpByMap(Map<String, Object> map); -->
    <select id="getEmpByMap" resultType="com.itheima.domain.Employee">
        select * from ${tableName} where id=${id} and last_name=#{lastName}
    </select>

(2)返回List集合

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

(3)返回Map<String,Object>集合

​ 键值分别对应列名和对应的值

 <!--public Map<String, Object> getEmpByIdReturnMap(Integer id);  -->
    <select id="getEmpByIdReturnMap" resultType="map">
        select * from tbl_employee where id=#{id}
    </select>

(4)返回Map<主键类型,实体类>集合

​ 键值分别对应主键名和对应的实体类

<!--
public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName);  
-->
    <select id="getEmpByLastNameLikeReturnMap"
            resultType="com.itheima.domain.Employee">
        select * from tbl_employee where last_name like #{lastName}
    </select>

​ @MapKey注解:可以指定封装这个map的时候使用哪个属性作为主键

     @MapKey("lastName")
    public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);

3.setting设置自动映射

  • autoMappingBehavior默认是PARTIAL,开启自动映射的功能。唯一的要求是列名和javaBean属性名一致

  • 如果autoMappingBehavior设置为null则会取消自动映射

  • 数据库字段命名规范,POJO属性符合驼峰命名法,如A_COLUMN===aColumn,我们可以开启自动驼峰命名规则映射功能,mapUnderscoreToCamelCase=true

4.自定义resultMap映射

(1)属性

  • type:自定义规则的Java类型

  • id:唯一标识,用于标识一个result map

  • autoMapping:如果设置这个属性,MyBatis将会为这个ResultMap开启或者关闭自动映射。这个属性会覆盖全局的属性 autoMappingBehavior。默认值为:unset。

<resultMap type="com.itheima.domain.Employee" id="MySimpleEmp">
        <!--指定主键列的封装规则
        id:定义主键底层有优化;
        column:指定哪一列
        property:指定对应的javaBean属性
          -->
        <id column="id" property="id"/>
        <!-- 定义普通列封装规则 -->
        <result column="last_name" property="lastName"/>
        <!-- 其他不指定的列会自动封装:我们只要写resultMap就把全部的映射规则都写上。 -->
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
    </resultMap>
    
    <!-- resultMap:自定义结果集映射规则;  -->
    <!-- public Employee getEmpById(Integer id); -->
    <select id="getEmpById"  resultMap="MySimpleEmp">
        select * from tbl_employee where id=#{id}
    </select>

5.association

场景:

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

(1)联合查询

​ 级联属性封装结果集

<resultMap type="com.itheima.domain.Employee" id="MyDifEmp">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        <!-- dept为Employee的 Department dept成员变量名
             Department有id和departmentName属性
        -->
        <result column="did" property="dept.id"/>
        <result column="dept_name" property="dept.departmentName"/>
    </resultMap>

(2)association-嵌套结果集

 <resultMap type="com.itheima.domain.Employee" id="MyDifEmp2">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        
        <!--  association可以指定联合的javaBean对象
                property="dept":指定哪个属性是联合的对象
                javaType:指定这个属性对象的类型[不能省略]
        -->
        <association property="dept" javaType="com.itheima.domain.Department">
            <id column="did" property="id"/>
            <result column="dept_name" property="departmentName"/>
        </association>
    </resultMap>

(3)association-分步查询&延迟加载

  • 先按照员工id查询员工信息

  • 根据查询出来的d_id去部门表查出部门信息

  • 部门设置到员工中

 <resultMap type="com.itheima.domain.Employee" id="MyEmpByStep">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!-- association定义关联对象的封装规则
                select:表明当前属性是调用select指定的方法查出的结果
                column:指定将哪一列的值传给这个方法
            
            流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
         -->
        <association property="dept" 
            select="com.itheima.domain.DepartmentMapper.getDeptById"
            column="d_id">
        </association>
  • 开启延迟加载和属性按需加载需要进行如下配置:

<settings>  
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
</settings>

(4)Collection-集合类型&嵌套结果集

 <!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则  -->
    <resultMap type="com.itheima.domain.Department" id="MyDept">
        <id column="did" property="id"/>
        <result column="dept_name" property="departmentName"/>
        <!-- 
            collection定义关联集合类型的属性的封装规则 
            ofType:指定集合里面元素的类型
        -->
        <collection property="emps" ofType="com.itheima.domain.Employee">
            <!-- 定义这个集合中元素的封装规则 -->
            <id column="eid" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
        </collection>
    </resultMap>

(5)Collection-分布查询&延迟加载

<resultMap type="com.itheima.domain.Department" id="MyDeptStep">
        <id column="id" property="id"/>
        <id column="dept_name" property="departmentName"/>
        <collection property="emps" 
            select="com.itheima.domain.EmployeeMapperPlus.getEmpsByDeptId"
            column="{id}" fetchType="lazy"></collection>
         <!--
                多列的值传递过去:
                    将多列的值封装map传递;
                    column="{key1=column1,key2=column2}"
                fetchType="lazy":表示使用延迟加载;
                    - lazy:延迟
                    - eager:立即
          -->
</resultMap>

(6)discriminator鉴别器

<!-- <discriminator javaType=""></discriminator>
        鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
        封装Employee:
            如果查出的是女生:就把部门信息查询出来,否则不查询;
            如果是男生,把last_name这一列的值赋值给email;
     -->
     <resultMap type="com.itheima.domain.Employee" id="MyEmpDis">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!--
            column:指定判定的列名
            javaType:列值对应的java类型  -->
        <discriminator javaType="string" column="gender">
            <!--女生  resultType:指定封装的结果类型;不能缺少。/resultMap-->
            <case value="0" resultType="com.itheima.domain.Employee">
                <association property="dept" 
                    select="com.itheima.domain.DepartmentMapper.getDeptById"
                    column="d_id">
                </association>
            </case>
            <!--男生 ;如果是男生,把last_name这一列的值赋值给email; -->
            <case value="1" resultType="com.itheima.domain.Employee">
                <id column="id" property="id"/>
                <result column="last_name" property="lastName"/>
                <result column="last_name" property="email"/>
                <result column="gender" property="gender"/>
            </case>
        </discriminator>
     </resultMap>

猜你喜欢

转载自blog.csdn.net/fy_java1995/article/details/82811351
今日推荐