星空网 > 软件开发 > Java

Mybatis学习笔记(四) 之动态SQL语句

动态SQL

mybatis 的动态sql语句是基于OGNL表达式的。可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:

1. if 语句 (简单的条件判断)
2. choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似.
3. trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)
4. where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误)
5. set (主要用于更新时)
6. foreach (在实现 mybatis in 语句查询时特别有用)

1、if 处理

普通带有where查询的语句如下,

  <!-- 综合查询 -->  <select id="findUserList" parameterType="User" resultType="User">    select * from `user` where id = #{id} and userName like '%${userName}%'   </select>

此时,如果id或userName为null,此语句查询结果可能报错,那么我们可以对where语句使用逻辑上的if判断:如果值为null或等于空字符串,我们就不进行此条件的判断,增加灵活性。

加入判断之后,如下:

<!-- 传递pojo综合查询用户信息 -->	<select id="findUserList" parameterType="user" resultType="user">		select * from user where 1=1 		<if test="id!=null and id!=''">and id=#{id}		</if>		<if test="username!=null and username!=''">and username like '%${username}%'		</if>	</select>

这条语句查询用户表,如果id或usrname不为null或空,传入userName和id查询条件,那么就查询语句就为:select * from `user` where id = #{id} and userName like '%${userName}%' ;反之,则查询select * from user where 1=1 所有记录。

注意:where语句中加上1=1的目的就是防止 if语句中都不成立情况下,where语句后条件会为空,这样会报错的。如下:如果if条件都不成立,就相当于查询,select * from user where 这是错误的sql语句。

<!-- 传递pojo综合查询用户信息 -->	<select id="findUserList" parameterType="user" resultType="user">		select * from user where		<if test="id!=null and id!=''">and id=#{id}		</if>		<if test="username!=null and username!=''">and username like '%${username}%'		</if>	</select>

2、choose (when, otherwise)处理

相当于java语言中的switch,有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。而使用if标签时,只要test中的表达式为true,就会执行if标签中的条件。MyBatis提供了choose 元素。if标签是与(and)的关系,而choose标签是或(or)的关系。

choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则choose结束,跳出choose,当choose中所有when的条件都不满则时,则执行otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default

<select id="queryByName" parameterType="User" resultType="User">   SELECT <include refid="columns"></include>   FROM sys_user   WHERE user_type_id = 1   <choose>     <when test="userName != null">user_name like '%' #{userName} '%'</when>     <when test="nickName != null">nick_name like '%' #{nickName} '%'</when>     <otherwise>is_valid = 1</otherwise>   </choose> </select> 

3、trim处理

trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀) 

trim 属性

                prefix:前缀覆盖并增加其内容

                suffix:后缀覆盖并增加其内容

                prefixOverrides:前缀判断的条件

                suffixOverrides:后缀判断的条件

  <select id="findUserList" parameterType="User" resultType="User">    select b.* from sys_menu b where where 1=1  	<trim suffix="WHERE" suffixOverrides="AND | OR">   		<if test="id != null and id !='' "> AND b.id =#{id} </if>   		<if test="name != null"> AND b.menu_name like #{name} </if>     </trim>   </select>

最终sql打印为:select b.* from sys_menu b where 1 = 1 AND b.menu_name like '' WHERE

4、foreach处理

传递list实现

当要传入多个id查询时候,比如 :SELECT * FROM USERS WHERE username LIKE '%张%'  and id IN (10,89,16),在使用IN查询的时候。

这个时候,就可以使用foreach来传入参数。如果if成立,实现的查询语句就为上面sql语句。

<select id="selectUserByList" parameterType="java.util.List" resultType="user">		select * from user 		<where>		<!-- 传递List,List中是pojo -->		<if test="list!=null">		<foreach collection="list" item="item" open="and id in("separator=","close=")">		  #{item.id} 		</foreach>		</if>		</where>	</select>

传递单个数据(数组中是字符串类型)实现

<!-- 传递数组综合查询用户信息 -->	<select id="selectUserByArray" parameterType="Object[]" resultType="user">		select * from user 		<where>		<!-- 传递数组 -->		<if test="array!=null">		<foreach collection="array"index="index"item="item"open="and id in("separator=","close=")">		  #{item} 		</foreach>		</if>		</where>	</select>

我们知道Mybatis进行SQL映射时,传入参数只能有一个,如果想传入多个参数,只能使用Java的List或是Array进行封装后再传入。上面的语句就是将要删除的多条记录的Id值放在了List对象中传入。

foreach 元素的功能是非常强大的,它允许你指定一个集合,声明可以用在元素体内的集合项和索引变量。它也允许你指定开闭匹配的字符串(上例中的open和close属性)以及在迭代中间放置分隔符(separator属性)。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。

我们可以将一个 List 实例或者数组作为参数对象传给 MyBatis,当我们这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以“list”作为键,而数组实例的键将是“array”。

参考

1、mybatis系列:http://blog.csdn.net/chris_mao/article/details/48827961

2、动态sql语句:http://limingnihao.iteye.com/blog/782190

 




原标题:Mybatis学习笔记(四) 之动态SQL语句

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊VAT税号申请及费用详解:https://www.kjdsnews.com/a/1352965.html
亚马逊VAT税号申请及查询指南:https://www.kjdsnews.com/a/1352966.html
亚马逊VAT税务计算指南:https://www.kjdsnews.com/a/1352967.html
亚马逊VAT税务缴纳指南:https://www.kjdsnews.com/a/1352968.html
亚马逊VAT税务筹划方法介绍:https://www.kjdsnews.com/a/1352969.html
亚马逊VAT税率有多少?:https://www.kjdsnews.com/a/1352970.html
餐饮品牌「杨国福」加速出海,计划在欧洲新开20家门店:https://www.kjdsnews.com/a/1841042.html
点点点,2024年消费者法案就这样通过了!:https://www.kjdsnews.com/a/1841043.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流