MyBatis中的动态SQL

  • Post author:
  • Post category:其他





MyBatis

中的动态

SQL


MyBatis

中的动态

sql

主要包含如下几种元素:

if



choose



when



otherwise



trim



where



set

以及

foreach

几种,我们下面分别来看看这几种




if

元素


if



MyBatis

动态

sql

中的判断元素,这个有点类似于

Java

中的

if

语句,不同的是这里的

if

一般常常和

test

配合使用

<select id="getUser" resultMap="u" parameterType="String">
	select * from user2
	<if test="address!=null and address !=''">
		WHERE address LIKE concat('%',#{address},'%')
	</if>
</select>

  • 当用户传入的

    address

    不为

    null

    或空字符串的时候,我就加上一个

    where

    条件,否则就什么条件都不加入




choose

元素


choose

有点类似于

Java

中的

switch

,常常配合

when



otherwise

一起来使用

<select id="getUser2" resultMap="u">
	SELECT * FROM user2 WHERE 1=1
	<choose>
		<when test="id!=null">
			AND id = #{id}
		</when>
        <when test="address!=null">
            AND address = #{address}
        </when>
        <when test="username!=null">
            AND user_name LIKE concat(#{username},'%')
        </when>
        <otherwise>
            AND 10 > id
        </otherwise>
	</choose>
</select>

  • 在查询条件中,如果用户传来了

    id

    ,那么我就查询该

    id

    的数据,如果用户传来了

    address

    ,那么我就我们添加

    address

    的查询条件,如果用户传来了

    username

    ,那么我就添加

    username

    的查询条件,最后如果用户任何一个查询条件都没有添加进来,那么默认查询条件就是查询

    id

    小于

    10

    的所有数据




where

元素

在上面的案例中发现了一个问题,就是我们在添加查询条件的时候,在查询条件之前都先添加了

where 1 = 1

,然后后面直接在这之后再追加

and

什么什么的,那么每次这样来写显然有点麻烦,有没有简单一点的方案呢?当然有,我们可以通过

where

元素

<select id="getUser3" resultMap="u">
	SELECT * FROM user2
	<where>
		<choose>
			<when test="id!=null">
				AND id = #{id}
            </when>
            <when test="address!=null">
                AND address = #{address}
            </when>
            <when test="username!=null">
                AND user_name LIKE concat(#{username},'%')
            </when>
            <otherwise>
                AND 10 > id
            </otherwise>
		</choose>
	</where>
</select>

这样,只有

where

元素中有条件成立,才会将

where

关键字组装到

sql

中,这样就比前一种方式简单许多




trim

元素


trim

有点元素替换的意思,还是上面的案例,我们可以将

and

替换为

where

<select id="getUser4" resultMap="u">
	SELECT * FROM user2
    <trim prefix="where" prefixOverrides="and">
    	AND id = 1
	</trim>
</select>

这个最终执行的

sql

SELECT * FROM user2 where id = 1




set

元素


set

是我们在更新表的时候使用的元素,通过

set

元素,我们可以逐字段的修改一条数据

<update id="update">
	UPDATE user2
	<set>
		<if test="username!=null">
			user_name = #{username},
        </if>
        <if test="password!=null">
            password = #{password}
        </if>
    </set>
    WHERE id = #{id}
</update>



set

元素中,如果遇到了逗号,系统会自动将之去除




foreach

元素


foreach

元素用来遍历集合。

foreach

标签的属性主要有

item,index,collection,open,separator,close


  • collection

    :传入的参数中集合的名称,

    List

    对象默认用

    list

    代替作为键;数组对象有

    array

    代替作为键;

    Map

    对象没有默认的键。该属性是必须指定的

  • item

    :集合中元素迭代时的别名,该参数为必选

  • index

    :在

    list

    和数组中

    index

    是元素的序号;在

    map



    index

    是元素的

    key

    ,该参数可选

  • open

    :开始符号,一般是和

    close=")"

    合用。该参数可选

  • separator

    :元素之间的分隔符,

    separator=","

    会自动在元素中间用

    ,

    隔开,避免手动输入逗号导致

    sql

    错误。该参数可选

  • close

    :关闭符号,一般是和

    open="("

    合用。该参数可选



传入的参数为

List

<select id="selectByIds" resultType="com.olive.pojo.User">
	select * from t_user where id in
	<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
		#{item}
	</foreach>
</select>



传入的参数为

Array

<select id="selectByIds" resultType="com.olive.pojo.User">
	select * from t_user where id in
	<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
		#{item}
	</foreach>
</select>   



传入的参数为

Map

<select id="selectByIds" resultType="com.olive.pojo.User">
	select * from t_user where  id in
	<foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
		#{item}
	</foreach>
</select>


collection

的值

userIds

是存储在

map

中的

key

,比如:

map.put("userIds",userIds); 




bind

元素

使用

bind

元素我们可以预先定义一些变量,然后在查询语句中使用

<select id="getUserByName" resultMap="u">
	<bind name="un" value="username+'%'"></bind>
    SELECT* FROM user2 WHERE user_name LIKE #{un}
</select>



版权声明:本文为weixin_38192427原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。