1、动态SQL:if 语句
非动态sql
<select id ="selectUser" resultType="user" parameterType="com.te.po.User">
select * from user where username=#{username} and id=#{id}
</select>
通过上面的查询语句我们可以知道,如果username或者id为空,那么查询结果也会为空。使用动态语句可以解决这个问题
<select id ="selectUser" resultType="user" parameterType="com.te.po.User">
select * from user where
<if test="username!=null and username !=''">
username=#{username}
</if>
<if test="id!=null and id !=''">
and id=#{id}
</if>
</select>
这样写我们可以看到,如果id等于 null,那么查询语句为: select * from user where username=#{username}, 但是如果usename 为空呢?那么查询语句为 select * from user where and id=#{id},这是错误的 SQL 语句,如何解决呢?
2、动态sql:if+where
<select id="selectUser" resultType="user" parameterType="com.te.po.User">
select * from user
<where>
<if test="username != null and username!=''">
username=#{username}
</if>
<if test="id != null and id !=''">
and id=#{id}
</if>
</where>
</select>
这个“where”标签会知道如果它包含的if标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。
3、动态sql:if+set
<!-- 根据 id 更新 user 表的数据 -->
<update id="updateUserById" parameterType="com.te.po.User">
update user
<set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="sex != null and sex != ''">
sex = #{sex}
</if>
</set>
where id=#{id}
</update>
这样写,如果第一个条件 username 为空,那么 sql 语句为:update user u set u.sex=? where id=?
如果第一个条件不为空,那么 sql 语句为:update user set username = ? ,sex = ? where id=?
4、动态sql:choose(when,otherwise)
有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java 的 switch 语句
<select id="selectUserByChoose" resultType="com.te.po.User" parameterType="com.te.po.User">
select * from user
<where>
<choose>
<when test="id !='' and id != null">
id=#{id}
</when>
<when test="username !='' and username != null">
and username=#{username}
</when>
<otherwise>
and sex=#{sex}
</otherwise>
</choose>
</where>
</select>
这里我们有三个条件,id,username,sex,只能选择一个作为查询条件
如果 id 不为空,那么查询语句为:select * from user where id=?
如果 id 为空,那么看username 是否为空,如果不为空,那么语句为 select * from user where username=?;
如果 username 为空,那么查询语句为 select * from user where sex=?
这个动态sql有点类似于or
5、动态sql:trim
trim标记是一个格式化的标记,可以完成set或者是where标记的功能
5.1、用 trim 改写上面第二点的 if+where 语句
<select id="selectUser" resultType="user" parameterType="com.te.po.User">
select * from user
<!-- <where>
<if test="username != null and username !=''">
username=#{username}
</if>
<if test="sex != null and sex!=''">
and sex=#{sex}
</if>
</where> -->
<trim prefix="where" prefixOverrides="and | or">
<if test="username != null and username !=''">
and username=#{username}
</if>
<if test="sex != null and sex!=''">
and sex=#{sex}
</if>
</trim>
</select>
prefix:前缀
prefixoverride:去掉第一个and或者是or
5.2、用 trim 改写上面第三点的 if+set 语句
<!-- 根据 id 更新 user 表的数据 -->
<update id="updateUserById" parameterType="com.te.po.User">
update user
<!-- <set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="sex != null and sex != ''">
sex = #{sex}
</if>
</set> -->
<trim prefix="set" suffixOverrides=",">
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex},
</if>
</trim>
where id=#{id}
</update>
suffix:后缀
suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
6、动态SQL: foreach
需求:我们需要查询 user 表中 id 分别为1,2,3的用户
sql语句:select * from user where id=1 or id=2 or id=3
select * from user where id in (1,2,3)
6.1、建立一个 UserPo 类,里面封装一个 List ids 的属性
import java.util.List;
public class UserPo {
//封装多个用户的id
private List<Integer> ids;
public List<Integer> getIds() {
return ids;
}
public void setIds(List<Integer> ids) {
this.ids = ids;
}
}
6.2我们用 foreach 来改写 select * from user where id=1 or id=2 or id=3
<select id="selectUserByListId" parameterType="com.ys.vo.UserPo" resultType="com.ys.po.User">
select * from user
<where>
<!--
collection:指定输入对象中的集合属性
item:每次遍历生成的对象
open:开始遍历时的拼接字符串
close:结束时拼接的字符串
separator:遍历对象之间需要拼接的字符串
select * from user where 1=1 and (id=1 or id=2 or id=3)
-->
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>
//根据id集合查询user表数据
@Test
public void testSelectUserByListId(){
String statement = "com.te.po.userMapper.selectUserByListId";
UserPo up = new UserPo();
List<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(3);
up.setIds(ids);
List<User> listUser = session.selectList(statement, up);
for(User u : listUser){
System.out.println(u);
}
session.close();
}
6.3、我们用 foreach 来改写 select * from user where id in (1,2,3)
<select id="selectUserByListId" parameterType="com.te.vo.UserPo" resultType="com.te.po.User">
select * from user
<where>
<!--
collection:指定输入对象中的集合属性
item:每次遍历生成的对象
open:开始遍历时的拼接字符串
close:结束时拼接的字符串
separator:遍历对象之间需要拼接的字符串
select * from user where 1=1 and id in (1,2,3)
-->
<foreach collection="ids" item="id" open="and id in (" close=") " separator=",">
#{id}
</foreach>
</where>
</select>