动态SQL

目录

if

where

trim

set

choose-when-otherwise

foreach

批量删除

 批量插入

sql标签与include标签


不能使用&&,只能使用 and

空值:name != null and name != ''

if

public List<Monster> findMonsterByAge(@Param(value = "agex") Integer age);
    <select id="findMonsterByAge" resultType="Monster" parameterType="Integer">
        select * from `monster` where 1 = 1
        <if test="agex >= 0">
            and `age` > #{agex}
        </if>
        <if test="xxx">
            and xxx
        </if>
    </select>

 

select * from monster where [1 = 1] [and] age > #{agex} and xxx

如果第一个不成立,要加 1 = 1,where 1 = 1 and xxx

如果全成立,要加and,where 1 = 1 and age > #{agex} and xxx

where

所有条件为空时,where标签保证不会生成where子句

自动去除某些条件前面多余的and或or 

    <select id="findMonsterByIdAndName" parameterType="Monster" resultType="Monster">
        select * from `monster`
        <where>
            <if test="id >= 0">
                and `id` > #{id}
            </if>
            <if test="name != null and name != ''">
                and `name` = #{name}
            </if>
        </where>
    </select>

trim

在trim标签前/后添加前/后缀

        preifx:加前缀

        suffix:加后缀

把trim标签内容的前/后缀去掉

        prefixOverrides:删除前缀

        suffixOverrides:删除后缀

 

set

主要用在update语句中,用来生成set关键字,同时去掉最后多余的 ","

只更新提交的不为空的字段,如果提交的数据是空或者“ ”,那么这个字段将不更新数据

正常的update语句:null也会传给数据库

    <update id="setMonster" parameterType="map">
        update `monster`
        <set>
            <if test="age != null and age != ''">
                `age` = #{age},
            </if>
            <if test="email != null and age != ''">
                `email` = #{email},
            </if>
        </set>
        where id = #{id}
    </update>

choose-when-otherwise

相当于if elseif elseif else

先根据name查,name为空根据id查,否则根据salary查

如果都为空,会执行最后一条语句

    <select id="findchoose" parameterType="map" resultType="Monster">
        select * from `monster`
        <choose>
            <when test="name != null and name != ''">
                where `name` = #{name}
            </when>
            <when test="id > 0">
                where `id` > #{id}
            </when>
            <otherwise>
                where `salary` > 100
            </otherwise>
        </choose>
    </select>

foreach

常用于对集合进行遍历(尤其是在构建IN条件语句的时候)

collection:指定数组或集合

item:代表数组或集合中的元素

separator:循环之间的分隔符

open:foreach循环拼接的所有sql语句的最前面以什么开始

close:foreach循环拼接的所有sql语句的最后面以什么开始

底层map存放的key是array或arg0,value是这个数组

map.put("array",数组)、map.put("arg0",数组)

不写注解:collection= array 或 arg0

写注解@Param("name"):collection = "name"

批量删除

int deleteByIds (@Param("ids") Long[] ids);
<select id="deleteByIds">
        delete from `monster`
        <if test="ids != null and ids != ''">
            <where>
                id IN
                <foreach collection="ids" item="idx" open="(" separator="," close=")">
                    #{idx}
                </foreach>
            </where>
        </if>
</select>

 批量插入

List<Car> cars = Arrays.asList(car1, car2, car3);
int insertBatch (@Param("cars") List<Car> cars);
<insert id="insertBatchByForeach">
  insert into t_car values 
  <foreach collection="cars" item="car" separator=",">
    (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
  </foreach>
</insert>

sql标签与include标签

sql标签用来声明sql片段

include标签用来将声明的sql片段包含到某个sql语句当中

作用:代码复用。易维护。

    <sql id="carCols">
        id,
        car_num carNum,
        brand,guide_price guidePrice,
        produce_time produceTime,
        car_type carType
    </sql>

    <select id="selectAllRetMap" resultType="map">
        select
            <include refid="carCols"/>
        from t_car
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值