MyBatis中的动态sql

简介:

1. 动态sql

动态sql是mybatis中的一个核心,什么是动态sql?动态sql即对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。就拿上一篇博文中对用户的综合查询一例来说:

select * from user where user.sex = #{user.sex} and user.username like '%${user.username}%'

假如这个user是null咋整?或者user.sex或者user.username为null呢?所以更严谨的做法应该是在执行这个语句之前要先进行判断才对,确保都不为空,那么我再去查询。这就涉及到了mybatis中的动态sql了。
在mybatis中,动态sql可以使用标签来表示,这很类似于jstl表达式,我们可以将上面的sql语句改成动态sql,如下:

<select id="findUserList" parameterType="mybatis.po.UserQueryVo" resultType="mybatis.po.User">
	select * from user	<!-- where可以自动去掉条件中的第一个and -->
	<where>
		<if test="user!=null">
			<if test="user.sex!=null and user.sex!=''">
				and user.sex = #{user.sex}			</if>
			<if test="user.username!=null and user.username!=''">
				and user.username like '%${user.username}%'	</if>
		</if>
	</where>
</select>

上面的代码很好理解,主要就是加了一些判断,条件不为空,才进行查询条件的拼接,让mybatis动态的去执行。那么在测试代码中,我们可以故意的将user.sex不赋初值,就可以看到查询的结果是不一样的。

2. sql片段

那么现在还有个问题,如果好几个statement都需要这样做,而且动态sql部分都一样,这就会导致一些代码的重复,所以如果遇到这种情况,我们就应该抽取,动态sql也可以抽取,我们可以将动态的这部分sql抽取成sql片段,然后在具体的statement中引用进来即可。如下:

<sql id="query_user_where">
	<if test="user!=null">
		<if test="user.sex!=null and user.sex!=''">
			and user.sex = #{user.sex}		</if>
		<if test="user.username!=null and user.username!=''">
			and user.username like '%${user.username}%'		</if>
	</if>
</sql>

id是给该sql片段起个名字而已,内部就是上面的where动态部分,然后我们将上面原来的动态部分改成对这个sql片段的引用,如下:

<select id="findUserList" parameterType="mybatis.po.UserQueryVo" resultType="mybatis.po.User">
	select * from user	<where>
		<!-- 引用sql片段的id,如果refid指定的id不在本mapper文件中,需要在前面加上namespace -->
		<include refid="query_user_where"></include>
		<!-- 还可以引用其他sql片段 -->
	</where>
</select>

3. foreach

还有个问题:如果我们要向sql传递数组或List该咋整呢?mybatis使用的是foreach解析。为了模拟这个场景,我们将上面的查询改成多个id查询,有两种查询方式:

SELECT * FROM USER WHERE id=1 OR id=12 OR id=17SELECT * FROM USER WHERE id IN(1,12,17)

首先有一点很明确,既然要使用多个id进行查询,那么多个id肯定要作为参数传进来,所以存储多个id的List需要放到UserQueryVo中作为一个属性,这点很好理解,所以我们先在UserQueryVo中增加这个属性:

//传入多个id
private List<Integer> ids;

然后我们修改UserMapper.xml中的sql片段(还是写在sql片段中),如下:

<sql id="query_user_where">
	<if test="user!=null">
		<if test="user.sex!=null and user.sex!=''">
			and user.sex = #{user.sex}		</if>
		<if test="user.username!=null and user.username!=''">
			and user.username like '%${user.username}%'		</if>
	</if>
	<if test="ids!=null">
		<!-- 使用右边的sql拼接:AND (id=1 OR id=12 OR id=17) -->
		<foreach collection="ids" item="user_id" open="AND (" close=")" separator="OR">
			id=#{user_id}		
        </foreach>
	</if>
</sql>

下面简单介绍一下这个foreach中相关属性的作用:

collection:指定输入对象中的集合属性,这里就是这个ids。 item:表示每个遍历生成的对象,自己起个名儿,在foreach体中使用。 open:开始遍历时拼接的sql串。 close:结束遍历时拼接的sql串。 separator:遍历的两个对象中需要拼接的sql串。

我们测试一下,然后看下控制台打印出来的sql就很容易理解了。测试程序:

@Testpublic void testFindUserList() throws Exception {
	
	SqlSession sqlSession = sqlSessionFactory.openSession();
	//创建UserMapper对象,mybatis自动生成mapper代理对象
	UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
	
	//创建包装对象,设置查询条件
	UserQueryVo userQueryVo = new UserQueryVo();
	User user = new User();
	//由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中
	user.setSex("男");
	user.setUsername("倪升武");
	
	//传入多个id
	List<Integer> ids = new ArrayList<Integer>();
	ids.add(1);
	ids.add(12);
	ids.add(17);
	userQueryVo.setIds(ids);
	
	userQueryVo.setUser(user);	
	//调用userMapper的方法
	List<User> list = userMapper.findUserList(userQueryVo);
	System.out.println(list);}

  看下控制台打印出的sql:

select * from user WHERE user.sex = ? and user.username like '%倪升武%' AND ( id=? OR id=? OR id=? ) 

注意一个细节:在mybatis中,如果输入的是Integer或者int类型的0,上面那个if判断标签返回的是false,也就是说,即使非空非'',也不会拼接标签体中的sql。
所以mybatis自动的将多个id拼接到了sql中。那么另外一个sql的实现就不再赘述了,跟上面的一样,唯一不同的就是sql片段部分,如下:

<!-- 使用右边的sql拼接:AND id IN(1,12,17) -->
<foreach collection="ids" item="user_id" open="AND id IN(" close=")" separator=",">
	#{user_id}</foreach>

本文作者: java乐园
本文来自云栖社区合作伙伴“JAVA乐园”,了解相关信息可以关注“JAVA乐园
相关文章
|
1月前
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
21 1
|
1月前
|
Java 关系型数据库 数据库连接
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
26 1
|
1月前
Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)
Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)
15 1
|
1月前
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
22 1
|
1月前
Mybatis+mysql动态分页查询数据案例——房屋信息的实现类(HouseDaoMybatisImpl)
Mybatis+mysql动态分页查询数据案例——房屋信息的实现类(HouseDaoMybatisImpl)
22 2
|
1月前
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
15 1
|
2天前
|
SQL XML Java
mybatis 调用修改SQL时 出现了一个问题 没有修改成功也没有报错
mybatis 调用修改SQL时 出现了一个问题 没有修改成功也没有报错
11 0
|
1月前
|
Java 数据库连接 mybatis
Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)
Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)
20 1
|
1月前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
15 1
|
1月前
Mybatis+mysql动态分页查询数据案例——房屋信息的接口(IHouseDao)
Mybatis+mysql动态分页查询数据案例——房屋信息的接口(IHouseDao)
12 1