• Mybatis的mapper文件中trim标签详解


    轉自》:https://blog.csdn.net/QQ727338622/article/details/84308020

    0、背景

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

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

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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>
    

     

  • 相关阅读:
    深入理解hadoop之MapReduce
    centos关机与重启命令
    hadoop学习笔记(1)
    配置ssh免密码登录设置后还是提示需要输入密码
    js获得URL中的参数
    SQLite介绍
    sql记录
    sql游标使用
    sql触发器
    sql函数
  • 原文地址:https://www.cnblogs.com/sharpest/p/13705549.html
Copyright © 2020-2023  润新知