MyBatis常用到的动态sql

简介

相信大家没用Mybatis之前,都碰到过各种条件判断拼接SQL、需要去掉多余的逗号等痛苦,Mybatis中的动态SQL能很好的解决上面说的情况,可以很灵活的组装SQL语句,从而提高开发效率。

1、SQL的动态拼接有哪些

if标签

where标签

choose when otherwise标签

set标签

trim标签

bind标签

sql和include标签 foreach标签

2、if标签:

test中写判断条件 参数直接paramN或者别名 特点: 只要成立就拼接在Sql语句中,都成立就全部都拼接 注意: where子句中加上1=1来规避and的风险

<select id="uid" resultType="UserEntity"> 

select * from UserEntity where 1=1 

<if test="param1!=null and param1!=''"> and outno=#{param1} </if> 

<if test="param2!=null and param2!=''"> and inno=#{param2} </if> 

</select>

条件模糊查以及查询时间段内数据

<select id="uid" resultType="UserEntity"> 

select * from UserEntity where 1=1 

<if test="param1!=null and param1!=''"> and outno=#{param1} </if> 

<if test="param2!=null and param2!=''">and inno LIKE CONCAT('%',#{param2 },'%' ) </if> 

<if test="effectiveTime !=null and effectiveTime !=''">   and begin_time &lt;= #{effectiveTime}     

//effectiveTime 是封的查询条件类的查询字段  and end_time &gt;= #{effectiveTime}     

//begin_time,end_time 对应数据库字段。 </if> 

</select>

3、where标签:

特点:

会自动的给Sql语句添加where关键字,并将第一个and去除。

<select id="uid" resultType="UserEntity">    

select * from UserEntity    

<where>    

<if test="param1!=null and param1!=''">    and outno=#{param1}    </if>    <if test="param2!=null and param2!=''">    and inno=#{param2}    </if>    </where>   

</select>

4、choose when otherwise标签

特点:

条件只要有一个成立,其他的就不会再判断了。

如果没有成立的条件则默认执行otherwise中的内容

<select id="uid" resultType="UserEntity"> 

select * from UserEntity 

<where> 

<choose> 

<when test="param1!=null and param1!=''"> and outno=#{param1} </when> <when test="param2!=null and param2!=''"> and inno=#{param2} </when> <otherwise> and 1=1 </otherwise> 

</choose> 

</where> 

</select>

5、set标签:

产生一个set关键字,自动去除最后一个逗号。

注意:

在判断条件中最后保持有一个永远成立的条件。避免sql错误。

<update id="uid"> 

update accountTable <set> 

<if test="aname!=null and aname!=''"> aname=#{aname}, </if> 

<if test="money !=null  and money !=''"> money=#{money}, </if> 

<if test="ano !=null  and ano !=''"> ano=#{ano}, </if> 

</set> where  ano=#{ano} 

</update>

6、trim标签:

prefix:在trim的内容前添加指定的内容

prefixOverrides在trim的内容前去除指定的内容

suffix:在trim的内容后添加指定的内容

suffixOverrides:在trim的内容后去除指定的内容

注意:

先去除后添加

添加内容会默认添加一个空格。

<update id="upT" parameterType="account">     

update account     

<trim prefix="$" prefixOverrides="" suffix="" suffixOverrides="">     

<if test="ano !=null  and ano !=''">     ano=#{ano},     

</if>     

<if test="aname!=null and aname!=''">     aname=#{aname},     

</if>     

<if test="money !=null  and money !=''">   money=#{money},     

</if>     

</trim>     

where ano=#{ano} 

</update>

7、bind标签:

name:参数名

value:表达式,注意字符串拼接按照变量方式进行拼接

例如:

<bind name="money" value="'$'+money"/>

给参数重新赋值

<update id="upB" parameterType="account">      

<bind name="money" value="money+100"/>    

update account    

<trim prefix="set" suffixOverrides=",">    

<if test="ano !=null  and ano !=''">    ano=#{ano},    

</if>    

<if test="aname!=null and aname!=''">    aname=#{aname},    

</if>    

<if test="money !=null  and money !=''">    money=#{money},    

</if>     

</trim>    

where ano=#{ano} 

</update>

8、sql和include标签:

sql标签:在外部声明公用SQL语句

id

include标签:引入声明的公共SQL语句

refid:

优点:便于SQL的整体修改

缺点:难于阅读

<select id="selA" resultType="account">    

select <include refid="mysql">

</include> from account   

</select>   

<sql id="mysql">    ano,aname,apwd,money   

</sql>

9、foreach标签:

构造IN条件语句时需要对集合进行遍历,这时候可以使用foreach元素。foreach会去掉多余","。若集合为空,则不会执行foreach元素中的操作,但此时会多出"in"关键字,报错。

item:集合中的元素

index:元素所在集合的下标,迭代map时,index是键

collection:集合的类型,可选值list,array,除此之外还可以是@Param("name")、Map中的key、类的成员变量 open、

close:在首、尾拼接的字符

separator:每个元素间的分隔符

注: foreach标签支持List、Set、Map、Array等的遍历

迭代数组或者List、Set集合时,index是迭代次数,item是本次迭代获取的元素;

迭代Map(或Map.Entry对象的集合)时,index是键,item是值

collection属性介绍

传入单参数且是List时,collection="list"

传入单参数且是Array时,collection="array"

传入单参数且是Set时,需使用@Param注解,同下

传入单参数且使用@Param("name")时,collection="name",即和@Param注解的value属性值相同,此时list、array无效

传入多参数且封装成Map时,如map.put("ids", Arrays.asList(1, 2)),此时collection="ids"

传入多参数且封装成类时,如User类中有成员变量List<Integer> roleIds,此时collection="roleIds";若User类中有成员变量Role role,Role类中有成员变量prilIds,此时collection="role.prilIds"

<select id="selF" parameterType="list" resultType="account">    

select * from account where ano in    

<foreach collection="list" item="item" open="(" separator="," close=")">    #{item}    

</foreach> 

</select>   

<insert id="inF">    

insert into log values    

<foreach collection="list"  item="log" separator=",">    

(#{log.outno},#{log.inno},#{log.money})    

</foreach>     

</insert>

10、示例

批量修改的动态sql

controller层

/**     

* 批量修改     

*/    

@PutMapping("/aaa")    @ApiOperation(value = "下架")    

public R aaa(@RequestBody Asa asa) {        this.goodTestService.updateState(asa.getIds(),asa.getMsg());        return R.data("cg");    

}

service

package com.troy.testa.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.troy.testa.dto.GoodTestDTO; import com.troy.testa.entity.GoodTest; import org.springframework.data.domain.Pageable; import java.util.List; /** 

* (GoodTest)表服务接口 

* @author zhangh 

* @date 2021-03-21 15:31:34 

*/ 

public interface GoodTestService extends IService<GoodTest> {    

void updateState(String[] ids,String msg); 

}

serviceImp

@Override    

public void updateState(String[] ids,String msg) {        baseMapper.updateState(ids,msg);    

}

dao

package com.troy.testa.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.troy.testa.dto.GoodTestDTO; import com.troy.testa.entity.GoodTest; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import com.baomidou.mybatisplus.core.metadata.IPage; import java.util.List; 

/** 

* (GoodTest)表数据库访问层 

* @author zhangh 



*/ 

@Mapper public interface GoodTestDao extends BaseMapper<GoodTest> {    void updateState(@Param("ids") String[] ids,@Param("msg") String msg); 

}

xml

<?xml version="1.0" encoding="UTF-8"?> 

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 

<mapper namespace="com.troy.testa.dao.GoodTestDao">    

<update id="updateState">        

<foreach collection="ids" item="item" index="index" separator=";">            UPDATE good_test            

SET good_state = 1,good_name=#{msg}            

WHERE id = #{item}        

</foreach>    

</update>

postman测试


表已批量修改成功!

猜你喜欢

转载自blog.csdn.net/m0_58019259/article/details/127794278