文章目录
一、动态SQL之if标签1.1 在IUserDao.java中添加方法1.2 在IUserDao.xml中映射该方法1.3 测试1.4 输出结果
二、动态SQL之where标签2.1 在IUserDao.xml中映射方法2.2 测试2.3 测试结果
三、动态SQL之foreach标签3.1 创建QueryVo.java3.2 在IUserDao.java中添加方法3.3 在IUserDao.xml中映射该方法3.4 测试3.5 测试结果
一、动态SQL之if标签
1.1 在IUserDao.java中添加方法
List
<User> findUserByCondition(User user
);
1.2 在IUserDao.xml中映射该方法
<!--根据用户信息查询
-->
<select id
="findUserByCondition" parameterType
="user" resultMap
="userMap">
select
* from user where
1=1
<if test
="userName != null">
and username
=#
{userName
}
</if>
</select
>
1.3 测试
@Test
public void testFindUserByCondition() {
User user
= new User();
user
.setUserName("李四");
List
<User> users
= dao
.findUserByCondition(user
);
for (User u
: users
) {
System
.out
.println(u
);
}
}
1.4 输出结果
User
{userId
=2, userName
='李四', userBirthday
=Thu Jul
29 08:00:00 CST
1999, userGender
='女'}
User
{userId
=7, userName
='李四', userBirthday
=Sun Oct
04 08:00:00 CST
2020, userGender
='男'}
注意:具体代码看项目day03_mybatis_02dynamicSQL
二、动态SQL之where标签
2.1 在IUserDao.xml中映射方法
<!--根据用户信息查询
-->
<select id
="findUserByCondition" parameterType
="user" resultMap
="userMap">
select
* from user
<where>
<if test
="userName != null">
and username
=#
{userName
}
</if>
<if test
="userGender != null">
and gender
=#
{userGender
}
</if>
</where
>
</select
>
2.2 测试
@Test
public void testFindUserByCondition() {
User user
= new User();
user
.setUserName("李四");
user
.setUserGender("女");
List
<User> users
= dao
.findUserByCondition(user
);
for (User u
: users
) {
System
.out
.println(u
);
}
}
2.3 测试结果
User
{userId
=2, userName
='李四', userBirthday
=Thu Jul
29 08:00:00 CST
1999, userGender
='女'}
三、动态SQL之foreach标签
3.1 创建QueryVo.java
package cn
.lhh
.domain
;
import java
.util
.List
;
public class QueryVo {
private User user
;
private List
<Integer> ids
;
public User
getUser() {
return user
;
}
public void setUser(User user
) {
this.user
= user
;
}
public List
<Integer> getIds() {
return ids
;
}
public void setIds(List
<Integer> ids
) {
this.ids
= ids
;
}
}
3.2 在IUserDao.java中添加方法
List
<User> findUserByIds(QueryVo vo
);
3.3 在IUserDao.xml中映射该方法
<!-- 根据多个id查询多个用户
-->
<select id
="findUserByIds" parameterType
="queryVo" resultMap
="userMap">
select
* from user
<where>
<if test
="ids != null and ids.size() > 0">
<foreach collection
="ids" open
="and id in (" close
=")" item
="uid" separator
=",">
#
{uid
} <!-- #中的内容应该与item保持一致
-->
</foreach
>
</if>
</where
>
</select
>
3.4 测试
@Test
public void testFindUserByIds() {
List
<Integer> ids
= new ArrayList<>();
ids
.add(3);
ids
.add(1);
ids
.add(7);
QueryVo vo
= new QueryVo();
vo
.setIds(ids
);
List
<User> users
= dao
.findUserByIds(vo
);
for (User user
: users
) {
System
.out
.println(user
);
}
}
3.5 测试结果
User
{userId
=1, userName
='张三', userBirthday
=Mon Mar
29 08:00:00 CST
1999, userGender
='男'}
User
{userId
=3, userName
='王大大', userBirthday
=Thu Oct
22 08:00:00 CST
2020, userGender
='男'}
User
{userId
=7, userName
='李四', userBirthday
=Sun Oct
04 08:00:00 CST
2020, userGender
='男'}
具体代码见day03_mybaits_02dynamicSQL项目
视屏观看ssm mybatis_p45-P47