---
title: "MyBatis3_轻松入门_4"
author: "hunthon"
date: "2013-10-30T21:07:50Z"
tags: ["mybatis3", "动态sql"]
categories: ["企业架构"]
slug: "mybatis3-4"
draft: false
lastmod: "2013-10-30T21:07:50Z"
id: 1967041
url: "https://hunthon.iteye.com/blog/1967041"
---

# MyBatis3

博文目录

1. [if 动态SQL的实现](#1)
2. [choose(when,otherwise) 动态SQL的实现](#2)
3. [set 动态SQL的实现](#3)
4. [foreach动态 SQL的实现](#4)

 

[]()

 **动态SQL**

**问题：**  
我有一个SQL语句，根据用户名和密码来查一个用户！前面的博文讲到了，将条件封装到一个对象中进行传值！  
SQL语句：

```
select id,username,password 
from t_user 
where username=#{username} 
and 
password=#{password}
```

  传参的代码如下：

```
User user=new User();
user.setUsername("tom");
user.setPassword("000000");

userMapper.findByUsernameAndPassword(user);
```

 

 这样就可以实现多条件查询了，但是如果我们传给方法的user没有设置password，即：

```
User user=new User();
user.setUsername("tom");

userMapper.findByUsernameAndPassword(user);
```

 

这样会**报错**的！！！

这时我们可以MyBatis提供的**动态查询**：  
上面的问题可以使用if条件查询进行解决！

在**UserMapper.xml**将方法的配置修改如下：

```
<select id="findByUsernamePassword" parameterType="User" resultType="User">
	select id,username,password 
	from t_user 
	where 
	username=#{username} 
	<!--如果password!=null,查询条件加一条，否则只有username=*-->
	<if test="password!=null">
		and 
		password=#{password}
	</if>
</select>
```

 

[]()

 这样就可以实现if动态查询了！！  
你可以使用多个If进行条件的过滤，但是看起来挺怪的，我们介绍下面的：**choose(where,otherwise)**

```
<select id="findByUsernamePassword" parameterType="User" resultType="User">
	select id,
	<!--当password!=null时,选择id,username,password,否则选择id,username-->
	<choose>
		<when test="password!=null">
 			username,password 
		</when>
		<otherwise>
			username 
		</otherwise>
	</choose>
	from t_user 
	where 
	username=#{username} 
	<if test="password!=null">
		and 
		password=#{password}
	</if>
</select>
```

 

 对于下面这种情况，当两个 If 条件都不符合时，剩下一个where怎么办呢？肯定是错的！

```
<select id="findByUsernamePassword" parameterType="User" resultType="User">
	select id,
	<choose>
		<when test="password!=null">
 			username,password 
		</when>
		<otherwise>
			username 
		</otherwise>
	</choose>
	from t_user 
	where 
	<!-- 若下面的都不成立，上面的where就是多余的，SQL就是错误的 -->
	<if test="username!=null">
		username=#{username} 
	</if>
	<if test="password!=null">
		and 
		password=#{password}
	</if>
</select>
```

 

 我们使用**where标签**进行条件的封装，如果一个都不成立，则就没有where否则自动添加where。

```
<select id="findByUsernamePassword" parameterType="User" resultType="User">
	select id,
	<choose>
		<when test="password!=null">
 			username,password 
		</when>
		<otherwise>
			username 
		</otherwise>
	</choose>
	from t_user 
	<where> 
		<!-- 若下面的都不成立，就不会出现where-->
		<if test="username!=null">
 			username=#{username} 
		</if>
		<if test="password!=null">
			and 
			password=#{password}
		</if>
	</where>
</select>
```

 

 使用**trim**可以实现跟where相同的效果：

```
<select id="findByUsernamePassword" parameterType="User" resultType="User">
	select id,
	<choose>
		<when test="password!=null">
 			username,password 
		</when>
		<otherwise>
			username 
		</otherwise>
	</choose>
	from t_user 
	<trim prefix="WHERE" prefixOverrides="AND |OR ">
		<!-- 若下面的都不成立，就不会出现where-->
		<if test="username!=null">
 			username=#{username} 
		</if>
		<if test="password!=null">
			and 
			password=#{password}
		</if>
	</trim>
</select>
```

 

[]()

 **注意：**  
条件修改后的返回数据是多个还是单个，要注意修改其resultType

对于我们要修改某一记录时，我们使用**set**来实现对数据的修改：

```
<update id="edit" parameterType="User">
	update t_user 
	<set>
		<if test="username!=null">username=#{username},</if>
		<if test="password!=null">password=#{password} </if>
	</set>
	where id=#{id}
</update>
```

 

 同样可以使用trim来替换set：

```
<update id="edit" parameterType="User">
	update t_user 
	<trim prefix="SET" suffixOverrides=",">
		<if test="username!=null">username=#{username},</if>
		<if test="password!=null">password=#{password} </if>
	</trim>
	where id=#{id}
</update>
```

 

[]()

 对于：**where \** in ()**查询，我们使用**foreach**：

```
<select id="findUserIn" resultType="User" parameterType="List">
	select id,username,password 
	from t_user 
	where id in 
	<foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
		#{item}  
	</foreach>  
</select>
```

 

 **list**指示这里的参数需要的是一个List  
**item**表示每一项  
**open,separator,close**表示Item们包裹在 () 中，并以 , 分隔！！！

 测试：

```
UserMapper userMapper=session.getMapper(UserMapper.class);
		
List<Integer> l=new ArrayList<Integer>();
l.add(1);
l.add(2);
List<User> userList=userMapper.findUserIn(l);
System.out.println(userList.size());//2
```