<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mappernamespace="com.lusifer.mybatis.dao.DynamicStudentDao"> <!-- if --> <selectid="selectByIf"resultType="com.lusifer.mybatis.entity.Student"> SELECT id, name, age, score FROM student WHERE 1 = 1 <iftest="name != null and name != ''"> AND name LIKE concat('%', #{name}, '%') </if> <iftest="age != null and age > 0"> AND age > #{age} </if> </select> </mapper>
where 标签
<if/> 标签的中存在一个比较麻烦的地方:需要在 where 后手工添加 1=1 的子句。因为,若 where 后的所有 <if/> 条件均为 false,而 where 后若又没有 1=1 子句,则 SQL 中就会只剩下一个空的 where,SQL 出错。所以,在 where 后,需要添加永为真子句 1=1,以防止这种情况的发生。但当数据量很大时,会严重影响查询效率。
定义映射文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<!-- where--> <selectid="selectByWhere"resultType="com.lusifer.mybatis.entity.Student"> SELECT id, name, age, score FROM student <where> <iftest="name != null and name != ''"> AND name LIKE concat('%', #{name}, '%') </if> <iftest="age != null and age > 0"> AND age > #{age} </if> </where> </select>
<!-- choose --> <selectid="selectByChoose"resultType="com.lusifer.mybatis.entity.Student"> SELECT id, name, age, score FROM student <where> <choose> <whentest="name != null and name != ''"> AND name LIKE concat('%', #{name}, '%') </when> <whentest="age != null and age > 0"> AND age > #{age} </when> <otherwise> AND 1 != 1 </otherwise> </choose> </where> </select>
<!-- foreach --> <selectid="selectByForeach"resultType="com.lusifer.mybatis.entity.Student"> <!-- select * from student where id in (2, 4) --> SELECT id, name, age, score FROM student <iftest="array != null and array.length > 0"> WHERE id IN <foreachcollection="array"open="("close=")"item="id"separator=","> #{id} </foreach> </if> </select>
foreach 标签 – 遍历集合
遍历集合的方式与遍历数组的方式相同,只不过是将 array 替换成了 list
遍历泛型为基本类型的 List
定义 DAO 接口
1 2 3 4 5 6
/** * 使用 foreach 标签以 list 基本类型的形式查询 *@param ids *@return */ public List<Student> selectByForeachWithListBase(List<Long> ids);
定义映射文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<!-- foreach --> <selectid="selectByForeachWithListBase"resultType="com.lusifer.mybatis.entity.Student"> <!-- select * from student where id in (2, 4) --> SELECT id, name, age, score FROM student <iftest="list != null and list.size > 0"> WHERE id IN <foreachcollection="list"open="("close=")"item="id"separator=","> #{id} </foreach> </if> </select>
遍历泛型为自定义类型的 List
定义 DAO 接口
1 2 3 4 5 6
/** * 使用 foreach 标签以 list 自定义类型的形式查询 *@param students *@return */ public List<Student> selectByForeachWithListCustom(List<Student> students);
定义映射文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<!-- foreach --> <selectid="selectByForeachWithListCustom"resultType="com.lusifer.mybatis.entity.Student"> <!-- select * from student where id in (2, 4) --> SELECT id, name, age, score FROM student <iftest="list != null and list.size > 0"> WHERE id IN <foreachcollection="list"open="("close=")"item="student"separator=","> #{student.id} </foreach> </if> </select>
<sql id="select"> SELECT id, name, age, score FROM student </sql>
1 2 3 4 5 6 7 8 9 10 11 12
<!-- foreach --> <selectid="selectByForeachWithListCustom"resultType="com.lusifer.mybatis.entity.Student"> <!-- select * from student where id in (2, 4) --> <includerefid="select" />
<iftest="list != null and list.size > 0"> WHERE id IN <foreachcollection="list"open="("close=")"item="student"separator=","> #{student.id} </foreach> </if> </select>
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !