前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mybatis教程4(动态SQL)

mybatis教程4(动态SQL)

作者头像
用户4919348
发布2019-04-02 11:36:28
5020
发布2019-04-02 11:36:28
举报
文章被收录于专栏:波波烤鸭波波烤鸭

动态SQL语句

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

1. if语句

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

代码语言:javascript
复制
<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> 
代码语言:javascript
复制
// 接口
public List<User> queryUser(User user);

测试

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

2.choose, when, otherwise

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

代码语言:javascript
复制
<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语句如下:

代码语言:javascript
复制
select * from t_user where 

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

代码语言:javascript
复制
	<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> 

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

代码语言:javascript
复制
	<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主要也是用来解决更新问题的。

代码语言:javascript
复制
<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标记的功能

属性

说明

prefix

前缀

prefixOverrides

去掉第一个指定内容

suffix

后缀

suffixoverride

去掉最后一个指定内容

替代<where>的用法

代码语言:javascript
复制
	<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>的用法

代码语言:javascript
复制
<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>

或者

代码语言:javascript
复制
<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用来遍历,遍历的对象可以是数组,也可以是集合。

属性

说明

collection

collection属性的值有三个分别是list、array、map三种

open

前缀

close

后缀

separator

分隔符,表示迭代时每个元素之间以什么分隔

item

表示在迭代过程中每一个元素的别名

index

用一个变量名表示当前循环的索引位置

接口中方法

代码语言:javascript
复制
public interface UserMapper {
	// 如果不指定@Param 默认是array
	public List<User> queryUserByIds(@Param("ids")List<Integer> ids);

	public int insertUser(@Param("users")List<User> users);
}
代码语言:javascript
复制
<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 表达式中创建一个变量并将其绑定到上下文。

代码语言:javascript
复制
<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中的列

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年01月22日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 动态SQL语句
    • 1. if语句
      • 2.choose, when, otherwise
        • 3.where语句
          • 4.set语句
            • 5.trim
              • 6.foreach语句
                • 7.bind
                  • 8.sql块
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档