小白二十篇 MyBatis动态SQL常用元素

MyBatis动态SQL

MyBatis提供对SQL语句进行动态组装的能力,使用XML的简单元素便能完成动态SQL的功能。

元素介绍
元素名称元素描述
if判断语句,属于当条件分支判断
choose-when-otherwise类似于switch语法,是多条件分支判断
foreach循环语句,常用于in语句的列举条件中
test用于判断真假,常用于判断中
where相当于SQL中的where,但是可以加入条件判断是否和SQL语句组装
trim用于去掉SQL中的一些特殊字符串
set用于update中,与if何用达到可以对信息摘取更新的作用
元素使用的例子

数据表设计为:t_user(id,name,comment)

  1. if元素
    要求:
    根据名字name查找指定用户数据,其中name是一个选填条件,当没有填写时不作为查询条件。
<select id="findUser" parameterType="string" resultMap="userMap">
		select id,name,comment
		from t_user
		where 1 = 1
		<!-- 此处的 1=1 仅仅是当条件不满足时保证SQL语句不报错的作用 -->
		<if test="name != null and name != ' '">
			and name like concat('%',#{name },'%')
		</if>
</select>
  1. choose-when-otherwise元素
    要求:
    根据id,name,comment三个属性查找
    如果id不为空,只用id查找
    如果id为空name不为空,只用name查找
    如果id,name为空则要求comment不为空
<select id="findUser" parameterType="user" resultMap="userMap">
		select id,name,comment
		from t_user
		where 1=1
		<!-- 此处的 1=1 仅仅是当条件不满足时保证SQL语句不报错的作用 -->
		<choose>
			<when test="id != null and id !=' '">
				and id = #{id}
			</when>
			<when test="name != null and name !=' '">
				and name like concat('%',#{name},'%')
			</when>
			<otherwise>
				and note is not null
			</otherwise>
		</choose>
</select>
  1. where元素
    这个元素只是替代了where语句而已,只有当where元素里面的条件成立时,才将where关键字组装到SQL语句里面去。
    要求:
    根据name和comment进行模糊查询
<select id="findUser" parameterType="user" resultMap="userMap">
		select id,name,comment
		from t_user
		<where>
			<if test="name != null and name != ' '">
				and name like concat('%',#{name },'%')
			</if>
			<if test="comment != null and comment != ' '">
				and comment like concat('%',#{comment },'%')
			</if>
		</where>
</select>
  1. trim元素
    去除掉特殊的SQL语法就可以使用这个元素,比如去除and,or等等。
    prefix : 表示语句的前缀
    prefixOverrides : 表示需要去掉的字符串
<select id="findUser" parameterType="string" resultMap="userMap">
		select id,name,comment
		from t_user
		<trim prefix="where" prefixOverrides="and">
		<if test="name != null and name != ' '">
			and name like concat('%',#{name },'%')
		</if>
</select>
  1. set元素
    要求:
    根据id对指定用户的部分信息或全部信息进行更新
<update id="updateUser" parameterType="user">
		update t_user
		<set>
			<if test="name != null and name != ' '">
				name = #{name},
			</if>
			<if test="comment != null and comment != ' '">
				comment = #{comment}
			</if>
		</set>
		where id = #{id }
</update>
  1. foreach元素
    foreach支持数组,List,Set接口的集合并提供遍历。
    要求:
    对一个List<Integer>的用户id集合idList通过foreach元素找到这个集合中每个id对应的用户信息
    collection : idList是传递进来的参数名
    item :循环中当前元素
    index :当前元素在集合中的下标
    open/close :用什么符号将集合元素包装起来
    separator :各个元素的间隔符
<select id="findUserBySex" resultMap="user">
		select *
		from t_user
		where id in
			<foreach item="id" index="index" collection="idList"
				open="(" separator="," close=")">
				#{id}
			</foreach>
</select>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值