zoukankan      html  css  js  c++  java
  • mybatis的<trim>标签

      mybatis中<trim>用于格式化sql语句,可以帮助完成select,update,insert,delete语句的格式化操作,trim元素的主要功能(主要内部标签):参见下图

     prefix:包含的内容的前缀;

    suffix:与prefix对应,包含内容的后缀

    prefixOverrides:可以设定第一个出现的某个字符或者内容被忽略

    sufixOverrides:设定最后的某个字符或者内容被忽略

    “INSERT”:

    insert into essay
            <trim prefix="(" suffix=")" suffixOverrides=","> 
                <if test="time!= null">
                    time,
                </if>
                <if test="authorname!= null">
                    authorname,
                </if>
                <if test="content!= null">
                    content,
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="time != null">
                    #{time,jdbcType=TIMESTAMP},
                </if>
                <if test="authorname != null">
                    #{authorname,jdbcType=VARCHAR},
                </if>
                <if test="content != null">
                    #{content,jdbcType=VARCHAR},
                </if>
            </trim>

    “SELECT”:

    select t.username,t.age,t.gender
    from user
    <trim prefix="where" prefixoverride="and | or">
      <if test="name!=null" >and t.name=#{name}</if>
      <if test="institution_id!=null">and t.institution_id=#{insid}</if>
      <if test="gender!=null">and t.gender=#{gender}</if>
    </trim>

    "UPDATE":

    这个suffix可能有点多余,可以直接接在后面

    update user

    <trim prefix="set" suffixoverride="," sufix="where id=#{id}">
      <if test=name!=null">name=#{name},</if>
      <if test="gender!=null">gender="#{gender}",</if>
    </trim>

    “DELETE”

    这样的delete不建议写,就算这样写也需要后端传过来的值有能够确定的参数,否则最后拼接出来的sql可能起到意想不到的效果。。

    delete from user
    <trim prefix="where" prefixoverride="and | or">
      <if test="name!=null">and name=#{name}</if>
      <if test="age!=null">and age=#{age}</if>
      <if test="email!=null">and email=#{email}</if>
    </trim>
  • 相关阅读:
    Linux(Contos7.5)环境搭建之JDK1.8安装(二)
    python微信域名或者链接批量检测
    表单设计器3-查询面板
    表单设计器2-表格布局
    表单设计器1-基本操作
    信息无障碍国内标准
    信息无障碍国际标准
    信息无障碍服务对象
    信息无障碍定义
    E8.Net工作流开发问题
  • 原文地址:https://www.cnblogs.com/notably/p/11765128.html
Copyright © 2011-2022 走看看