Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in /www/wwwroot/blog.somekey.cn/usr/themes/Joe/public/tencent_protect.php on line 40

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/wwwroot/blog.somekey.cn/usr/themes/Joe/public/tencent_protect.php on line 40
Mybatis的mapper文件中trim标签详解 - 桃子🍑关键词

Mybatis的mapper文件中trim标签详解

admin
2022-09-28 / 0 评论 / 125 阅读 / 正在检测是否收录...

0、背景

parameterType参数类型student是别名,里面的字段有id,name,age,sex被封装成bean对象,跟数据库中student表中字段一一对应,以下案例只为一个SQL语句。(初入SSM坑,请多多指教)

update student set name='aa',age=20,sex='男' where id=1;

图片.png

1、prefix属性:在trim开始部分添加内容

例,在trim前面加上set

<update id="updateStudent2" parameterType="student">
    update student 
    <trim prefix="set">
        <if test="name!=null and name!=''">name=#{name},</if>
        <if test="age!=null and age!=''">age=#{age},</if>
        <if test="sex!=null and age!=''">sex=#{sex}</if>
    </trim>
    <where>id=#{id}</where>
</update>

2、suffix属性:在trim结束部分添加内容

例,在后面添加上where内容

<update id="updateStudent2" parameterType="student">
    update student set
    <trim suffix="where id=#{id}">
        <if test="name!=null and name!=''">name=#{name},</if>
        <if test="age!=null and age!=''">age=#{age},</if>
        <if test="sex!=null and age!=''">sex=#{sex}</if>
    </trim>
</update>

3、prefixOverrides属性:去除trim开始部分的内容

例,删掉name前面的set

<update id="updateStudent2" parameterType="student">
    update student set
    <trim prefixOverrides="set" >
        <if test="name!=null and name!=''">set name=#{name},</if>
        <if test="age!=null and age!=''">age=#{age},</if>
        <if test="sex!=null and age!=''">sex=#{sex}</if>
    </trim>
     <where>id=#{id}</where>
</update>

4、suffixOverrides属性:去除trim结束部分的内容

例,删掉最后一个逗号

<update id="updateStudent2" parameterType="student">
    update student set
    <trim suffixOverrides=",">
        <if test="name!=null and name!=''">name=#{name},</if>
        <if test="age!=null and age!=''">age=#{age},</if>
        <if test="sex!=null and age!=''">sex=#{sex},</if>
    </trim>
     <where>id=#{id}</where>
</update>
0

评论

博主关闭了所有页面的评论