MyBatis中动态SQL

MyBatis中动态SQL

一、说在前面

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

二、实例代码片段

1、if

	<!-- 传递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>

2、where

使用where来修改上面的代码,<where />可以自动处理第一个and。

<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>
		</where>
	</select>

3、foreach

向sql传递数组或List,mybatis使用foreach解析,代码如下:
(1)需求
在用户查询列表和查询总数的statement中增加多个id输入查询。
sql语句如下(两种方法):

SELECT * FROM user WHERE sex=1 and username LIKE "%小明%" and (id=1 OR id=16 OR id=22)

SELECT * FROM user WHERE sex=1 and username LIKE "%小明%" and id in (1,16,22)
(2)在pojo中定义list属性ids存储多个用户id,并添加getter/setter方法

	//传入多个id
	private List<Integer> ids;
	
	public List<Integer> getIds() {
		return ids;
	}

	public void setIds(List<Integer> ids) {
		this.ids = ids;
	}
(3)修改mapper.xml

		<if test="ids!=null">
		<!-- 使用foreach遍历传入的ids
			collection:指定输入对象集合属性
			item:每次遍历生成的对象
			open:开始遍历时候的拼接串
			close:结束遍历时候拼接的串
			separator:遍历的两个对象中需要拼接的串
		 -->
		 <!-- 
		 	实现下面的sql拼接
		 	and (id=1 OR id=16 OR id=22)
		  -->
			<foreach collection="ids" item="user_id" open="and (" close=")" separator="or">
				id=#{user_id}
			</foreach>
			
		   <!-- 
		 	 另一种方式实现拼接,实现下面的sql拼接
		 	 and id in (1,16,22)
		    -->
			<!-- <foreach collection="ids" item="user_id" open="and id in (" close=")" separator=",">
				#{user_id}
			</foreach> -->
			
		</if>
(4)测试代码

	//Mapper代理方式 用户综合信息查询
	@Test
	public void testFindUserList() throws Exception {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		
		//创建UserMapper对象,mybatis自动生成代理对象
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		//创建包装对象,设置查询条件
		UserQueryVo userQueryVo = new UserQueryVo();
		UserCustom userCustom = new UserCustom();
		userCustom.setSex("1");
		userCustom.setUsername("小明");
		
		//传入多个ids
		List<Integer> ids = new ArrayList<Integer>();
		ids.add(1);
		ids.add(16);
		ids.add(22);
		
		userQueryVo.setUserCustom(userCustom);
		userQueryVo.setIds(ids);
		
		//调用UserMapper的方法
		List<UserCustom> list = userMapper.findUserList(userQueryVo);
		System.out.println("Mapper代理方式 用户综合信息查询====》"+list);
	}


4、sql片段

Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下
(1)未提取重复之前代码
	<!-- 传递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>
		</where>
	</select>
(2)提取where条件

<sql id="query_user_where">
	<if test="id!=null and id!=''">
		and id=#{id}
	</if>
	<if test="username!=null and username!=''">
		and username like '%${username}%'
	</if>
</sql>
(3)使用include引用

	<select id="findUserList" parameterType="user" resultType="user">
		select * from user 
		<where>
		<include refid="query_user_where"/>
		</where>
	</select>
(4)总结
如果引用其它mapper.xml的sql片段,则在引用时需要加上namespace,如下:
<include refid="namespace.sql片段”/>


By luoyepiaoxue2014
微博地址:  http://weibo.com/luoyepiaoxue2014  点击打开链接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章