动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦
根据 stu_name 和 stu_sex 来查询数据。如果stu_name为空,那么将只根据stu_sex来查询;反之只根据stu_name来查询
首先不使用 动态SQL 来书写
- <select id="selectStudent" resultType="student" parameterType="cn.kgc.mybatis.pojo.Student">
- select * from user where stu_name=#{stu_name} and stu_sex=#{stu_sex}
- select>
可以发现,如果 #{stu_name} 为空,那么查询结果也是空,如何解决这个问题呢?使用 if 来判断
- <select id="selectStudent" resultType="student" parameterType="cn.kgc.mybatis.pojo.Student">
- select * from student where
- <if test="stu_name != null">
- stu_name=#{stu_name}
- if>
- <if test="stu_sex!= null">
- and stu_sex=#{stu_sex}
- if>
- select>
这样写我们可以看到,如果 sex 等于 null,那么查询语句为 select * from student where stu_name=#{stu_name},但是如果stu_name 为空呢?那么查询语句为 select * from user where and stu_sex=#{sex},这是错误的 SQL 语句,如何解决呢?
- <select id="selectStudent" resultType="student" parameterType="cn.kgc.mybatis.pojo.Student">
- select * from student
- <where>
- <if test="stu_name != null">
- stu_name=#{stu_name}
- if>
- <if test="stu_sex!= null">
- and stu_sex=#{stu_sex}
- if>
- where>
- select>
这个where标签会知道如果它包含的标签中有返回值的话,它就插入一个where。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉
上面的对于查询 SQL 语句包含 where 关键字,如果在进行更新操作的时候,含有 set 关键词
- <update id="updateStudentById" parameterType="cn.kgc.mybatis.pojo.Student">
- update student stu
- <set>
- <if test="stuname != null and stuname != ''">
- stu.stu_name = #{stuName},
- if>
- <if test="stusex != null and stusex != ''">
- stu.stu_sex = #{stuSex}
- if>
- set>
- where id=#{id}
- update>
如果第一个条件 username 为空,那么 sql 语句为:update user u set u.sex=? where id=?
如果第一个条件不为空,那么 sql 语句为:update user u set u.username = ? ,u.sex = ? where id=?
有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java 的 switch 语句
- <select id="selectStudentByChoose" resultType="com.ys.po.User" parameterType="com.ys.po.User">
- select * from student
- <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=?
trim标记是一个格式化的标记,可以完成set或者是where标记的功能
用 trim 改写上面的 if+where 语句
- <select id="selectStudent" resultType="student" parameterType="cn.kgc.mybatis.pojo.Student">
- select * from user
- <trim prefix="where" prefixOverrides=" and | or">
- <if test="stu_name != null">
- stu_name=#{stu_name}
- if>
- <if test="stu_sex!= null">
- and stu_sex=#{stu_sex}
- if>
- trim>
- select>
prefix:前缀
prefixoverride:去掉第一个and或者是or
用 trim 改写上面的 if+set 语句
- <update id="updateStudentById" parameterType="cn.kgc.mybatis.pojo.Student">
- update student stu
- <trim prefix="set" suffixOverrides=",">
- <if test="stuname != null and stuname != ''">
- stu.stu_name = #{stuName},
- if>
- <if test="stusex != null and stusex != ''">
- stu.stu_sex = #{stuSex}
- if>
- trim>
- where id=#{id}
- update>
suffix:后缀
suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
需求:我们需要查询 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)
用 foreach 来改写 select * from user where id=1 or id=2 or id=3
- <select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
- select * from user
- <where>
-
- <foreach collection="ids" item="id" separator="or">
- id=#{id}
- foreach>
- where>
- select>
用 foreach 来改写 select * from user where id in (1,2,3)
有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。
假如我们需要经常根据用户名和性别来进行联合查询,那么我们就把这个代码抽取出来,如下:
- <sql id="selectUserByUserNameAndSexSQL">
- <if test="username != null and username != ''">
- AND username = #{username}
- if>
- <if test="sex != null and sex != ''">
- AND sex = #{sex}
- if>
- sql>
- <select id="selectUserByUsernameAndSex" resultType="user" parameterType="com.ys.po.User">
- select * from user
- <trim prefix="where" prefixOverrides="and | or">
-
- <include refid="selectUserByUserNameAndSexSQL">include>
-
- trim>
- select>
①、最好基于 单表来定义 sql 片段,提高片段的可重用性
②、在 sql 片段中最好不要包括 where