Mybatis映射文件 if,where,foreach语句,动态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

上边的sql也可以改为:

<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>

<where />可以自动处理第一个and。

3.foreach

向sql传递数组或List,mybatis使用foreach解析,如下:

3.1 通过pojo传递list

传入多个id查询用户信息,用下边两个sql实现:

SELECT * FROM USERS WHERE username LIKE '%张%' AND (id=10 OR id =89 OR id=16)

SELECT * FROM USERS WHERE username LIKE '%张%'  id IN (10,89,16)

在pojo中定义list属性ids存储多个用户id,并添加getter/setter方法

mapper.xml

<if test="ids!=null and ids.size>0">
	    	<foreach collection="ids" open=" and id in(" close=")" item="id" separator="," >
	    		#{id}
	    	</foreach>
</if>

测试代码:

List<Integer> ids = new ArrayList<Integer>();
		ids.add(1);//查询id为1的用户
		ids.add(10); //查询id为10的用户
		queryVo.setIds(ids);
		List<User> list = userMapper.findUserList(queryVo);

3.2 传递单个List

传递List类型在编写mapper.xml没有区别,唯一不同的是只有一个List参数时它的参数名为list。

Mapper.xml

<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>

Mapper接口

public List<User> selectUserByList(List userlist) throws Exception;

测试:

Public void testselectUserByList()throws Exception{
		//获取session
		SqlSession session = sqlSessionFactory.openSession();
		//获限mapper接口实例
		UserMapper userMapper = session.getMapper(UserMapper.class);
		//构造查询条件List
		List<User> userlist = new ArrayList<User>();
		User user = new User();
		user.setId(1);
		userlist.add(user);
		user = new User();
		user.setId(2);
		userlist.add(user); 
		//传递userlist列表查询用户列表
		List<User>list = userMapper.selectUserByList(userlist);
		//关闭session
		session.close();
	}

3.3 传递单个数组,数组中是pojo

Mapper.xml

<!-- 传递数组综合查询用户信息 -->
	<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.id} 
		</foreach>
		</if>
		</where>
	</select>

sql只接收一个数组参数,这时sql解析参数的名称mybatis固定为array,如果数组是通过一个pojo传递到sql则参数的名称为pojo中的属性名。

index:为数组的下标。

item:为数组每个元素的名称,名称随意定义

open:循环开始

close:循环结束

separator :中间分隔输出

Mapper接口:

public List<User> selectUserByArray(Object[] userlist) throws Exception;

测试:

Public void testselectUserByArray()throws Exception{
		//获取session
		SqlSession session = sqlSessionFactory.openSession();
		//获限mapper接口实例
		UserMapper userMapper = session.getMapper(UserMapper.class);
		//构造查询条件List
		Object[] userlist = new Object[2];
		User user = new User();
		user.setId(1);
		userlist[0]=user;
		user = new User();
		user.setId(2);
		userlist[1]=user;
		//传递user对象查询用户列表
		List<User>list = userMapper.selectUserByArray(userlist);
		//关闭session
		session.close();
	}
3.4 传递单个数组(数组中是字符串类型)

Mapper.xml

<!-- 传递数组综合查询用户信息 -->
	<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>

如果数组中是简单类型则写为#{item},不用再通过ognl获取对象属性值了。

Mapper接口:

public List<User> selectUserByArray(Object[] userlist) throws Exception;

测试:

Public void testselectUserByArray()throws Exception{
		//获取session
		SqlSession session = sqlSessionFactory.openSession();
		//获限mapper接口实例
		UserMapper userMapper = session.getMapper(UserMapper.class);
		//构造查询条件List
		Object[] userlist = new Object[2];
		userlist[0]=”1”;
		userlist[1]=”2”;
		//传递user对象查询用户列表
		List<User>list = userMapper.selectUserByArray(userlist);
		//关闭session
		session.close();
	}

4.sql片段

Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到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>
		</where>
	</select>

将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>
使用include引用:
<select id="findUserList" parameterType="user" resultType="user">
		select * from user 
		<where>
		<include refid="query_user_where"/>
		</where>
	</select>

注意:如果引用其它mapper.xml的sql片段,则在引用时需要加上namespace,如下:

<includerefid="namespace.sql片段”/>

5.关于 关联查询,其实和单表差不多,只是pojo里面要加入查询到的所有信息;

6.延迟加载--------待续














猜你喜欢

转载自blog.csdn.net/SELECT_BIN/article/details/80733258