一、if
if是mybatis动态SQL中的判断元素,这个有点类似于Java的if语句,不同的是这里的if一般常和test配合使用。if标签一般用于非空验证,如下例,若id为空,if标签里的代码,将不会执行,反之,则会执行中
<select id="getUser" resultMap="u" parameterType="String">
select * from user
<if test="address!=null and address !=''">
WHERE address LIKE concat('%',#{address},'%')
</if>
</select>
二、set
set是我们在更新表的时候使用的元素,通过set元素,我们可以逐字段的修改一条数据
在set元素中,如果遇到了逗号,系统会自动将之去除
<update id="update">
UPDATE user
<set>
<if test="username!=null">
user_name=#{username},
</if>
<if test="password!=null">
password=#{password}
</if>
</set>
WHERE id=#{id}
</update>
三、if+where
在添加查询条件的时候,经常在查询条件之前都先添加了where 1=1,后面直接在这之后再追加and什么什么的。
这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉
<!-- 演示动态sql-where标签的使用情景 -->
<select id="getUserByWhere" parameterType="user"resultType="com.mark.pojo.User">
SELECT
*
FROM USER
<!-- where会自动加上where同处理多余的and -->
<where>
<!-- if标签的使用 -->
<if test="id != null">
and id = #{id}
</if>
<if test="username != null and username != ''">
and username LIKE '%${username}%'
</if>
</where>
</select>
四、if+set
上面的对于查询 SQL 语句包含 where 关键字,如果在进行更新操作的时候,含有 set 关键词,我们怎么处理呢?
这样写,如果第一个条件 username 为空,那么 sql 语句为:update user u set u.sex=? where id=?
如果第一个条件不为空,那么 sql 语句为:update user u set u.username = ? ,u.sex = ? where id=?
<!-- 根据 id 更新 user 表的数据 -->
<update id="updateUserById" parameterType="com.ys.po.User">
update user u
<set>
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex}
</if>
</set>
where id=#{id}
</update>
五、choose(when,otherwise)
有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题。choose有点类似于Java中的switch,常常配合when和otherwise一起来使用。
在查询条件中,如果用户传来了id,那么我就查询该id的数据,如果用户传来了username,那么我就添加username的查询条件,最后如果用户任何一个查询条件都没有添加进来,那么默认查询条件就是查询sex的所有数据。
如果 id 不为空,那么查询语句为:select * from user where id=?
如果 id 为空,那么看username 是否为空,如果不为空,那么语句为 select * from user where username=?;
如果 username 为空,那么查询语句为 select * from user where sex=?
<select id="selectUserByChoose" resultType="com.ys.po.User" parameterType="com.mark.po.User">
select * from user
<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>
六、trim
trim有点元素替换的意思,还是上面的案例,我们可以将and替换为where
这个最终执行的sql是SELECT * FROM user where id=1
prefix:前缀,prefixoverride:去掉第一个and或者是or
suffix:后缀,suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
<select id="getUser4" resultMap="u">
SELECT * FROM user
<trim prefix="where" prefixOverrides="and">
AND id=1
</trim>
</select>
七、foreach
foreach元素用来遍历集合,比如我想查询多个城市的人,我的sql语句可能是这样SELECT * FROM user WHERE address IN('西安','北京'),我在查询的时候可能只是传入了一个list集合,该集合中有西安和北京两个查询条件,那我如何将这个集合组装成一个sql语句呢?
collection表示传入的参数中集合的名称,index表示是当前元素在集合中的下标,open和close则表示如何将集合中的数据包装起来,separator表示分隔符,item则表示循环时的当前元素。这样一段配置最终组合成的sql就是SELECT * FROM user
WHERE address IN('西安','北京')
<select id="getUserInCities" resultMap="u">
SELECT * FROM user
WHERE address IN
<foreach collection="cities" index="city" open="(" separator="," close=")" item="city">
#{city}
</foreach>
</select>
八、bind
使用bind元素我们可以预先定义一些变量,然后在查询语句中使用
<select id="getUserByName" resultMap="u">
<bind name="un" value="username+'%'"></bind>
SELECT* FROM user2 WHERE user_name LIKE #{un}
</select>
九、SQL 片段
定义共用重复sql片段
<!-- sql片段 定义,id:片段唯一标识 -->
<sql id="user_column">
`id`,
`username`,
`birthday`,
`sex`,
`address`,
`uuid2`
</sql>
使用sql片段
SELECT
<!-- sql片段的使用:include:引入sql片段,refid引入片段id -->
<include refid="user_column" />
FROM USER