zoukankan      html  css  js  c++  java
  • 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>
    

     

  • 相关阅读:
    mysql 数据库【目录】
    Django 模板层
    Django文件下载(通过反向解析)
    Django 的路由系统
    Linux 搭建Django环境 + nginx + virtualenv虚拟环境
    layui 框架之秒传文件 (前端分段 MD5 型成秒传)
    Bootstrap 使用小点总结
    Django 之数据表操作
    前端之旅【目录】
    学习中遇到的小坑坑
  • 原文地址:https://www.cnblogs.com/sharpest/p/13705549.html
Copyright © 2011-2022 走看看