Mybatis---动态SQL

IF

当传入的参数不完全你的时候,查询语句中会有默认值null,
当null成为查询条件的时候回造成查询不出结果的问题.

在<select>标签中加入<if>:
test属性用来判断字符串是否为空
<if test=” userEmail != null and userEmail !=””>
	 and user email = #{userEmail}
</if>
and相当于&&,or相当于||
sql语句中要注意两点:
1.where后面要加上1=1,避免在条件不符合的时候以where结尾
2.语句中的and要手动添加连接

update选择性更新的方法名后缀一般是selective

insert中添加<if>需要在insert和set后面相同的位置一起添加if语句

_parameter

	单个参数的时候_parameter默认为该参数
	多个参数的时候_parameter.get(0)获取第一个参数
	或者在传参的时候使用@param注解,例如
@param(''userName")String userName
用#{userName}获取userName的值

CHOOSE…WHEN…OTHERWISE

if标签无法实现逻辑语句中的if...else功能,这个时候需要使用choose...when...otherwise来实现.
	<choose> 
		 <when test=” id != null ” > 
		 	and id= #{id} 
		 </when>
		 <when test=” userName != null and userName !=””>
			 and user name = #{userName} 
		 </when>
		 <otherwise> 
			 and 1 = 2 
		 </otherwise> 
	</choose>
当前两个when条件都不成立的时候,会执行otherwise中的语句,where不成立,查询不出结果.

WHERE

如果该标签包含的元素中有返回值,就插入一个 where;如果 where 后面的字符串是以 AND 和 OR 开头的,就将它们剔除。

SET

如果该标签包含的元素中有返回值,就插入一个 set:如果 set 后面的 字符串是以逗号结尾的,就将这个逗号剔除。 

TRIM

作用:一般用于去除sql语句中多余的and关键字,逗号,或者给sql语句前拼接 “where“、“set“
以及“values(“ 等前缀,或者添加“)“等后缀,可用于选择性插入、更新、删除或者条件查询等操作

where 和 set 标签 的功能都可以用 trim 标签来实现,并且在底层就是通过 TrimSqlNode 实现的
trim的属性:
 - prefix:当 trim 元素内包含内容时,会给内容增加 prefix 指定的前缀。 		
 - prefixOverrides:当 trim元素内包含内容时,会把内容中匹配的前缀字符串去掉。  		 
 - suffix:当 trim 元素内包含内容时,会给内容增加 suffix 指定的后缀。  		 
 - suffixOverrides :当 trim 元素内包含内容时,会把内容中匹配的后缀字符串去掉。

FOREACH

foreach包含的属性:
-collection: 必填,值为要选代循环的属性名。这个属性值的情况有很多。
-item:变量名,值为从法代对象中取出的每一个值。
-index:索引的属性名,在集合数组情况下值为当前索引值, 
当选代循环的对象是 Map 类型时,这个值为 Map 的 key (键值)。 
-open:整个循环内容开头的字符串 。
-close: 整个循环内容结尾的字符串。 separator:每次循环的分隔符

foreach实现批量插入:

	<foreach collection="list" item="user" separator=",">
			(
			#{user.userName}, #{user.userPassword},#{user.userEmail},
			#{user.userInfo}, #{user.headImg, jdbcType=BLOB}, #{user.createTime, jdbcType=TIMESTAMP})
	</foreach>
mysql数据库批量插入返回自增主键
	<insert id="insertList" useGeneratedKeys="true" keyProperty="id">
调用mapper中的insertList接口之后,可以对所传userList直接遍历获取主键
for(SysUser user : userList){
	System.out.println(user.getId());
}

BIND

concat在不同的数据库中存在语法差异,mysql中可以串联多个参数,oracle中只支持2个参数,如果更换数据库
则需要改变sql代码,所以这个地方需要用<bind>标签来对参数进行连接.
 <if test="@tk.mybatis.util.StringUtil@isNotEmpty(userName)">
	and user_name like concat('%', #{userName}, '%')
</if>
使用<bind>标签:
<if test=" userName!= null and userName !=''"> 
	<bind name="userNarneLike " value="'%' + userName + '%'"/>
	and user name like #{userNarneLike}
</if>
使用 bind 拼接字符串不仅可以避免因更换数据库而修改 SQL,也能预防 SQL 注入

多数据库支持
mybatis-config.xml中配置

<databaseidProvider type=” DB_VENDOR” /> 
<if test=" userName != null and userName != ''">
	 <if test="_databaseid == 'mysql'"> 
 		and user_name like concat ('%', #{userName},'%' )
 	 </if> 
 	 <if test="_databaseid == 'oracle'"> 
 	 	and user_name like '%'||#{userName}||'%' 
 	 </if>
 </if> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值