Mybatis04-动态Sql

动态Sql语句

&emsp MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。
  虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。
  动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。

if语句

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

<select id="query1" resultType="user" parameterType="user">
		select 
			*
		from t_user
			where 
				1=1
				<if test="id!=null and id!=0">
					and id=#{id}
				</if>
				<if test="username!=null">
					and username=#{username}
				</if>
				<if test="password!=null">
					and password=#{password}
				</if>
				
	</select>
public List<User> query1(User user);

测试

InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		SqlSession session = factory.openSession();
		IUserDao dao = session.getMapper(IUserDao.class);
		User user=new User();
		user.setId(1);
		List<User> list = dao.query1(user);
		for (User u : list) {
			System.out.println(u);
		}

在这里插入图片描述

choose, when, otherwise

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


	<select id="query2" resultType="user" parameterType="user">
		select 
			*
		from t_user
		where
			1=1
			<choose>
				<when test="username!=null">
					and username=#{username}
				</when>
				<when test="password!=null">
					and password=#{password}
				</when>
				<otherwise>
					order by id desc
				</otherwise>
			</choose>
	</select>

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

				<otherwise>
					order by id desc
				</otherwise>

这条语句是被动执行的,当前面的条件都不满足的时候会执行该条件,否则不会执行
在这里插入图片描述
在这里插入图片描述

where语句

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

select * from t_user where 

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

<select id="query1" resultType="user" parameterType="user">
		select 
			*
		from t_user
			where 
				1=1
				<if test="id!=null and id!=0">
					and id=#{id}
				</if>
				<if test="username!=null">
					and username=#{username}
				</if>
				<if test="password!=null">
					and password=#{password}
				</if>
				
	</select>

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

<select id="query3" resultType="user" parameterType="user">
		select 
			*
		from t_user
			<where>
				<if test="id!=null and id!=0">
					and id=#{id}
				</if>
				<if test="username!=null">
					and username=#{username}
				</if>
				<if test="password!=null">
					and password=#{password}
				</if>
			</where>
	</select>

在这里插入图片描述

set语句

<update id="update1" parameterType="user">
		update
			t_user
		<set>
			<if test="username!=null">username=#{username},</if>
			<if test="password!=null">password=#{password},</if>
		</set>
			where
				id=#{id}
	</update>

trim

trim标记是一个格式化的标记,可以完成set或者是where标记的功能

属性 说明
prefix 前缀
prefixOverrides 去掉第一个指定内容
suffix 后缀
suffixoverride 去掉最后一个指定内容

替代的用法

<select id="query4" resultType="user" parameterType="user">
		select
		*
		from t_user
		<trim prefix="where" prefixOverrides="AND">
			<if test="id!=null and id!=0">
				and id=#{id}
			</if>
			<if test="username!=null">
				and username=#{username}
			</if>
			<if test="password!=null">
				and password=#{password}
			</if>
		</trim>	
	</select>

替代的用法

<update id="update3" parameterType="user">
		update
			t_user
		<trim prefix="set" suffixOverrides=",">
			<if test="username!=null">username=#{username},</if>
			<if test="password!=null">password=#{password},</if>
		</trim>
			where
				id=#{id}
	</update>

foreach语句

属性 说明
collection collection属性的值有三个分别是list、array、map三种
open 前缀
close 后缀
separator 分隔符,表示迭代时每个元素之间以什么分隔
item 表示在迭代过程中每一个元素的别名
index 用一个变量名表示当前循环的索引位置

接口中方法

	public List<User> queryByIds(@Param("ids") List<Integer> ids);
select id="queryByIds" resultType="user">
		select * 
			from t_user
			where 1=1
				<if test="ids!=null">
					and id in
					<foreach collection="ids" open="(" close=")" separator="," item="id">
						#{id}
					</foreach>
				</if>
	</select>

在这里插入图片描述
插入多条数据

<insert id="insert1" parameterType="User" 
		useGeneratedKeys="true" keyProperty="id">
		insert into 
			t_user(username,password)
		values
			<foreach collection="list" item="user" separator=",">
				(#{user.username},#{user.password})
			</foreach>
		
	</insert>

测试

<insert id="insert1" parameterType="User" 
		useGeneratedKeys="true" keyProperty="id">
		insert into 
			t_user(username,password)
		values
			<foreach collection="list" item="user" separator=",">
				(#{user.username},#{user.password})
			</foreach>
		
	</insert>

在这里插入图片描述

bind

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

	<bind name="id" value="1"/>

![
](https://img-blog.csdnimg.cn/2019042815470369.png)

sql块

<!-- 声明一个SQL代码块实现重复使用 --> 
	<sql id="baseSql">
		id,username,password
	</sql>
	

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41546940/article/details/89639682