【MyBatis】篇三.自定义映射resultMap和动态SQL

谁借莪1个温暖的怀抱¢ 2023-10-08 19:13 125阅读 0赞

= = = =MyBatis整理= = = =

篇一.MyBatis环境搭建与增删改查
篇二.MyBatis查询与特殊SQL
篇三.自定义映射resultMap和动态SQL
篇四.MyBatis缓存和逆向工程

文章目录

  • 1、自定义映射
    • P1:测试数据准备
    • P2:字段和属性的映射关系
    • P3:多对一的映射关系
    • P4:一对多的映射关系
  • 2、动态SQL
    • 2.1 IF标签
    • 2.2 where标签
    • 2.3 trim标签
    • 2.4 choose、when、otherwise标签
    • 2.5 foreach标签
    • 2.6 SQL标签

1、自定义映射

若字段名和实体类的属性名不一致,则需要自定义映射。

P1:测试数据准备

员工表:
在这里插入图片描述
部门表:
在这里插入图片描述

定义对应的实体类:(注意,字段名是下划线命名,属性名是驼峰命名,不再一致了)
在这里插入图片描述

定义Mapper接口和映射文件:

在这里插入图片描述

P2:字段和属性的映射关系

当字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用_),实体类中的属性名符合Java的规则(使用驼峰),此时使用之前的自动映射resultType,则命名不一致的属性值为null,解决思路有三种:

思路一:给字段名起别名,使其和属性名保持一致

  1. <!--List<Emp> getAllEmp();-->
  2. <select id="getAllEmp" resultType="Emp">
  3. select eid,emp_name empName,age,sex,email from t_emp
  4. </select>

思路二:设置全局配置,将下划线_自动映射为驼峰

在核心配置文件mybatis-config.xml中:

  1. <settings>
  2. <!--将表中字段的下划线自动转换为驼峰-->
  3. <setting name="mapUnderscoreToCamelCase" value="true"/>
  4. </settings>
  5. <!--注意这种只能转换规范命名,即emp_name映射为empName-->

此时,SQL语句正常写即可。

思路三:使用resultMap,不再使用之前的resultType做自动映射

  1. <resultMap id="empResultMap" type="Emp">
  2. <id property="eid" column="eid"></id>
  3. <result property="empName" column="emp_name"></result>
  4. <result property="age" column="age"></result>
  5. <result property="sex" column="sex"></result>
  6. <result property="email" column="email"></result>
  7. </resultMap>
  8. <!--List<Emp> getAllEmp();-->
  9. <select id="getAllEmp" resultMap="empResultMap">
  10. select * from t_emp
  11. </select>
  12. <!--注意别只写不一样的属性,一样的也得写一遍-->

resultMap即设置自定义映射关系:

  • 属性:
    。 id:我定义的映射的唯一标识,不能重复,给select标签中的resultMap属性用
    。 type:查询的数据要映射的实体类的类型
  • 子标签:
    。 id:设置主键的映射关系
    。 result:设置普通字段的映射关系
  • 子标签属性:
    。 property:设置映射关系中实体类中的属性名
    。 column:设置映射关系中表中的字段名

运行结果:
在这里插入图片描述

P3:多对一的映射关系

多对一的时候,在’多’的这边设置,设置’一’所对应的对象;在’一’那边,设置’多’的对象集合 。处理多对一的映射关系(如查询企业员工信息及其部门)有三种实现方式:

  1. public class Emp {
  2. private Integer eid;
  3. private String empName;
  4. private Integer age;
  5. private String sex;
  6. private String email;
  7. /**
  8. * 这里设置'一'所对应的对象
  9. */
  10. private Dept dept;
  11. //...构造器、get、set方法等
  12. }

思路一:使用级联属性赋值

  1. <resultMap id="empAndDeptResultMapOne" type="Emp">
  2. <id property="eid" column="eid"></id>
  3. <result property="empName" column="emp_name"></result>
  4. <result property="age" column="age"></result>
  5. <result property="sex" column="sex"></result>
  6. <result property="email" column="email"></result>
  7. <result property="dept.did" column="did"></result>
  8. <result property="dept.deptName" column="dept_name"></result>
  9. </resultMap>
  10. <!--Emp getEmpAndDept(@Param("eid")Integer eid);-->
  11. <select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
  12. select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid}
  13. </select>

思路二:通过association处理多对一的映射

  • association:处理多对一映射关系
  • property:需要处理多对一映射关系的属性名
  • javaType:该属性的类型

    1. <id property="eid" column="eid"></id>
    2. <result property="empName" column="emp_name"></result>
    3. <result property="age" column="age"></result>
    4. <result property="sex" column="sex"></result>
    5. <result property="email" column="email"></result>
    6. <association property="dept" javaType="Dept">
    7. <id property="did" column="did"></id>
    8. <result property="deptName" column="dept_name"></result>
    9. </association>

以上的逻辑是:在association中写要处理多对一关系的属性名dept,再说明该属性的Java类——>知道类型,通过反射拿到该类型的属性did和deptName——>将查询出来的字段赋值给属性——>Dept类的对象有了——>赋值给属性dept

思路三:分步查询处理多对一的映射

先查询员工信息–>拿到部门id–>根据部门id查询部门信息—>将信息赋值给dept属性

  1. 引用属性--那个属性的值也是一个对象,丈夫是一个对象:
  2. 有姓名属性、年龄属性、妻子属性。妻子属性就是一个引用属性,里面是个对象,有它自己的属性
  3. //EmpMapper里的方法
  4. /**
  5. * 通过分步查询,员工及所对应的部门信息
  6. * 分步查询第一步:查询员工信息
  7. */
  8. Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
  • property:即要处理多对一映射关系的属性名
  • select:即设置分布查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)
  • column:设置分布查询的条件,要根据员工表的did去查询部门信息,即第二个SQL要根据什么去查

    1. <id property="eid" column="eid"></id>
    2. <result property="empName" column="emp_name"></result>
    3. <result property="age" column="age"></result>
    4. <result property="sex" column="sex"></result>
    5. <result property="email" column="email"></result>
    6. <association property="dept"
    7. select="com.llg.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
    8. column="did"></association>

第二步:

  1. //DeptMapper里的方法
  2. /**
  3. * 通过分步查询,员工及所对应的部门信息
  4. * 分步查询第二步:通过did查询员工对应的部门信息
  5. * 这里的查询结果要给Emp的dept属性赋值,所以返回类型Dept
  6. */
  7. Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
  8. <!--此处的resultMap仅是处理字段和属性的映射关系,不想写就开启setting后使用resultType-->
  9. <resultMap id="EmpAndDeptByStepTwoResultMap" type="Dept">
  10. <id property="did" column="did"></id>
  11. <result property="deptName" column="dept_name"></result>
  12. </resultMap>
  13. <!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
  14. <select id="getEmpAndDeptByStepTwo" resultMap="EmpAndDeptByStepTwoResultMap">
  15. select * from t_dept where did = #{did}
  16. </select>

测试类:

  1. @Test
  2. public void testGetEmpAndDeptByStep() {
  3. SqlSession sqlSession = SqlSessionUtils.getSqlSession();
  4. EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
  5. Emp emp = emmpMapper.getEmpAndDeptByStepOne(5);
  6. System.out.println(emp);
  7. }

运行结果:
在这里插入图片描述
梳理下逻辑:
在这里插入图片描述

分步查询的好处–延迟加载

分步查询,实现一个功能分了两步,但这两步各自也是一个单独的功能。这就分步查询的好处—实现延迟加载

在核心配置文件中配置全局信息(setting标签):

  • lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
  • aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。 否则,每个属性会按需加载





此时:我只获取员工名称,可以发现第二句SQL并未执行,这就是按需加载,获取的数据是什么,就只会执行相应的sql

在这里插入图片描述

而当获取部门信息的时候,两句就都会执行。为了清晰看到效果,先关掉延迟加载,可以看到是两句SQL都执行完了,再拿数据:
在这里插入图片描述

再开启延迟加载,可以看到是先执行了一句SQL,拿到了员工姓名,后面需要部门信息的时候,又执行了第二句SQL,这就是按需加载!!!
在这里插入图片描述
更改了全局设置,是否延迟加载则是对所有SQL的。当开启了全局的延迟加载(注意fetchType的前提是开启全局),要想单独控制某一个,可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载:

  • fetchType=“lazy(延迟加载)”
  • fetchType=“eager(立即加载)”

P4:一对多的映射关系

多对一的时候,在’多’的这边设置,设置’一’所对应的对象;在’一’那边,设置’多’的对象集合 。

  1. public class Dept {
  2. private Integer did;
  3. private String deptName;
  4. //'多'的类型的集合
  5. private List<Emp> emps;
  6. //...构造器、get、set方法等
  7. }

思路一:使用collection

  • collection:处理一对多的映射关系
  • ofType:表示该属性所对应的集合中存储的数据的类型

    public interface DeptMapper{

    1. /**
    2. * 查询部门即其下的所有员工信息
    3. */
    4. Dept getDeptAndEmp(@Param("did") Integer did);

    }

    1. <id property="did" column="did"></id>
    2. <result property="deptName" column="dept_name"></result>
    3. <collection property="emps" ofType="Emp">
    4. <id property="eid" column="eid"></id>
    5. <result property="empName" column="emp_name"></result>
    6. <result property="age" column="age"></result>
    7. <result property="sex" column="sex"></result>
    8. <result property="email" column="email"></result>
    9. </collection>

思路二:分步查询

第一步:

  1. public interface DeptMapper{
  2. /**
  3. * 通过分步查询,查询部门及对应的所有员工信息
  4. * 分步查询第一步:查询部门信息
  5. */
  6. Dept getDeptAndEmpByStepOne(@Param("did") Integer did);
  7. }
  8. <resultMap id="DeptAndEmpByStepOneResultMap" type="Dept">
  9. <id property="did" column="did"></id>
  10. <result property="deptName" column="dept_name"></result>
  11. <collection property="emps"
  12. select="com.llg.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
  13. column="did"></collection>
  14. </resultMap>
  15. <!--Dept getDeptAndEmpByStepOne(@Param("did") Integer did);-->
  16. <select id="getDeptAndEmpByStepOne" resultMap="DeptAndEmpByStepOneResultMap">
  17. select * from t_dept where did = #{did}
  18. </select>

第二步:

  1. public interface EmpMapper{
  2. /**
  3. * 通过分步查询,查询部门及对应的所有员工信息
  4. * 分步查询第二步:根据部门id查询部门中的所有员工
  5. */
  6. List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);
  7. }
  8. <!--List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);-->
  9. <select id="getDeptAndEmpByStepTwo" resultType="Emp">
  10. select * from t_emp where did = #{did}
  11. </select>

结果:
在这里插入图片描述
当然,分布查询在这儿也可以得到验证:
在这里插入图片描述


2、动态SQL

Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题

2.1 IF标签

  1. public interface DynamicSQLMapper{
  2. /**
  3. * 多条件查询
  4. */
  5. List<Emp> getEmpByCondition(Emp emp);
  6. }
  • if标签可通过test属性对表达式进行判断,若表达式的结果为true,则标签中的内容会拼接到SQL中,反之标签中的内容不会拼接。
  • 在where后面添加一个恒成立条件1=1,这个条件不会影响查询结果,而又可以很好的拼接后面的SQL:当empName传过来为空,select * from t_emp where and age = ? and sex = ? and email = ?,此时where与and连用,SQL语法错误

测试:
在这里插入图片描述

2.2 where标签

当where标签中有内容时,会自动生成where关键字,并将内容前多余的and或者or去掉,而当where标签中没内容时,where关键字也就不再生成。

  1. <!--List<Emp> getEmpByCondition(Emp emp);-->
  2. <select id="getEmpByCondition" resultType="Emp">
  3. select * from t_emp
  4. <where>
  5. <if test="empName != null and empName !=''">
  6. emp_name = #{empName}
  7. </if>
  8. <if test="age != null and age !=''">
  9. and age = #{age}
  10. </if>
  11. <if test="sex != null and sex !=''">
  12. and sex = #{sex}
  13. </if>
  14. <if test="email != null and email !=''">
  15. and email = #{email}
  16. </if>
  17. </where>
  18. </select>

where标签和if标签一般配合使用:

  • 若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
  • 若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的and/or去掉


    emp_name = #{empName} and


    age = #{age}

当empName有值,age为空,则SQL为:

  1. select * from d_emp where emp_name="llg" and
  2. //此处where标签去不掉and了

2.3 trim标签

trim用于去掉或添加标签中的内容,当标签中有内容的时候:

  • prefix:在trim标签中的内容的前面添加某些指定内容
  • suffix:在trim标签中的内容的后面添加某些指定内容
  • prefixOverrides:在trim标签中的内容的前面去掉某些指定内容
  • suffixOverrides:在trim标签中的内容的后面去掉某些指定内容

当标签中没有内容的时候,trim标签也没有任何效果

  1. <!--List<Emp> getEmpByCondition(Emp emp);-->
  2. <select id="getEmpByCondition" resultType="Emp">
  3. select * from t_emp
  4. <trim prefix="where" suffixOverrides="and|or">
  5. <if test="empName != null and empName !=''">
  6. emp_name = #{empName} and
  7. </if>
  8. <if test="age != null and age !=''">
  9. age = #{age} and
  10. </if>
  11. <if test="sex != null and sex !=''">
  12. sex = #{sex} or
  13. </if>
  14. <if test="email != null and email !=''">
  15. email = #{email}
  16. </if>
  17. </trim>
  18. </select>

测试类:

  1. //测试类
  2. @Test
  3. public void getEmpByCondition() {
  4. SqlSession sqlSession = SqlSessionUtils.getSqlSession();
  5. DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
  6. List<Emp> emps= mapper.getEmpByCondition(new Emp(null, "甲", null, null, null, null));
  7. System.out.println(emps);
  8. }

在这里插入图片描述

2.4 choose、when、otherwise标签

choose…when…otherwise相当于if…else if…else

  • when即if或者else if,至少要有一个,when后面的test条件成立,则拼接
  • otherwise相当于else,最多只能有一个

测试程序:

  1. @Test
  2. public void getEmpByChoose() {
  3. SqlSession sqlSession = SqlSessionUtils.getSqlSession();
  4. DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
  5. List<Emp> emps = mapper.getEmpByChoose(new Emp(null, "", null, "", "", null));
  6. System.out.println(emps);
  7. }

结果:
在这里插入图片描述

2.5 foreach标签

foreeach标签的属性有:

  • collection:设置要循环的数组或集合
  • item:表示集合或数组中的每一个数据
  • separator:设置循环体之间的分隔符,分隔符前后默认有一个空格,如,
  • open:设置foreach标签中的内容的开始符
  • close:设置foreach标签中的内容的结束符

通过数组实现批量删除

  1. public interface DynamicSQLMapper{
  2. /**
  3. * 通过数组实现批量删除
  4. */
  5. int deleteMoreByArray(List<Integer> eids);
  6. }

只论SQL,批量删除的实现可以通过以下两种写法:

  1. delete from t_emp where eid in (6,7,8);
  2. delte from t_emp where eid=6 or eid=7 or eid=8;

关于第一种SQL的实现:

在这里插入图片描述
优化一下:

  1. <!--int deleteMoreByArray(Integer[] eids);-->
  2. <delete id="deleteMoreByArray">
  3. delete from t_emp where
  4. <foreach collection="eids" item="eid" separator="or">
  5. eid = #{eid}
  6. </foreach>
  7. </delete>

关于第二种SQL的实现:

  1. <!--int deleteMoreByArray(Integer[] eids);-->
  2. <delete id="deleteMoreByArray">
  3. delete from t_emp where eid in
  4. <foreach collection="eids" item="eid" separator="," open="(" close=")">
  5. #{eid}
  6. </foreach>
  7. </delete>

测试:

  1. @Test
  2. public void deleteMoreByArray() {
  3. SqlSession sqlSession = SqlSessionUtils.getSqlSession();
  4. DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
  5. int result = mapper.deleteMoreByArray(new Integer[]{
  6. 6, 7, 8});
  7. System.out.println(result);
  8. }

结果:
在这里插入图片描述

在这里插入图片描述

通过集合实现批量插入

  1. public interface DynamicSQLMapper{
  2. /**
  3. * 通过集合实现批量添加
  4. */
  5. int insertMoreByList(List<Emp> emps);
  6. }

只说SQL,写法应该是:

  1. insert into t_emp values
  2. (a1,a2,a3),
  3. (b1,b2,b3),
  4. (v1,v2,v3);

使用foreach动态实现:

  1. <!--int insertMoreByList(@Param("emps") List<Emp> emps);-->
  2. <insert id="insertMoreByList">
  3. insert into t_emp values
  4. <foreach collection="emps" item="emp" separator=",">
  5. (null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
  6. </foreach>
  7. </insert>

注意,这里不用open和close,批量插入的原SQL是每条数据中有括号,即每次循环有括号,而不是删除SQL中的开头和结尾有括号。

  1. //测试程序
  2. @Test
  3. public void insertMoreByList() {
  4. SqlSession sqlSession = SqlSessionUtils.getSqlSession();
  5. DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
  6. Emp emp1 = new Emp(null,"a",1,"男","123@321.com",null);
  7. Emp emp2 = new Emp(null,"b",1,"男","123@321.com",null);
  8. List<Emp> emps = Arrays.asList(emp1, emp2);
  9. int result = mapper.insertMoreByList(emps);
  10. System.out.println(result);
  11. }

结果:
在这里插入图片描述

2.6 SQL标签

在映射文件中,声明一段SQL片段,把常用的一段SQL进行记录,在要使用的地方使用include标签进行引入。

  • 声明

    eid,emp_name,age,sex,email
  • 引用

发表评论

表情:
评论列表 (有 0 条评论,125人围观)

还没有评论,来说两句吧...

相关阅读