基于 mybatis 对一张表进行CRUD操作,一般情况下写的 SQL 语句都比较简单,但是业务如果变得复杂起来。我们需要写复杂的 SQL语句,就需要去拼接,稍不注意哪怕少了个空格,都会导致错误。Mybatis的动态SQL功能能够解决这种问题,也大大提高开发效率。
1. if 标签
作用:它用来判断参数
属性:
test (要判断的条件)
如果满足该条件,则会在 sql 语句中增加 if 标签中的那一段 sql
<!-- 根据条件查询用户信息-->
<select id="findUserByCondition" resultType="User" parameterType="User">
SELECT
id AS userId,
username AS userName,
sex AS userSex,
address AS userAddress,
birthday AS userBirthday
FROM
USER
WHERE
1 = 1
<if test="userName != null and userName != ''">
and username = #{userName}
</if>
<if test="userSex != null userSex != ''">
and sex = #{userSex}
</if>
</select>
2. where 标签
使用where标签时,它上层的sql语句不加where条件关键字
如果where标签内有返回值,则where会自动给sql语句加上where
如果这个标签(返回值)是以and,or等查询关键字开头,则自动去除这些关键字
如果where标签内没有返回值,则不加where条件关键字
<select id="findUserByCondition" resultType="User" parameterType="User">
SELECT
id AS userId,
username AS userName,
sex AS userSex,
address AS userAddress,
birthday AS userBirthday
FROM
USER
<where>
<if test="userName != null and userName != ''">
and username = #{userName}
</if>
<if test="userSex != null and userSex != ''">
and sex = #{userSex}
</if>
</where>
</select>
3. foreach 标签
作用 : 迭代一个集合,一般用于in条件
它里面主要有以下五个属性:
collection: 代表要遍历的集合元素,注意编写时不要写#{}
open: 表示改语句以什么开始,它的值是 (
close: 表示改语句的结束部分,它的值是 )
item:代表遍历集合的每个元素,起个别名
separator: 分割符,它的值是 “,”
<select id="findUserInIds" resultType="User" parameterType="QueryVo">
SELECT
id AS userId,
username AS userName,
sex AS userSex,
address AS userAddress,
birthday AS userBirthday
FROM
USER
<where>
<if test="ids != null and ids.size() > 0">
<foreach collection="ids" open="and id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
4. sql 标签
为了简化开发,把公共的 sql 语句抽取出来,在有需要的地方就引用它。
<!-- 抽取重复的sql语句 -->
<sql id="columns">
SELECT
id AS userId,
username AS userName,
sex AS userSex,
address AS userAddress,
birthday AS userBirthday
FROM
USER
</sql>
注意:标签中的 id 唯一对应标签中的 refid
5. include 标签
<select id="findUserInIds" resultType="User" parameterType="QueryVo">
<!-- 引入抽取的部分 -->
<include refid="columns"></include>
<where>
<if test="ids != null and ids.size() > 0">
<foreach collection="ids" open="and id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
6. choose 标签
如果我们的业务需求,查询条件有一个满足即可,就可以使用 choose 标签可以解决。类似于 Java 的 switch 语句。
<select id="findUserByChoose" resultType="User" parameterType="User">
<include refid="columns"></include>
<where>
<choose>
<when test="userId != null and userId !=''">
and id = #{userId}
</when>
<when test="userName != null and userName !=''">
and username = #{userName}
</when>
<otherwise>
and sex = #{userSex}
</otherwise>
</choose>
</where>
</select>
7. trim 标签
属性:
prefix:前缀。
prefixOverrides:去掉第一个指定内容
<select id="findUserByCondition" resultType="User" parameterType="User">
SELECT
id AS userId,
username AS userName,
sex AS userSex,
address AS userAddress,
birthday AS userBirthday
FROM
USER
<trim prefix="where" prefixOverrides="and | or">
<if test="userName != null and userName != ''">
and username = #{userName}
</if>
<if test="userSex != null and userSex != ''">
and sex = #{userSex}
</if>
</trim>
</select>