MyBatis的动态SQL

MyBatis的动态SQL主要有if、choose(when、otherwise)、trim(where、set)、foreach.

1、if的用法

<select id="findRoles" parameterType="string" resultType="roleResultMap">
	select role_num,role_name,role_note from role_t where 1=1
	<if test="roleName!=null and roleName!=''">
		and role_name like concat('%',#{roleName},'%')
	</if>
</select>

将参数roleName传递到映射器,采取构造对roleName进行模糊查询,如果这个参数为空,就不去构造这个条件。

2、choose、when、otherwise

if语句是一种非此即彼的关系,有时候我们不需要非此即彼的选择,可能需要第三种甚至更多的选择,需要使用choose、when、otherwise

<!--choose、when、otherwise的用法-->
<select id="findRoles" parameterType="string" resultType="roleResultMap">
	select role_num,role_name,role_note from role_t where 1=1
	<choose>
		<when test="roleNum!=null and roleNum!=''">
			and role_num=#{roleNum}
		</when>
		<when test="roleName!=null and roleName!=''">
			and role_name like concat('%',#{roleName},'%')
		</when>
		<otherwise>
			and role_note is not null
		</otherwise>
</select>

3、trim、where、set元素

where元素的用法:当where里面的条件成立的时候,才会加入where这个SQL关键字到组装的SQL里面,否则就不加入,例如上面"1=1",如果没有这个就是错误的SQL语句,可以改写为:

<!--where的用法-->
<select id="findRoles" parameterType="string" resultType="roleResultMap">
	select role_num,role_name,role_note from role_t 
	<!--条件满足时,where子句才会被加入-->
	<where>
		<if test="roleName!=null and roleName!=''">
			and role_name like concat('%',#{roleName},'%')
		</if>
	</where>
</select>

如果想要去掉一些特殊的SQL语法,比如常见的and、or,使用trim元素可以达到目的;

<!--trim的用法-->
<select id="findRoles" parameterType="string" resultType="roleResultMap">
	select role_num,role_name,role_note from role_t 
	<!--去掉一些特殊的字符串,prefix代表语句的前缀,-->
	<!--prefixOverrides代表需要去掉的那种字符串-->
	<trim prefix="where" prefixOverrides="and">
		<if test="roleName!=null and roleName!=''">
			and role_name like concat('%',#{roleName},'%')
		</if>
	</trim>
</select>

在update中,如果只想更新特定的字段,可以使用set元素来操作;

<!--set元素的操作-->
<update id="updateRole" parameterType="role">
	update role_t
	<set>
		<if test="roleName!=null and roleName!=''">
			role_name=#{roleName}
		</if>
		<if test="note!=null and note!=''">
			note=#{note}
		</if>
	</set>
</update>

4、foreach循环

foreach是一个循环语句,他的作用是遍历集合。可以支持对数组、List、Set的遍历

<!--foreach的使用-->
<select id="findUserBySex" resultType="user">
	select * from user_t where sex in
	<foreach collection="sexList" item="sex" index="index" open="(" seperator="," close=")">
		#{sex}
	</foreach>
</select>
  • collection:传进来的集合参数的名称
  • item:循环中的当前元素
  • index:当前元素在集合中的位置下标
  • open、close:用什么符号将这些元素包装起来
  • separator:各元素之间的间隔符

5、test属性

test属性用于条件判断语句中,作用是判断真假,在if语句中使用广泛

<select id="findRoles" parameterType="string" resultType="roleResultMap">
	select role_num,role_name,role_note from role_t 
	<!--条件满足时,where子句才会被加入-->
	<where>
		<!--使用test判断-->
		<if test="roleNum!=null and roleNum!='1234'">
			and role_name like concat('%',#{roleName},'%')
		</if>
	</where>
</select>

6、bind元素

bind的作用是通过OGNL表达式定义一个上下文变量,方便下文使用,可以在MySQL和Oracle数据库之间通用,提高了可移植性;
定义接口为:
public List<RoleBean> findRole(@Param("roleName")String roleName, @Param("note")String note);
然后定义映射文件:

<!--bind元素的使用-->
<select id="findRole" resultType="com.hhh.demo.model.RoleBean">
	<bind name="pattern_roleName" value="'%'+roleName+'%'"/>
	<bind name="pattern_note" value="'%'+note+'%'"/>
	select id,
	role_name as roleName,
	create_date as createDate,
	end_date as endDate,
	end_flag as endFalg,
	note
	from role_t where
	role_name like #{pattern_roleName}
	and note like #{pattern_note}
</select>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值