Mybatis值trim标签

        Mybatis具有实现动态SQL的能力,使用这个特性免不了会用到trim这个标签,trim标签的功能简单来说就是自定义格式拼凑SQL语句。
        trim有4个属性:

prefix:表示在trim包裹的SQL前添加指定内容
suffix:表示在trim包裹的SQL末尾添加指定内容
prefixOverrides:表示去掉(覆盖)trim包裹的SQL的指定首部内容
suffixOverrides:表示去掉(覆盖)trim包裹的SQL的指定尾部内容

来看例子:
查找某个用户,通过名字和年龄

public User getUser(@Param("LastName") String lastName,@Param("age") Integer age,@Param("phone") String phone);

对应xml,先看首先想到的:

<select id="getUser" result="user">
	select * from user_tab where last_name=#{lastName} and age=#{age} and phone=#{phone}
</select>

执行时输出的SQL语句
在这里插入图片描述

  1. prefix
<select id="getUser" resultType="user">
	select * from user_tab 
	<trim prefix="where">
		last_name=#{lastName} and age=#{age} and phone=#{phone}
	</trim>
</select>

输出同样SQL,这个就是prefix的效果
在这里插入图片描述

  1. prefixOverrides
    当然实际使用时查找条件都需要做一些判断,最基本的是否为null。
<select id="getUser" resultType="user">
	select * from user_tab 
	<trim prefix="where">
		<if test="lastName != null">
			last_name=#{lastName}
		</if>
		<if test="age != null">
			and age=#{age}
		</if>
		<if test="phone != null">
			and phone=#{phone}
		</if>
	</trim>
</select>

执行语句与上面一样,但是假如现在lastName参数为null,我们再看:

// 查询
User user = userMapper.getUser(null, 12,"120");

在这里插入图片描述
因为lastName为null,所以第一个if不成立里面的SQL语句不拼接,第二个if里面的and边紧跟在where后面了,语法错误,这是只要加上prefixOverride即可,把首个“and”去掉

<select id="getUser" resultType="user">
	select * from user_tab 
	<trim prefix="where" prefixOverrides="and">
		<if test="lastName != null">
			last_name=#{lastName}
		</if>
		<if test="age != null">
			and age=#{age}
		</if>
		<if test="phone != null">
			and phone=#{phone}
		</if>
	</trim>
</select>

在这里插入图片描述
如果是先要去掉and获取or,则必须这样写prefixOverrides=“and |or”,注意中间的空格。

  1. suffix、suffixOverrides
    上面查找,这里来看更新
// 根据id来更新
public void updateUser(@Param("user") User user);

对应的xml

<!--普通版-->
<update id="updateUser">
	update user_tab
	set last_name=#{lastName},age=#{age},phone=#{phone}
	where id=#{id}
</update>

使用suffix,并加上条件判断

<update id="updateUser">
	<trim suffix="where id=#{id}">
		update user_tab
		set
		<if test="lastName != null">
			last_name=#{lastName},
		</if>
		<if test="age != null">
			age=#{age},
		</if>
		<if test="phone != null">
			phone=#{phone}
		</if> 
	</trim>
</update>

执行输出跟上面的普通版语句一样没问题,可以看到suffix的效果
在这里插入图片描述
同样的假如phone为null了,

// 更新
userMapper.updateUser(new User(2, "jack", 20, null));

在这里插入图片描述
因为最会一个if条件不成立,所以倒数第二个if里面的逗号在where的左侧语法错误,此时使用suffixOverrides即可

<update id="updateUser">
	<trim suffix="where id=#{id}" suffixOverrides=",">
		update user_tab
		set
		<if test="lastName != null">
			last_name=#{lastName},
		</if>
		<if test="age != null">
			age=#{age},
		</if>
		<if test="phone != null">
			phone=#{phone}
		</if> 
	</trim>
</update>

再次执行输出,成功执行,这个就是suffixOverrides的作用
在这里插入图片描述

好了,这个就是trim标签即属性的基本介绍,具体怎么使用看实际情况,上面只是作为演示。

  • 13
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
mybatistrim标签是用来在SQL语句中去除不必要的空白字符的。该标签可以用于去除SQL语句开头和结尾的空白字符以及条件语句中的多余空白字符。trim标签有以下几个属性可以使用: 1. prefix:指定要在SQL语句开头去除的前缀字符。 2. suffix:指定要在SQL语句结尾去除的后缀字符。 3. prefixOverrides:指定要在条件语句中去除的前缀字符。 4. suffixOverrides:指定要在条件语句中去除的后缀字符。 trim标签可以在SQL语句中的任何位置使用,包括select、insert、update和delete语句。通过使用trim标签,我们可以简化SQL语句并提高代码的可读性。 例如,下面是一个使用trim标签的示例: ```xml <select id="getUserList" resultType="User"> SELECT * FROM user WHERE 1=1 <trim prefix="AND" prefixOverrides="OR"> <if test="name != null and name != ''"> OR name = #{name} </if> <if test="age != null"> OR age = #{age} </if> </trim> </select> ``` 在这个示例中,trim标签被用来去除WHERE子句中多余的OR字符,并且在满足条件时添加AND前缀。这样可以避免在生成的SQL语句中出现多余的OR和AND,使得SQL语句更加简洁和易读。<span class="em">1</span> #### 引用[.reference_title] - *1* [mybatis trim标签的使用详解](https://download.csdn.net/download/weixin_38601390/12724461)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值