(七)mybatis学习之动态SQL

前言

mybatis的核心是对sql语句进行灵活的操作,通过表达式进行判断,对sql进行灵活拼接、组装。

动态sql包括:if、choose(when,otherwise)、where、set、trim、foreach、sql片段

if

if标签比较简单,这里记录一下文档内容的例子,通过看例子,就清楚的知道if是如何使用的了。

<select id="findActiveBlogWithTitleLike"
resultType="Blog">
SELECT * FROM BLOG
WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
</select>


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

choose(when,otherwise)

choose标签跟if差不多,多了几层判断而已,这里也使用文档的例子。
<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>


where

where 标签可以自动配置where关键字,并自动去掉条件中的第一个and

示例:

配置文件内容:

<select id="selectWhere" parameterType="user" resultType="user">
		select * from tab_user
		<where>
			<if test="userName!=null and userName!=''">
				and userName like '%${userName}%'
			</if>
			<if test="city!=null and city!=''">
				and city like '%${city}%'
			</if>
		</where>
	</select>


接口方法:

//查询用户列表
	public List<User> selectWhere(User user) throws Exception;

执行测试后,在控制台看到如下的sql语句:

  2016-04-04 11:16:02,157 [main] DEBUG [mapper.UserMapper.selectWhere] - ==>  Preparing: select * from tab_user WHERE userName like '%c%' and city like '%广州%' 
  2016-04-04 11:16:02,209 [main] DEBUG [mapper.UserMapper.selectWhere] - ==> Parameters: 
  2016-04-04 11:16:02,252 [main] DEBUG [mapper.UserMapper.selectWhere] - <==      Total: 1
说明where标签确实是把动态sql中的第一个and去掉了

set

在update操作的时候,使用set标签能自动配置set关键字,并把条件后面多余的逗号去掉

<!-- 更新用户信息 -->
	<update id="updateUser" parameterType="pojo.User" >
		update tab_user 
		<set>
			<if test="userName!=null and userName!=''">
				userName=#{userName},
			</if>
			<if test="password!=null and password!=''">
				`password`=#{password},
			</if>
		</set>
		where id=#{id}
	</update>
	

trim

trim标签更加灵活的处理字符串之间的替换,可以实现where,set标签的功能

1.使用trim实现where的功能:

<!-- where标签 -->
	<select id="selectWhere" parameterType="user" resultType="user">
		select * from tab_user
		<where>
			<if test="userName!=null and userName!=''">
				and userName like '%${userName}%'
			</if>
			<if test="city!=null and city!=''">
				and city like '%${city}%'
			</if>
		</where>
	</select>
	
	<!-- 使用trim实现上面的where标签的功能 -->
	<select id="selectWhereByTrim" parameterType="user" resultType="user">
		select * from tab_user
		<!-- 
			把最前面是prefixOverrides指定的内容,用prefix指定的内容去替换,比如这里,如果条件查询中前面是and或or开头,则把and或or替换为where
			把最后面是suffixOverrides指定的内容,用suffix指定的内容去替换,这里后面不需要替换什么,所以都设为""。默认都是""
		 -->
		<trim prefix="where" prefixOverrides="and|or" suffix="" suffixOverrides="">
			<if test="userName!=null and userName!=''">
				and userName like '%${userName}%'
			</if>
			<if test="city!=null and city!=''">
				and city like '%${city}%'
			</if>
		</trim>
	</select>

2.使用trim实现set的功能

<!-- 使用set标签更新用户信息 -->
	<update id="updateUser" parameterType="pojo.User" >
		update tab_user 
		<set>
			<if test="userName!=null and userName!=''">
				userName=#{userName},
			</if>
			<if test="password!=null and password!=''">
				`password`=#{password},
			</if>
		</set>
		where id=#{id}
	</update>
	
	<!-- 使用trim实现上面set标签更新用户信息的功能 -->
	<update id="updateUserByTrim" parameterType="pojo.User" >
		update tab_user 
		<!-- 
			把最前面是prefixOverrides表示的内容,用prefix指定的内容去替换。这里是在前面加上set关键字。即是把最前面的""用set替换。
			把最后面是suffixOverrides表示的内容,用suffix指定的内容去替换。这里是把最后多余的逗号去掉,所以使用""去替换逗号
		 -->
		<trim prefixOverrides="" prefix="set" suffixOverrides="," suffix="">
			<if test="userName!=null and userName!=''">
				userName=#{userName},
			</if>
			<if test="password!=null and password!=''">
				`password`=#{password},
			</if>
		</trim>
		where id=#{id}
	</update>


foreach

<!-- 查询语句为: select * from tab_user where id=1 or id=2 -->
	<select id="selectForeach1" parameterType="user" resultType="user">
		select * from tab_user where 
		<!-- 
			collection:指定输入对象的属性名称,比如这里是user对象中的idsList属性
			item:每个遍历的对象变量名(自定义),这里设置为_id
			open:开始遍历时设置的拼接内容,这里在开始遍历时不需要拼接任何内容
			close:结束遍历时设置的拼接内容,结束遍历时不需要拼接任何内容
			separator:遍历两个对象中间需要拼接的内容,内容之间用 or 拼接
		 -->
		<foreach collection="idsList" item="_id" open="" close="" separator=" or ">
			id=#{_id}
		</foreach>
	</select>
	
	<!--查询语句为: select * from tab_user where id in(1,2) -->
	<select id="selectForeach2" parameterType="user" resultType="user">
		select * from tab_user where id in
		<!-- 
			collection:指定输入对象的属性名,比如这里是user对象中的idsList属性
			item:每个遍历的对象变量名(自定义),这里设置为tempId
			open:开始遍历时设置的拼接字符串,这里需要以左括号开始
			close:结束遍历时设置的拼接字符串,以右括号结束
			separator:遍历的两个对象中间需要拼接的内容,内容之间用逗号拼接
		 -->
		<foreach collection="idsList" item="tempId" open="(" close=")" separator=",">
			#{tempId}
		</foreach>
	</select>


sql片段

这里也记录一下文档的介绍,实际中需要用的时候再查看文档进行开发。
示例1:
<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>


<select id="selectUsers" resultType="map">
select
<include refid="userColumns"><property name="alias" value="t1"/></include>,
<include refid="userColumns"><property name="alias" value="t2"/></include>
from some_table t1
cross join some_table t2
</select>

示例2:
<sql id="sometable">
${prefix}Table
</sql>

<sql id="someinclude">
from
<include refid="${include_target}"/>
</sql>

<select id="select" resultType="map">
select
field1, field2, field3
<include refid="someinclude">
<property name="prefix" value="Some"/>
<property name="include_target" value="sometable"/>
</include>
</select>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值