mybatis框架xml下trim中的prefix与suffix等标签的用法
作者:徐林2333 时间:2023-09-20 18:55:24
trim中prefix与suffix等标签用法
1.prefix
前缀增加的内容
2.suffix
后缀增加的内容
3.prefixOverrides
前缀需要覆盖的内容,一般是第一个判断条件前面的多余的结构,如:第一个判断条件前面多了 ‘and'
select * from User where name='zhangsan' and age='20';
<select id='queryUser'>
select * from User
<trim prefix='where' prefixOverrides='and'>
<if test="name != null and name != ''">
name = #{name}
</if>
<if test="age !=null and age !=''">
and age = #{age}
</if>
</trim>
<select>
第一个条件前面没有任何符号,第二个条件要加上and,否则sql语句会报错。很理想的状态是第一个和第二个都有值,但是既然判断,说明也可能会没有值,当第一个name没有值的时候,这个时候sql语句就会是 select * from User where and age='',很明显这个sql语句语法存在问题。
在这里标签属性prefixOverrides就起作用了,它会让前缀where覆盖掉第一个and。覆盖之后的是:select * from User where age='';
4.suffixOverrides
后缀需要覆盖的内容,一般是最后一个数据的后面符号,如:set值的时候,最后一个值的后面多一个逗号‘,'
<insert id="insertSelective" parameterType="org.javaboy.vhr.model.Salary">
insert into salary
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="basicSalary != null">
basicSalary,
</if>
<if test="bonus != null">
bonus,
</if>
<if test="lunchSalary != null">
lunchSalary,
</if>
<if test="trafficSalary != null">
trafficSalary,
<if test="name != null">
name,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="basicSalary != null">
#{basicSalary,jdbcType=INTEGER},
</if>
<if test="bonus != null">
#{bonus,jdbcType=INTEGER},
</if>
<if test="lunchSalary != null">
#{lunchSalary,jdbcType=INTEGER},
</if>
<if test="trafficSalary != null">
#{trafficSalary,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
</trim>
</insert>
加了suffixOverrides=","
结果:
insert into salary (id,basicSalary,bonus,lunchSalary,trafficSalary,name) values (#{id},#{basicSalary},#{bonus},#{lunchSalary},#{trafficSalary},#{name})
不加suffixOverrides=","
结果:
insert into salary (id,basicSalary,bonus,lunchSalary,trafficSalary,name,) values (#{id},#{basicSalary},#{bonus},#{lunchSalary},#{trafficSalary},#{name},)
加了suffixOverrides=","的话,本例中最后一个条件中的逗号“,”会被后缀覆盖掉
mybatis之 trim prefix="(" suffix=")"
1.如下所示
<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>
prefix
:在trim标签内sql语句加上前缀。
suffix
:在trim标签内sql语句加上后缀。
suffixOverrides
:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。
prefixOverrides
:指定去除多余的前缀内容
2.下面是一个往购物车表中插入数据的mybatis语句
<insert id="insert" parameterType="com.tortuousroad.groupon.cart.entity.Cart">
insert into cart
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="userId != null">
user_id,
</if>
<if test="dealId != null">
deal_id,
</if>
<if test="dealSkuId != null">
deal_sku_id,
</if>
<if test="count != null">
count,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="userId != null">
#{userId,jdbcType=BIGINT},
</if>
<if test="dealId != null">
#{dealId,jdbcType=BIGINT},
</if>
<if test="dealSkuId != null">
#{dealSkuId,jdbcType=BIGINT},
</if>
<if test="count != null">
#{count,jdbcType=INTEGER},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
suffixOverrides=","
执行的sql语句也许是这样的:insert into cart (id,user_id,deal_id,) values(1,2,1,);显然是错误的
指定之后语句就会变成insert into cart (id,user_id,deal_id) values(1,2,1);这样就将“,”去掉了。
前缀也是一个道理这里就不说了。
来源:https://blog.csdn.net/weixin_49071539/article/details/116309637
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 可视化垃圾回收_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/1/88181_0s.png)
C# 动画窗体(AnimateWindow)的小例子
Java基于ShardingSphere实现分库分表的实例详解
![](https://img.aspxhome.com/file/2023/0/76270_0s.jpg)
Java中File文件操作类的基础用法
![](https://img.aspxhome.com/file/2023/2/78922_0s.png)
java中i = i++和i =++i的深入讲解
Android UI设计与开发之实现应用程序只启动一次引导界面
![](https://img.aspxhome.com/file/2023/2/91652_0s.jpg)
Android编程中EditText限制文字输入的方法
Java中clone方法使用笔记
![](https://img.aspxhome.com/file/2023/6/83146_0s.png)
使用IDEA搭建ssm框架的详细图文教程
![](https://img.aspxhome.com/file/2023/4/94744_0s.jpg)
spring boot mybatis枚举映射示例代码
System.currentTimeMillis()计算方式与时间的单位转换详解
基于springboot2集成jpa,创建dao的案例
LINQ基础之From和GroupBy子句
![](https://img.aspxhome.com/file/2023/9/83229_0s.png)
SpringBoot如何优雅的处理全局异常
![](https://img.aspxhome.com/file/2023/5/69355_0s.png)
Android中通过view方式获取当前Activity的屏幕截图实现方法
Android界面 NotificationManager使用Bitmap做图标
IDEA社区版下载安装流程详解(小白篇)
![](https://img.aspxhome.com/file/2023/4/61684_0s.png)
Android Studio中通过CMake使用NDK并编译自定义库和添加预编译库
![](https://img.aspxhome.com/file/2023/6/82386_0s.png)
C# WinForm制作登录界面的实现步骤
![](https://img.aspxhome.com/file/2023/7/68607_0s.png)
android studio与手机连接调试步骤详解
![](https://img.aspxhome.com/file/2023/1/139031_0s.png)