目录
一、if 元素
当未传递任何参数时,程序会将数据表中的所有数据查出。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="对应的 mapper 类的名字">
<!-- if 元素的使用 -->
<select id="find" parameterType="" resultType="">
select * from t_customer where 1=1
<if test="username != null and username != ''">
and username like concat('%',#{username},'%')
</if>
<if test="jobs != null and jobs != ''">
and jobs = #{jobs}
</if>
</select>
</mapper>
二、choose、when、otherwise 元素
案例场景:
当客户名称不为空,则只根据客户名称进行客户筛选;
当客户名称为空,而客户职业不为空,则只根据客户职业进行客户筛选;
当客户名称和客户职业都为空,则要求查询出所有电话不为空的客户信息。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="对应的 mapper 类的名字">
<select id="find" parameterType="" resultType="">
select * from t_customer where 1=1
<choose>
<when test="username != null and username != ''">
and username like concat('%',#{username},'%')
</when>
<when test="jobs != null and jobs != ''">
and jobs = #{jobs}
</when>
<otherwise>
and phone is not null
</otherwise>
</choose>
</select>
</mapper>
三、where、trim 元素
用 <where> 元素替代" where 1=1 "。只有 <where> 元素内的条件成立时,才会在拼接SQL中加入 where 关键字,否则将不会添加。即使 where 之后的内容有多余的 "AND" 或 "OR",<where> 元素也会自动将它们去除。
直接改写博客的第一段代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="对应的 mapper 类的名字">
<!-- if 元素的使用 -->
<select id="find" parameterType="" resultType="">
select * from t_customer
<where>
<if test="username != null and username != ''">
and username like concat('%',#{username},'%')
</if>
<if test="jobs != null and jobs != ''">
and jobs = #{jobs}
</if>
</where>
</select>
</mapper>
<trim> 元素的作用是去除一些特殊的字符串,它的 prefix 属性代表的是语句的前缀(这里使用 where 来连接后面的 SQL 片段),而 prefixOverrides 属性代表的是需要去除的那些特殊字符串(这里定义了要去除SQL中的 and)。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="对应的 mapper 类的名字">
<!-- if 元素的使用 -->
<select id="find" parameterType="" resultType="">
select * from t_customer
<trim prefix="where" prefixOverrides="and">
<if test="username != null and username != ''">
and username like concat('%',#{username},'%')
</if>
<if test="jobs != null and jobs != ''">
and jobs = #{jobs}
</if>
</trim>
</select>
</mapper>
四、set 元素
让程序只更新需要更新的字段。
<set> 元素会动态前置 SET 关键字,同时也会消除 SQL 语句中最后一个多余的逗号,<if> 元素用于判断相应的字段是否传入值,如果传入的更新字段非空,就将此字段进行动态SQL组装,并更新此字段,否则字段不执行更新。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="对应的 mapper 类的名字">
<update id="update" parameterType="">
update t_customer
<set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="jobs != null and jobs != ''">
jobs = #{jobs},
</if>
<if test="phone != null and phone != ''">
phone = #{phone},
</if>
</set>
where id = #{id}
</update>
</mapper>
注意:
在映射文件中使用 <set> 和 <if> 元素组合进行 update 语句动态 SQL 组装时,如果 <set> 元素内包含的内容都为空,则会出现 SQL 语法错误,所以在使用 <set> 元素进行字段信息更新时,要确保传入的更新不能都为空。
五、foreach 元素
如果将 id 值小于 100 的客户信息全部查询出来???
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="对应的 mapper 类的名字">
<select id="findByIds" parameterType="List" resultType="" >
select * from t_customer where id in
<foreach item="id" index="index" collection="list"
open="(" separator="," close=")">
#{id}
</foreach>
</select>
</mapper>
- item:配置的是循环中当前的元素(集合中泛型的变量名)
- index:配置的是当前元素在集合的位置下标
- collection:配置的 list 是传递过来的参数类型(首字母小写),它可以是一个 array、list(或collection)、Map集合的键、POJO包装类中数组或集合类型的属性名等。(指的是我们需要操作的集合)
- open 和 close:配置的是以什么符号将这些集合元素包装起来
- separator:配置的是各个元素的间隔符
在使用 <foreach> 时最关键也是最容易出错的就是 collection 属性,该属性是必须制定指定的,而且在不同情况下,该属性的值是不一样的。主要有以下 3 种情况。
(1)如果传入的是单参数且参数类型是一个数组或者 List 的时候,collection 属性值分别为 array 和 list(或 collection )
(2)如果传入的参数时多个的时候,就需要把它们封装成一个 Map 了,当然单参数也可以封装成 Map 集合,这时候 collection 属性值就为 Map 的键
(3)如果传入的参数是 POJO 包装类的时候, collection 属性值就为该包装类中需要进行遍历的数组或集合的属性名。