SpringBoot——Mybatis-XML映射文件—动态SQL

使用XML映射文件配置SQL语句的规范 

XML文件当中的Mapper标签里面使用的select标签的id属性是Mapper接口里面的方法名,resultType属性名是SQL语句要返回的对象类型。 

MybatisX插件

用于管理接口方法和映射文件的关系

注解和XML配置SQL语句两种选择 

 动态SQL——where标签和if标签的使用

 在如下的图中,可以只需要输入其中一些值来进行查询,但是在使用注解进行开发时没法做到。

 要实现的效果是查询时有哪些字段就根据哪些字段来查询。

只输入 姓名查询后输出如下,预编译SQL中只有一个条件。

 但是这里还有一个问题,假如name为空时其他判断不为空组装的SQL语句会有问题

没有name时,where 后面的条件会出现一个and

条件全部为空时,where后面直接没有条件了,因此要修改成如下的形式

使用提供的where标签,会动态的根据里面的if标签成立与否生成where

还有一个作用就是可以自动去除多余的and和or

<select id="list" resultType="com.example.pojo.Emp">
        select * from emp
        <where>
        <if test="name!=null">
          name like concat('%',#{name},'%')
       </if>
        <if test="gender!=null">
            and gender = #{gender}
        </if>
        <if test="begin!=null and end!=null">
            and
            entrydate between #{begin} and #{end}
        </if>
        </where>
             order by update_time desc
    </select>

 

 if和set标签-案例

在更新操作里面,如果传进来的emp对象的字段只更新了一部分,那么按照原本的方法会出现剩下的字段变为null,这是不合理的,我们要做的是根据哪些字段有更新值才更新。

@Update("update emp set username = #{username}, name = #{name}, gender = #{gender}, image = #{image}," +
        " job = #{job}, entrydate = #{entrydate}, dept_id = #{deptId},update_time = #{updateTime} where id = #{id}")
public void update(Emp emp);

使用XML配置开发SQL语句

(按住ctrl+alt+L格式化)

 <update id="update">
        update emp
        set
        <if test="username!=null">username = #{username},</if>
        <if test="name!=null">name = #{name},</if>
        <if test="gender!=null">gender = #{gender},</if>
        <if test="image!=null">image = #{image},</if>
        <if test="job!=null">job = #{job},</if>
        <if test="entrydate!=null">entrydate = #{entrydate},</if>
        <if test="deptId!=null">dept_id = #{deptId},</if>
        <if test="updateTime!=null">update_time = #{updateTime}</if>
        where id = #{id}
    </update>

但是这里也会有一个问题如下 ,只传递了一个值多了一个逗号

解决方案

使用myabtis提供的set标签,可以把后面的逗号自动去除

<update id="update">
        update emp
        <set>
            <if test="username!=null">username = #{username},</if>
            <if test="name!=null">name = #{name},</if>
            <if test="gender!=null">gender = #{gender},</if>
            <if test="image!=null">image = #{image},</if>
            <if test="job!=null">job = #{job},</if>
            <if test="entrydate!=null">entrydate = #{entrydate},</if>
            <if test="deptId!=null">dept_id = #{deptId},</if>
            <if test="updateTime!=null">update_time = #{updateTime}</if>
        </set>
        where id = #{id}
    </update>

  动态SQL——foreach标签

要实现一个批量删除的操作,将传进来的id一个一个删除元组

在Mapper接口中新增如下方法,使用List传进去所有需要删除的id的集合

public void deleteByIds(List<Integer> ids);

根据SQL语句比如有  delete from emp where id in  (18,19,20)

在XML中如下配置

<delete id="deleteByIds">
        delete from emp where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

四个属性

collection: 要遍历的集合 
item: 遍历出来的元素
separator: 分隔符
open: 遍历开始前拼接的SQL字段

close: 遍历开始后拼接的SQL字段

单元测试:

  @Test
    public void testdelete(){
        List<Integer> ids= Arrays.asList(14,15,16);
        empMapper.deleteByIds(ids);
    }

输出如下

   动态SQL——sql标签和include标签

需求分析 : 如下的两个select语句中假如将来有修改,那么需要改动的地方很多,所以要将公共部分抽取出来进行封装。

此处借助<sql>和<include>标签进行实现 

使用<sql>进行抽取,有一个id属性作为唯一标识,在需要用到的地方使用<include>引入,有一个refid就是对应要引入的<sql>片段的属性值 

 最后总结3个准则和6个标签

猜你喜欢

转载自blog.csdn.net/m0_62327332/article/details/129770482