关于mybatis:Mybatis-select记录封装

select记录封装

  • 返回一个List汇合, resultType要写汇合中元素的类型
<!-- public List<Employee> getEmpsByLastNameLike(String lastName); -->
<!--resultType:如果返回的是一个汇合,要写汇合中元素的类型  -->
<select id="getEmpsByLastNameLike" resultType="com.atguigu.mybatis.bean.Employee">
    select * from tbl_employee where last_name like #{lastName}
</select>
  • 返回一条记录的map, key为列名, 值就是对应的值
<!--public Map<String, Object> getEmpByIdReturnMap(Integer id);  -->
<select id="getEmpByIdReturnMap" resultType="map">
    select * from tbl_employee where id=#{id}
</select>
  • 多条记录封装成一个map, key为id, 值是记录封装后的javaBean
//@MapKey:通知mybatis封装这个map的时候应用哪个属性作为map的key
@MapKey("lastName")
public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);
<!--public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName);  -->
<select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee">
    select * from tbl_employee where last_name like #{lastName}
</select>

主动映射配置

  • 全局setting设置
  1. autoMappingBehavior默认为PARTIAL, 开启主动映射性能;惟一的要求是列名和javaBean属性名统一

2.mapUnderscoreToCamelCase=true, 开启主动驼峰命名标准映射性能

  1. 自定义resultMap, 实现高级映射性能

resultMap自定义映射规定

    <!--自定义某个javaBean的封装规定
    type:自定义规定的Java类型
    id:惟一id不便援用
      -->
    <resultMap type="com.atguigu.mybatis.bean.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>
  • 创立表
create table tb_dept (

id int(11) primary key auto_increment,

dept_name varchar(255)

)
  • 增加列

    alter table tb_emp add column d_id int(11);
  • 增加束缚

    alter table tb_emp add constraint fk_emp_dept foreign key(d_id) references tb_dept(id);

联结查问:级联属性封装后果集

场景一:
查问Employee的同时查问员工对应的部门;一个员工有与之对应的部门信息;

<!-- 
    场景一:
        查问Employee的同时查问员工对应的部门
        Employee===Department
        一个员工有与之对应的部门信息;
        id  last_name  gender    d_id     did  dept_name (private Department dept;)
     -->
     
     
    <!--
        联结查问:级联属性封装后果集
      -->
    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        <result column="did" property="dept.id"/>
        <result column="dept_name" property="dept.departmentName"/>
    </resultMap>

应用association定义关联的单个对象的封装规定;

<!-- 
        应用association定义关联的单个对象的封装规定;
     -->
    <resultMap type="com.atguigu.mybatis.bean.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.atguigu.mybatis.bean.Department">
            <id column="did" property="id"/>
            <result column="dept_name" property="departmentName"/>
        </association>
    </resultMap>

association分步查问

<!-- 应用association进行分步查问:
        1、先依照员工id查问员工信息
        2、依据查问员工信息中的d_id值去部门表查出部门信息
        3、部门设置到员工中;
     -->
     
     <!--  id  last_name  email   gender    d_id   -->
     <resultMap type="com.atguigu.mybatis.bean.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.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
             column="d_id">
         </association>
     </resultMap>

<!-- DepartmentMapper.xml -->
<mapper namespace="com.atguigu.mybatis.dao.DepartmentMapper">
    <!--public Department getDeptById(Integer id);  -->
    <select id="getDeptById" resultType="com.atguigu.mybatis.bean.Department">
        select id,dept_name departmentName from tbl_dept where id=#{id}
    </select>

association分步查问&提早加载

 <!-- 能够应用提早加载(懒加载);(按需加载)
         Employee==>Dept:
             咱们每次查问Employee对象的时候,都将一起查问进去。
             部门信息在咱们应用的时候再去查问;
             分段查问的根底之上加上两个配置:
      -->
    <!-- ==================association============================ -->
    
    <!-- mybatis-config.xml-->
    <!--显示的指定每个咱们须要更改的配置的值,即便他是默认的。避免版本更新带来的问题  -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- value:false 示意按需加载; 否则会总是加载 -->
        <setting name="aggressiveLazyLoading" value="false"/>

关联汇合

嵌套后果集的形式,应用collection标签定义关联的汇合类型的属性封装规定

场景二:
查问部门的时候将部门对应的所有员工信息也查问进去:正文在DepartmentMapper.xml中

<!-- 
    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.atguigu.mybatis.bean.Department" id="MyDept">
        <id column="did" property="id"/>
        <result column="dept_name" property="departmentName"/>
        <!-- 
            collection定义关联汇合类型的属性的封装规定 
            ofType:指定汇合外面元素的类型
        -->
        <collection property="emps" ofType="com.atguigu.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:分段查问

<!-- collection:分段查问 -->
    <resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep">
        <id column="id" property="id"/>
        <result column="dept_name" property="departmentName"/>
        <collection property="emps" 
            select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
            column="{deptId=id}" fetchType="lazy"></collection>
    </resultMap>

    
    <!-- 扩大:多列的值传递过来:
            将多列的值封装map传递;
            column="{key1=column1,key2=column2}"
        fetchType="lazy":示意应用提早加载;
                - lazy:提早
                - eager:立刻

鉴别器

mybatis能够应用discriminator判断某列的值,而后依据某列的值扭转封装行为

封装Employee:
        如果查出的是女生:就把部门信息查问进去,否则不查问;
        如果是男生,把last_name这一列的值赋值给email;
<!-- =======================鉴别器============================ -->
    
     <resultMap type="com.atguigu.mybatis.bean.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.atguigu.mybatis.bean.Employee">
                 <association property="dept" 
                     select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
                     column="d_id">
                 </association>
             </case>
             <!--男生 ;如果是男生,把last_name这一列的值赋值给email; -->
             <case value="1" resultType="com.atguigu.mybatis.bean.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>

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理