MyBatis 动态元素 trim元素

< trim> 标签的作用:
  我感觉trim元素是个很灵活的元素,能将要执行的sql语句修剪得整整齐齐哈哈哈。
  trim元素的作用是添加字符串以及剪除字符串,一般可以把它当成where元素或者set元素来用,另外,只有它体内有sql语句时才会执行。
  trim有四个属性建立了它灵活的基础:
    prefix:前缀,当trim标签中的sql语句被处理完毕后,准备与原sql拼接时,会在trim的sql语句前加上前缀所设置的属性值。
    是trim里的第一句话。
    prefixOverrides:前覆盖,如果trim标签下的sql语句开头的字符串和它的属性值相同的话,它会剪除掉他,后面还有相同的则不会管。
    suffix:后缀,当trim标签中的sql语句被处理完毕后,准备与原sql拼接时,会在trim的sql语句的最后面加上后缀所设置的属性值。
    suffixOverrides:后覆盖,如果trim标签下的sql语句结尾的字符串和它的属性值相同的话,它会剪除掉他,前面相同的则不会管。
 
  
   
   
实践一下:
以下下所有的sql语句其实都写在CM1.xml中……

1、把它当where使用时:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- namespace表示命名空间 -->
<mapper namespace="com.itheima.mapper.CM1">
	<!-- parameterType 传入的参数类型 -->
	<!-- resultType 接收的参数类型 -->	
	<select id="T" parameterType="com.itheima.po.Account"
		resultType="com.itheima.po.Account">
		select * from account
		<trim prefix="where" prefixOverrides="and">
			<if test="username != null and username != '' ">
				and username like concat('%',#{username},'%')
			</if>
			<if test="balance != null and balance > 0">
				and balance > #{balance}
			</if>
		</trim>
</mapper>

来试着拼接下

基础的SQL语句:

select * from account

当trim标签下的if标签的条件都为真时:

处理trim标签中的sql语句

1、prefixOverrides="and"所以删除最前面的and
2、and username like concat(’%’,#{username},’%’)加上and balance > #{balance}
3、加上前缀“where”
4、最后拼接原sql

select * from account where username like concat('%',#{username},'%') and balance > #{balance}

当trim标签下的if标签的条件都为假时:

不做任何操作……因为体内没有内容,所以不会加前缀什么的。

select * from account 

看看当set操作怎么样
 
  
   
2、把它当set使用时:

<update id="T2" parameterType="com.itheima.po.Account">
		update account
		<trim prefix="set" suffixOverrides="," suffix="where id=2">
			<if test="username != null and username != '' ">
				username=#{username},
			</if>
			<if test="balance != null and balance >= 0 ">
				balance=#{balance},
			</if>
		</trim>

	</update>

来试着拼接下

基础的SQL语句:

update account

当trim标签下的if标签的条件都为真时:

处理trim标签中的sql语句

1、suffixOverrides=",“所以删除最后的”,"
2、username=#{username},balance=#{balance}
3、加上前缀“set”
4、加上后缀"where id=2"
4、最后拼接原sql

update account set username=#{username},balance=#{balance} where id=2

当trim标签下只有第一个if标签的条件为真时:

1、suffixOverrides=",“所以删除最后的”,"
2、username=#{username}
3、加上前缀“set”
4、加上后缀"where id=2"
4、最后拼接原sql

update account set username=#{username} where id=2

是不是很简单。

3、我们在来灵活的测试下他的四个属性
T3

<select id="T3" parameterType="com.itheima.po.Account"
		resultType="com.itheima.po.Account">
		select * from account
		<trim prefix="where 1=1 and 4=4" prefixOverrides="and 2=2">
			and 2=2 and 3=3 and 2=2
		</trim>

	</select>

拼接后的sql如下

select * from account where 1=1 and 4=4 and 3=3 and 2=2

T4

<select id="T4" parameterType="com.itheima.po.Account"
		resultType="com.itheima.po.Account">
		select * from account
		<trim prefix="where 1=1" suffixOverrides="and 2=2">
			and 2=2 and 3=3 and 2=2
		</trim>

	</select>

拼接后的sql如下

select * from account where 1=1 and 2=2 and 3=3

T5

<select id="T5" parameterType="com.itheima.po.Account"
		resultType="com.itheima.po.Account">
		select   
		<trim prefix=" username,balance from account" suffix="and balance>0" prefixOverrides="hhh"  suffixOverrides="34" >
			 hhh where  id=234 
		</trim>

	</select>

拼接后的sql如下

select username,balance from account where  id=2 and balance>0

嘿,这只是简单的测试,完全还能再拓展不是么。哈,我是拓展不了了,因为能力有限,trim的实践就到这里,再见!

MyBatis动态SQL中的一些元素 if、set、trim、choose、foreach、bind

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值