Mybatis中trim的使用

目录

trim属性

select查询

insert新增

update修改


trim属性

属性描述
prefix使用trim语句块部分拼接前缀
suffix使用trim语句块部分拼接后缀
prefixOverrides去除拼接sql的第一个关键字或字符 比如where后第一个and
suffixOverrides去除拼接sql的最后一个关键字或字符 比如插入语句最后一个,

select查询

<select id="testTrimPage" resultType="com.chensir.system.domain.vo.UserVo">
        select *
        from user
        <trim prefix="where" prefixOverrides="and|AND">
            is_del = 0
            <if test="query.name != null and query.name != ''">
                and name like concat('%', #{query.name},'%')
            </if>
            <if test="query.age != null">
                and age = #{query.age}
            </if>
            <if test="query.sex != null and query.sex != ''">
                and sex = #{query.sex}
            </if>
            <if test="query.beginCreateTime != null and query.beginCreateTime != ''">
                and create_time >= #{query.beginCreateTime}
            </if>
            <if test="query.endCreateTime != null and query.endCreateTime != ''">
                and create_time &lt;= #{query.endCreateTime}
            </if>
        </trim>
    </select>

insert新增

<insert id="testTrimAdd">
        insert into user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="query.name != null and query.name !=''">name,</if>
            <if test="query.age != null">age,</if>
            <if test="query.sex != null and query.sex !=''">sex,</if>
            <if test="query.sort != null and query.sort !=''">sort,</if>
            <if test="query.createName != null and query.createName !=''">create_name,</if>
            <if test="query.createTime != null">create_time,</if>
            <if test="query.updateName != null and query.updateName !=''">update_name,</if>
            <if test="query.updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="query.name != null and query.name !=''">#{query.name},</if>
            <if test="query.age != null">#{query.age},</if>
            <if test="query.sex != null and query.sex !=''">#{query.sex},</if>
            <if test="query.sort != null and query.sort !=''">#{query.sort},</if>
            <if test="query.createName != null and query.createName !=''">#{query.createName},</if>
            <if test="query.createTime != null">#{query.createTime},</if>
            <if test="query.updateName != null and query.updateName !=''">#{query.updateName},</if>
            <if test="query.updateTime != null">#{query.updateTime},</if>
        </trim>
    </insert>

update修改

    
   <!-- =================== where写在最后 =================== -->

   <update id="testTrimPut">
        update user
        <trim prefix="SET" suffixOverrides=",">
            <if test="query.name != null and query.name !=''">name = #{query.name},</if>
            <if test="query.age != null">age = #{query.age},</if>
            <if test="query.sex != null and query.sex !=''">sex = #{query.sex},</if>
            <if test="query.sort != null and query.sort !=''">sort = #{query.sort},</if>
            <if test="query.updateName != null and query.updateName !=''">update_name = #{query.updateName},</if>
            <if test="query.updateTime != null">update_time = #{query.updateTime},</if>
        </trim>
        where id=#{query.id}
    </update>

    <!-- =================== where使用suffix =================== -->

    <update id="testTrimPut">
        update user
        <trim prefix="SET" suffixOverrides="," suffix="where id = #{query.id}">
            <if test="query.name != null and query.name !=''">name = #{query.name},</if>
            <if test="query.age != null">age = #{query.age},</if>
            <if test="query.sex != null and query.sex !=''">sex = #{query.sex},</if>
            <if test="query.sort != null and query.sort !=''">sort = #{query.sort},</if>
            <if test="query.updateName != null and query.updateName !=''">update_name = #{query.updateName},</if>
            <if test="query.updateTime != null">update_time = #{query.updateTime},</if>
        </trim>
    </update>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值