ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

mybatis自定义参数

2022-08-20 13:00:20  阅读:137  来源: 互联网

标签:自定义 did 查询 参数 emp 设置 mybatis 属性 加载


首先我们先定义两个表t_emp / t_dept

由于我们的pojo中,使用了驼峰命名法,而数据表中使用的是下划线命名法

解决字段名和属性名不一致

为字段起别名,保持与属性名一致

select 列名 as 别名 from 表名

List<Emp> getAllEmpAs();
 <select id="getAllEmpAs" parameterType="Emp">
        select eid, emp_name as empName, age, sex, email from t_emp
    </select>

通过全局配置settings-mapUnderscoreToCamelCase

将下划线自动映射为驼峰

mybatis-config.xml

<settings>
        <!--**将下划线自动映射为驼峰**-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

使用resultMap

设置自定义映射关系,只在查询功能中使用

  • type属性,设置要对什么实体类进行处理

  • id属性,设置你resultMap的唯一标识

  • id标签设置主键的属性

  • result设置普通键的属性

  • association处理多对一的关系

  • collection处理一对多的关系

  • property设置属性名,必须是type属性设置的实体类属性名

  • column设置字段名,必须是sql语句查询出的字段名

List<Emp> getAllEmpResultMap();
   <resultMap id="empResultMap" type="Emp">
        <id property="eid" column="eid"/>
        <result property="empName" column="emp_name"/>
        <result property="age" column="age"/>
        <result property="sex" column="sex"/>
        <result property="email" column="email"/>
    </resultMap>
​
<select id="getAllEmpResultMap" resultMap="empResultMap">
        select eid, emp_name, age, sex, email from t_emp
    </select>

多对一的映射

这里就是说,多个员工在同一个部门里,查询员工对应所对应的部门信息

首先我们先设置emp+dept的mapper方法

Emp getEmpByIdLeftOutter(@Param("eid")Integer eid);
    <select id="getEmpByIdLeftOutter" resultMap="empAndDeptResultMap">
        selet *
        from t_emp as e
        left outter join t_dept as d
        on e.did = d.did
        where e.eid = #{eid}
    </select>

一条sql语句

级联属性赋值(用的少)
   <resultMap id="empAndDeptResultMap" type="Emp">
        <id property="eid" column="eid"/>
        <result property="empName" column="emp_name"/>
        <result property="age" column="age"/>
        <result property="sex" column="sex"/>
        <result property="email" column="email"/>         <result property="dept.did" column="did"/>
        <result property="dept.deptName" column="dept_name"/>
    </resultMap>
使用association标签
  • association:处理多对一的映射关系

    • property:需要处理的属性名

    • javaType:该属性的类型

    • select:设置分布查询的sql唯一标识(namespace.SQLid / 或mapper接口的全类名.方法名)

    • column:设置分布查询的条件

    <resultMap id="empAndDeptResultMap" type="Emp">
        <id property="eid" column="eid"/>
        <result property="empName" column="emp_name"/>
        <result property="age" column="age"/>
        <result property="sex" column="sex"/>
        <result property="email" column="email"/>     	  
        <association property="dept" javaType="Dept">
            <id property="did" column="did"/>
            <result property="deptName" column="dept_name"/>
         </association>
    </resultMap>

accoiation+多条sql语句分布查询(用的多)

 

第0步:打开setting设置下划线转驼峰命名
   <settings>
        <!--**将下划线自动映射为驼峰**-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
第一步:查询查询员工信息

association:处理多对一的映射关系

  • property:需要处理的属性名

  • javaType:在该属性的实体类名

  • select:设置分布查询的sql唯一标识(namespace.SQLid 或 mapper接口的全类名.方法名)

  • column:设置分布查询的条件

Emp getEmpByIdByStep(@Param("eid")Integer eid);
    <resultMap id="empAndDeptResultMapByStep" type="Emp">
        <id property="eid" column="eid"/>
        <result property="empName" column="emp_name"/>
        <result property="age" column="age"/>
        <result property="sex" column="sex"/>
        <result property="email" column="email"/>
        <association property="dept"
                     select="com.atguigu.mybatis.mappers.DeptMapper.getDeptById" column="did">
        </association>
    </resultMap>

    <select id="getEmpByIdByStep" resultMap="empAndDeptResultMapByStep">
        select *
        from t_user
        where id = #{eid}
    </select>
第二步:通过did查询员工所对应的部门
Dept getDeptById(@Param("did")Integer did);
   <select id="getDeptById" resultMap="Dept">
        selet *
        from t_dept
        where id = #{did}
    </select>

延迟加载

如果设置了延迟加载,分布查询只想执行第一步就只能触发第一步,如果想一二步一起执行,就触发执行两步,实现按需加载,获取的数据是什么,就只会执行相应的sql

分布查询可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息(settings)

  • lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象(分布查询的第 2 / 3 / n 步)都会延迟加载

  • aggressiveLazyLoading:按需加载,当开启时,任何方法的调用都会加载该对象的所有属性。否则,** **

  • 如果开启了延时加载,那么全局都开启了延时加载association的fetchType属性,"lazy / eager"设置延迟 / 立即加载。如果没开启延迟加载,那么这个属性无论设置什么,都是立即加载

mybatis-config.xml

   <!--全局配置-->
    <settings>
        <!--**将下划线自动映射为驼峰**-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--延迟加载全局开关-->
        <setting name="lazyLodingEnabled" value="true"/>
        <!--开启按需加载-->
        <setting name="aggressiveLazyLoading" value="true"/>
    </settings>

EmpMapper.xml

 <resultMap id="empAndDeptResultMapByStep" type="Emp">
        <id property="eid" column="eid"/>
        <result property="empName" column="emp_name"/>
        <result property="age" column="age"/>
        <result property="sex" column="sex"/>
        <result property="email" column="email"/>
        <association property="dept"
                     select="com.atguigu.mybatis.mappers.DeptMapper.getDeptById"
                     column="did"
                     fetchType="lazy">
        </association>
    </resultMap>

一对多映射

使用 (一).集合<多> 实现一对多

private List<Emp> emps;

collection

使用collection主要就是处理一对多的关系,也就是处理一个集合的关系

  • property:集合的名字

  • ofType:该属性所对应的集合中存储数据的类型

  • select:设置分布查询的sql唯一标识(namespace.SQLid 或 mapper接口的全类名.方法名)

  • column:设置分布查询的条件

  • fetchType:"lazy / eager"设置延迟 / 立即加载。如果没开启延迟加载,那么这个属性无论设置什么,都是立即加载

一步查询

(0)DeptMapper.xml.collection

这里emp不用设置它的dept属性,否则无限循环查询

    <resultMap id="DeptAndEmps" type="dept">
        <id property="did" column="did" />
        <result property="deptName" column="dept_name" />
        <collection property="emps" ofType="Emp">
            <id property="eid" column="eid"/>
            <result property="empName" column="emp_name"/>
            <result property="age" column="age"/>
            <result property="sex" column="sex"/>
            <result property="email" column="email"/>
        </collection>
    </resultMap>
(1)DeptMapper
Dept getDeptAndEmpsById(@Param("did")Integer did)
  <select id="getDeptAndEmpsById" resultMap="DeptAndEmps">
        select * 
        from t_dept as d 
        left outter join t_emp as e
        on d.did = e.did
        where d.did = #{did}
    </select>

分步查询

一般分步查询,第一步都用resultMap,第二步使用resultType,除非第二步里也要往下分步

(0)DetpMapper.xml.collection

注意这里不需要ofType了,直接把第二步查出来的当作集合了

<resultMap id="DeptAndEmpsByStep" type="detp">
        <id property="did" column="did" />
        <result property="deptName" column="dept_name" />
        <collection property="emps"
                    select="com.atguigu.mybatis.mappers.EmpMapper.getDeptAndEmpsByIdStepTwo"
                    column="did"/>
    </resultMap>
(1)第一步:根据did查询部门所有信息 DeptMappper
Dept getDeptById(@Param("did")Integer did);
    <select id="getDeptById" resultMap="DeptAndEmpsByStep">
        select *
        from t_dept
        where id = #{did}
    </select>
(2)第二步:根据did查询员工集合信息 EmpMapper
List<Emp> getDeptAndEmpsByIdStepTwo(@Param("did")Integer did);
 <select id="getDeptAndEmpsByIdStepTwo" resultType="Emp">
        select *
        from t_emp
        where did = #{did}
    </select>

标签:自定义,did,查询,参数,emp,设置,mybatis,属性,加载
来源: https://www.cnblogs.com/phonk/p/16607546.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有