以学生表student
为例:java
id | name | age | score |
---|---|---|---|
1 | zhangsan | 11 | 60 |
2 | lisi | 22 | 60 |
3 | wangwu | 33 | 90 |
4 | zhaoliu | 14 | 59 |
<?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="com.lusifer.mybatis.dao.DynamicStudentDao"> <!-- if --> <select id="selectByIf" resultType="com.lusifer.mybatis.entity.Student"> SELECT id, name, age, score FROM student WHERE 1 = 1 <if test="name != null and name != ''"> AND name LIKE concat('%', #{name}, '%') </if> <if test="age != null and age > 0"> AND age > #{age} </if> </select> </mapper>
说明:sql
为了解决两个条件均未作设定的状况,在 where
后添加了一个“1=1”
的条件。这样就不至于两个条件均未设定而出现只剩下一个 where,而没有任何可拼接的条件的不完整 SQL 语句。数组
<!-- where--> <select id="selectByWhere" resultType="com.lusifer.mybatis.entity.Student"> SELECT id, name, age, score FROM student <where> <if test="name != null and name != ''"> AND name LIKE concat('%', #{name}, '%') </if> <if test="age != null and age > 0"> AND age > #{age} </if> </where> </select>
说明:mybatis
<if/>
标签的中存在一个比较麻烦的地方:须要在 where
后手工添加 1=1
的子句。由于,若 where
后的全部 <if/>
条件均为 false,而where
后若又没有 1=1
子句,则 SQL 中就会只剩下一个空的 where
,SQL 出错。因此,在 where
后,须要添加永为真子句1=1
,以防止这种状况的发生。但当数据量很大时,会严重影响查询效率。可是<where>
语句则避免这一麻烦的发生。app
<!-- choose --> <select id="selectByChoose" resultType="com.lusifer.mybatis.entity.Student"> SELECT id, name, age, score FROM student <where> <choose> <when test="name != null and name != ''"> AND name LIKE concat('%', #{name}, '%') </when> <when test="age != null and age > 0"> AND age > #{age} </when> <otherwise> AND 1 != 1 </otherwise> </choose> </where> </select>
说明:spa
该标签中只能够包含 <when/>
<otherwise/>
,能够包含多个 <when/>
与一个 <otherwise/>
。它们联合使用,完成 Java 中的开关语句 switch
..case
功能。3d
本例要完成的需求是,若姓名不空,则按照姓名查询;若姓名为空,则按照年龄查询;若没有查询条件,则没有查询结果。code
注意:动态 SQL 的判断中使用的都是 OGNL 表达式。OGNL 表达式中的数组使用 array
表示,数组长度使用 array.length
表示。xml
<!-- foreach --> <select id="selectByForeach" resultType="com.lusifer.mybatis.entity.Student"> <!-- select * from student where id in (2, 4) --> SELECT id, name, age, score FROM student <if test="array != null and array.length > 0"> WHERE id IN <foreach collection="array" open="(" close=")" item="id" separator=","> #{id} </foreach> </if> </select>
说明:blog
<foreach/>
标签用于实现对于数组与集合的遍历。对其使用,须要注意:
collection 表示要遍历的集合类型,这里是数组,即 array。
open、close、separator 为对遍历内容的 SQL 拼接。
本例实现的需求是,查询出 id 为 2 与 4 的学生信息。
注:遍历集合的方式与遍历数组的方式相同,只不过是将 array 替换成了 list
定义接口
/** * 使用 foreach 标签以 list 基本类型的形式查询 * @param ids * @return */ public List<Student> selectByForeachWithListBase(List<Long> ids);
SQL
<!-- foreach --> <select id="selectByForeachWithListBase" resultType="com.lusifer.mybatis.entity.Student"> <!-- select * from student where id in (2, 4) --> SELECT id, name, age, score FROM student <if test="list != null and list.size > 0"> WHERE id IN <foreach collection="list" open="(" close=")" item="id" separator=","> #{id} </foreach> </if> </select>
定义接口
/** * 使用 foreach 标签以 list 自定义类型的形式查询 * @param students * @return */ public List<Student> selectByForeachWithListCustom(List<Student> students);
SQL
<!-- foreach --> <select id="selectByForeachWithListCustom" resultType="com.lusifer.mybatis.entity.Student"> <!-- select * from student where id in (2, 4) --> SELECT id, name, age, score FROM student <if test="list != null and list.size > 0"> WHERE id IN <foreach collection="list" open="(" close=")" item="student" separator=","> #{student.id} </foreach> </if> </select>
<sql/>
标签用于定义 SQL 片段,以便其它 SQL 标签复用。而其它标签使用该 SQL 片段, 须要使用 <include/>
子标签。该 <sql/> 标签能够定义 SQL 语句中的任何部分,因此 <include/>
子标签能够放在动态 SQL 的任何位置。
<sql id="select"> SELECT id, name, age, score FROM student </sql>
<!-- foreach --> <select id="selectByForeachWithListCustom" resultType="com.lusifer.mybatis.entity.Student"> <!-- select * from student where id in (2, 4) --> <include refid="select" /> <if test="list != null and list.size > 0"> WHERE id IN <foreach collection="list" open="(" close=")" item="student" separator=","> #{student.id} </foreach> </if> </select>