mybatis中的动态SQL

动态SQL的基本元素:
if:单条件分支判断
choose,when,otherwise:多条件分支判断
trim,set,where:用于处理SQL拼装问题
foreach:循环语句
bind:定义一个上下文变量
test:用于判断条件是否成立

if条件判断语句:当角色名称不为空时,根据角色名称查找对象

<resultMap id="roleResultMap" type="com.ssm.chapter6.pojo.Role">
	<id column="roleNo" property="role_no" />
	<id column="role_name" property="roleName" />
	<id column="note" property="note" />
</resultMap>
<select id="findRole1" parameterType="string" resultMap="roleResultMap">
	select role_no, role_name, note from t_role where 1=1
	<if test="roleName != null and roleName !=''">
		and role_name like concat('%', #{roleName}, '%')
	</if>
</select>

choose,when,otherwise多条件判断语句:相当于if-else-else if

<select id="findRole2" parameterType="role" resultMap="roleResultMap">
	select role_no, role_name, note from t_role
	where 1=1
	<choose>
		<when test="roleNo != null and roleNo !=''">
			AND role_no = #{roleNo}
		</when>
		<when test="roleName != null and roleName !=''">
			AND role_name like concat('%', #{roleName}, '%')
		</when>
		<otherwise>
			AND note is not null
		</otherwise>
	</choose>
</select>

where语句和if语句拼接(当条件成立是,where后面直接跟and不报错的原因,可以参考)
https://blog.csdn.net/qq_37745636/article/details/99086313

<select id="findRole3" parameterType="role" resultMap="roleResultMap">
	select role_no, role_name, note from t_role
	<where>
		<if test="roleName != null and roleName !=''">
			and role_name like concat('%', #{roleName}, '%')
		</if>
		<if test="note != null and note !=''">
			and note like concat('%', #{note}, '%')
		</if>
	</where>
</select>

当where元素里面的条件成立时,才会加入where这个SQL关键字到组装的SQL里面。

使用trim删掉特殊的元素,比如and,or

<select id="findRole4" parameterType="string" resultMap="roleResultMap">
	select role_no, role_name, note from t_role
	<trim prefix="where" prefixOverrides="and">
		<if test="roleName != null and roleName !=''">
			and role_name like concat('%', #{roleName}, '%')
		</if>
	</trim>
</select>

trim元素意味着要去掉一些特殊的字符,当时prefix代表的是if语句的前缀,当if语句成立时,才会加到语句的前面,而prefixOverrides代表的是当if语句成立时,去掉if语句中的and字符,suffixOverrides是去掉后缀,比如逗号。

使用set更新角色的数据,这样我们就可以选择更新的字段,而不用每个字段都更新,而且set语句在遇到逗号时,会自动把对应的逗号去掉。

<update id="updateRole" parameterType="role">
	update t_role
	<set>
		<if test="roleName != null and roleName !=''">
			role_name = #{roleName},
		</if>
		<if test="note != null and note != ''">
			note = #{note}
		</if>
	</set>
	where role_no = #{roleNo}
</update>

使用foreach遍历集合,它能够很好的支持数组和list,set几口的集合,它往往用于SQL中的in关键字。

<select id="findRoleByNums" resultMap="roleResultMap">
	select role_no, role_name, note from t_role where role_no in
	<foreach item="roleNo" index="index" collection="roleNoList"
		open="(" separator="," close=")">
		#{roleNo}
	</foreach>
</select>

解释一下,collection配置的roleNoList是传递进来的参数名称,它可以是一个数组,List,Set,等集合。
item配置的是循环中的当前的元素。
index配置的是当前元素在集合的位置下标。
open和close配置的是以什么符号将这些集合元素包装起来。
separator是各个元素的间隔符。

使用bind元素自定义一个上下文变量,方便使用,在进行模糊查询的时候,如果是MYSQL数据库,常常用到的是一个concat,它用’%'和参数相连,而在Oracle数据库中用||,这样SQL就需要提供两种形式去实现,但是有了bind元素,就不必使用数据库的语言,而是使用MyBatis的动态SQL即可完成。

<select id="findRole5" parameterType="string" resultMap="roleResultMap">
	<bind name="pattern" value="'%' + _parameter + '%'" />
	SELECT role_no, role_name, note FROM t_role
	where role_name like #{pattern}
</select>

这里的_parameter代表的是传递进来的参数,它和通配符一起组成了pattern,然后就可以在select语句中使用这个变量进行模糊查询了。无论是MYSQL还是Oracle都可以使用这样的语句,提高了代码的移植性。

使用bind传递多个参数
首先定义接口方法

public List<RoleBean> findRole(@Param("roleName") String roleName, @Param("note") String note);

定义映射文件

<select id="findRole6" resultMap="roleResultMap">
	<bind name="pattern_roleName" value="'%' + roleName + '%'" />
	<bind name="pattern_note" value="'%' + note + '%'" />
	SELECT role_no, role_name, note FROM t_role
	where role_name like
	#{pattern_roleName}
	and note like #{pattern_note}
</select>

使用多个条件进行模糊查询。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值