作者:death05的博客
推荐:路在脚下
trim元素的主要功能是可以在自己包含的内容钱加上某些前缀,也可以在其后加上某写后缀,与之对应的属性是prefix和suffix;
可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides。以下举例:
1、代码为:
    select * from user
  <trim prefix="WHERE" prefixoverride="AND |OR">
    <if test="name != null and name.length()>0"> AND name=#
{name}</if>
    <if test="gender != null and gender.length()>0"> AND gender=#
{gender}</if>
  </trim>
假如说name和gender的值都不为null的话,打印的SQL为:
select * from user where name = 'xx' and gender = 'xx'

where后不存在and,这是因为prefixoverride="AND |OR"代表去掉第一个and或者是or。

2、代码为:
    update user
  <trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
    <if test="name != null and name.length()>0"> name=#{name} ,
</if>
    <if test="gender != null and gender.length()>0"> AND gender=#
{gender} , </if>
  </trim>
假如说name和gender的值都不为null的话,打印的SQL为:
update user set name='xx' , gender='xx' where id='x'

可以参考第一个例子理解。

3、代码为:
<insert id="save" parameterType="NoticeEntity">
INSERT INTO S_NOTICE
<trim prefix="(" suffix=")" suffixOverrides=",">
ID,
<if test="title != null">TITLE,</if>
<if test="content != null">CONTENT,</if>
<if test="noticeStatus != null">NOTICE_STATUS,</if>
<if test="createdBy != null">CREATED_BY,</if>
CREATED_TS,
<if test="lastUpdBy != null">LAST_UPD_BY,</if>
LAST_UPD_TS,
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
SYS_GUID(),
<if test="title != null">#{title,jdbcType=VARCHAR},</if>
<if test="content != null">#{content,jdbcType=VARCHAR},</if>
<if test="noticeStatus != null">#{noticeStatus,jdbcType=VARCHAR},</if>
<if test="createdBy != null">#{createdBy,jdbcType=VARCHAR},</if>
systimestamp,
<if test="lastUpdBy != null">#{lastUpdBy,jdbcType=VARCHAR},</if>
systimestamp,
</trim>
</insert>

大家可以自行理解一下。

最新文章

  1. context上下文 php版解释
  2. spark优化
  3. 【应用笔记】【AN004】VB环境下基于RS-485的4-20mA电流采集
  4. (转)【Android测试工具】03. ApkTool在Mac上的安装和使用(2.0版本)
  5. 1.单一职责原则(Single Responsibility Principle)
  6. Android 换肤功能的实现(Apk插件方式)
  7. js template
  8. 让jquery easyui datagrid列支持绑定嵌套对象
  9. Apache2 MPM 模式了解
  10. Apache Mina入门实例
  11. JVM垃圾回收机制概述
  12. 移动端h5 实现多个音频播放
  13. 11、Grafana 5.0 新功能特性(译文)
  14. Maven配置国内镜像仓库
  15. HDU 2089 不要62 【数位dp】
  16. Beta阶段第五次冲刺
  17. CF1096D Easy Problem(DP)
  18. Python3.5爬取cbooo.cn数据并且同步到mysql中
  19. GTK 预置对话框 GtkDialog 文件/颜色/字体选取等 GtkFileSelection
  20. std::async

热门文章

  1. jdk1.8 -- Collectors 的使用
  2. Oracle参数文件修改
  3. 使用pycharm开发web——django2.1.5(一)入坑尝试第一步,基本搭建
  4. .net操作数据库
  5. windows scala helloworld例子详解
  6. kafka安装、相关命令以及PHP使用
  7. ts转js 并压缩
  8. configure,make和make install关系
  9. SQL将多行数据合并成一行【转】
  10. Shiro——入门Demo