MyBatis-动态SQL

MyBatis的动态SQL是其强大特性之一,能让开发者摆脱拼接SQL语句的痛苦。本文详细介绍了MyBatis动态SQL的多种语句,包括if、choose - when - otherwise、where、set、trim、foreach、bind以及sql片段,阐述了各语句的作用和使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态SQL语句

MyBatis的强大特性之一便是它的动态SQL。如果你有使用JDBC或其它类似框架的经验,你就能体会到根据不同条件拼接SQL语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态SQL这一特性可以彻底摆脱这种痛苦。

1.if语句

动态 SQL 通常要做的事情是根据条件包含 where 子句的一部分。比如:

<select id="queryUser" resultMap="baseMap"
 	 resultType="com.sxt.bean.User" parameterType="user"> 
	select id ,name ,age  from t_user 
	where 1 =1 
	<if test="username!=null">
	     and name = #{username}
	</if>  
</select> 

测试1:
在这里插入图片描述

测试2:
在这里插入图片描述

2.choose,when,otherwise

有时我们不想应用到所有的条件语句,而只想从中择其一项。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

3.where语句

在使用if语句做动态条件处理的时候如果所有条件都不满足,那么得到的SQL语句如下:

select * from t_user where 

在这种情况下,我们一般会加一个1=1来匹配语法规则

	<select id="queryUser" resultMap="baseMap"
	 	 resultType="com.sxt.bean.User" parameterType="user"> 
		select id ,name ,age  from t_user 
		where 1 =1  
		<if test="username!=null">
		     and name = #{username}
		</if>  
	</select> 

此时可以使用标签来处理这种情况

	<select id="queryUser" resultMap="baseMap"
	 	 resultType="com.sxt.bean.User" parameterType="user"> 
		select id ,name ,age  from t_user 
		<where>
			<if test="username!=null">
			     and name = #{username}
			</if>  
		</where>
	</select> 

在这里插入图片描述
 ------------------------------------------------------------------------------------------------------------------------在这里插入图片描述
在这里插入图片描述

4.set语句

set主要也是用来解决更新问题的。

<update id="updateBookById">
	update t_book
	<set>
			<if test="author!=null"> author=#{author},</if>
			<if test="name!=null"> b_name=#{name},</if>
			<if test="price!=null"> price=#{price},</if>
	</set>
	where id=#{id};
</update>

5.trim

trim标记是一个格式化的标记,可以完成set或者是where标记的功能
在这里插入图片描述
代替where的用法:

	<select id="queryUser" resultMap="baseMap" resultType="com.sxt.bean.User"
		parameterType="user">
		select id ,name ,age from t_user
		<!-- <where>
			<if test="username!=null">
				and name = #{username}
			</if>
		</where> -->
		<trim prefix="where" prefixOverrides="AND |OR ">
			<if test="username!=null">
				 and name = #{username}
			</if>
			<if test="age != 0">
				and age = #{age}
			</if>
		</trim>
	</select>

代替 set的用法:

<update id="updateUser" parameterType="User">
	update t_user
	<trim prefix="set" suffixOverrides=",">
		<if test="username!=null">
			name = #{username},
		</if>
		<if test="age != 0">
			age = #{age}
		</if>
	</trim>
	where id=#{id}
</update>

或者:

<update id="updateUser" parameterType="User">
	update t_user
	set 
	<trim  suffixOverrides=",">
		<if test="username!=null">
			name = #{username},
		</if>
		<if test="age != 0">
			age = #{age}
		</if>
	</trim>
	where id=#{id}
</update>

在这里插入图片描述

6.foreach语句

foreach用来遍历,遍历的对象可以是数组,也可以是集合。
在这里插入图片描述
接口中的方法:

public interface UserMapper {
	// 如果不指定@Param 默认是array
	public List<User> queryUserByIds(@Param("ids")List<Integer> ids);

	public int insertUser(@Param("users")List<User> users);
}

<select id="queryUserByIds" resultType="user">
	select * from t_user where id in 
	<foreach collection="ids" open="(" close=")" separator="," item="id" >
		#{id}
	</foreach>
</select>

<insert id="insertUser">
	insert into t_user(name,age)values
	<foreach collection="users" item="user" separator=",">
		(#{user.name},#{user.age})
	</foreach>
</insert>

在这里插入图片描述

7.bind

bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。

<select id="getUserById" resultMap="baseMap" resultType="com.sxt.bean.User">
	<!-- 声明了一个参数aaa 在后面就可以使用了 -->
	<bind name="aaa" value="12"/>
	select
	id ,name ,age from t_user where id=${aaa}
</select>

在这里插入图片描述

8.sql语句

sql片段一般用来定义sql中的列
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值