3.3.10 动态SQL

十、动态SQL

根据条件的不同, SQL 语句也会随之动态的改变. MyBatis 中,提供了一组标签用于实现动态 SQL.

1. <if>

用于进行条件判断, test 属性用于指定判断条件. 为了拼接条件, 在 SQL 语句后强行添加 1=1 的恒成立条件.

<select id="sel" resultType="user">

select * from t_user where 1=1

<if test="username != null and username != ''">

and username=#{username}

</if>

<if test="password != null and password != ''">

and password=#{password}

</if>

</select>

2. <where>

用于管理 where 子句. 有如下功能:

(1) 如果没有条件, 不会生成 where 关键字

(2) 如果有条件, 会自动添加 where 关键字

(3) 如果第一个条件中有 and, 去除之

<select id="sel" resultType="user">

select * from t_user

<where>

<if test="username != null and username != ''">

and username=#{username}

</if>

<if test="password != null and password != ''">

and password=#{password}

</if>

</where>

</select>

3. <choose><when><otherwise>

这是一套标签, 功能类似于 switch...case...

<select id="sel" resultType="user">

select * from t_user

<where>

<choose>

<when test="username != null and username != ''">

and username = #{username}

</when>

<when test="password != null and password != ''">

and password = #{password}

</when>

<otherwise>

and 1=1

</otherwise>

</choose>

</where>

</select>

4. <set>

用于维护 update 语句中的 set 子句. 功能如下:

(1) 满足条件时, 会自动添加 set 关键字

(2) 会去除 set 子句中多余的逗号

(3) 不满足条件时, 不会生成 set 关键字

int updUser(User user);

<update id="updUser" parameterType="user">

update t_user

<set>

id=#{id}, <!-- 防止所有条件不成立时的语法错误 -->

<if test="username != null and username != ''">

username=#{username},

</if>

<if test="password != null and password != ''">

password=#{password},

</if>

</set>

where id=#{id}

</update>

 

 

5. <trim>

用于在前后添加或删除一些内容

(1) prefix, 在前面添加内容

(2) prefixOverrides, 从前面去除内容

(3) suffix, 向后面添加内容

(4) suffixOverrides, 从后面去除内容

<update id="updUser" parameterType="user">

update t_user

<!--

prefix: 前缀, 向前面添加内容

prefixOverrides: 从前面删除内容

suffix: 后缀, 向后面添加内容

suffixOverrides: 从后面删除内容

-->

<trim prefix="set" prefixOverrides="user" suffix="hahaha"

suffixOverrides=",">

username=#{username},

</trim>

where id=#{id}

</update>

6. <bind>

用于对数据进行再加工, 用于模糊查询

<select id="sel" resultType="user">

select * from t_user

<where>

<if test="username!=null and username!=''">

<bind name="username" value="'%' + username + '%'"/>

and username like #{username}

</if>

</where>

</select>

 

7. <foreach>

用于在 SQL 语句中遍历集合参数, 在 in 查询中使用

(1) collection: 待遍历的集合

(2) open: 设置开始符号

(3) item: 迭代变量

(4) separator: 项目分隔符

(5) close: 设置结束符号

<select id="selIn" parameterType="list" resultType="user">

select * from t_user where id in

<foreach collection="list" open="(" separator="," close=")"

item="item">

#{item}

</foreach>

</select>

List<User> selIn(@Param("list") List<Integer> list);

8. <sql><include>

<sql>于提取 SQL 语句, <include>于引 SQL 语句

<sql id="mySql">

id, username, password

</sql>

<select id="selIn" parameterType="list" resultType="user">

select

<include refid="mySql"/>

from t_user where id in

<foreach collection="list" open="(" separator="," close=")"

item="item">

#{item}

</foreach>

</select>

猜你喜欢

转载自www.cnblogs.com/kendyho/p/10847940.html