美文网首页
七 . 动态sql-trim、where、set(mybatis

七 . 动态sql-trim、where、set(mybatis

作者: 任未然 | 来源:发表于2019-09-29 20:07 被阅读0次

一、choose(when,otherwise)

1、说明

choose元素的作用相当于JAVA中的Switch语句,基本用法和JSTL中使用一样,和otherwist搭配使用。

2、语法结构

<select..>
SQL语句1
<choose>
<when test="条件表达式">
    SQL语句2
</when>
<otherwise>
    SQL语句3
</otherwise>
</choose>
</select>

3、栗子

    <resultMap id="BaseResultMap" type="User">
        <id column="uid" jdbcType="INTEGER" property="uid" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="password" jdbcType="VARCHAR" property="password" />
        <result column="phone" jdbcType="VARCHAR" property="phone" />
        <result column="email" jdbcType="VARCHAR" property="email" />
        <result column="join_date" jdbcType="TIMESTAMP" property="joinDate" />
        <result column="login_date" jdbcType="TIMESTAMP" property="loginDate" />
        <result column="status" jdbcType="TINYINT" property="status" />
    </resultMap>
    <sql id="Base_Column_List">
        `uid`, `name`, `password`, phone, email, join_date, login_date, `status`
    </sql>

    <select id="findUserChoose" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM user u
        <where>
            <choose>
                <when test="user.name != null and user.name != '' ">
                    u.name LIKE CONCAT(CONCAT('%', #{user.name}),'%')
                </when>
                <when test="user.phone != null and user.phone != '' ">
                    AND u.phone = #{user.phone}
                </when>
                <when test="user.email != null and user.email != ''">
                    AND u.email = #{user.name,jdbcType=VARCHAR}
                </when>
                <otherwise>
                </otherwise>
            </choose>
        </where>
    </select>

二、where

1、说明

根据条件判断添加where后面的筛选条件

2、语法结构

<where> 
  ....
</where>

3、栗子

int findUserByWhere(@Param("name") String name, @Param("phone") String phone);
<select id="findUserByWhere" resultType="User">
  SELECT * FROM user
  <where>
    <if test="name != null and name != ''">
      AND name LIKE concat('%', #{name}, '%')
    </if>
    <if test="phone !=null and phone !=''">
      OR phone=#{phone}
    </if>
  </where>
</select>

三、set(更新专用)

1、说明

set元素主要是用在更新操作的时候,它的主要功能和where元素相似,主要是在set元素所在位置输出一个set关键字,而且还可以去除内容结尾中无关的逗号。

2、语法结构

<update...>
 update 表
 <set>
    动态追加更新字段
    <if test="条件判断"></if>
    <if test="条件判断"></if>
    <if test="条件判断"></if>
    ...
 </set>
    ...
</update>

3、栗子

int updateUser(@Param("user") User user);
<update id="updateUser">
        UPDATE user
        <set>
            <if test="user.email != null and user.email != ''">email=#{user.email},</if>
            <if test="user.phone != null and user.phone != ''">phone=#{user.phone}</if>
        </set>
        WHERE uid=#{user.uid}
    </update>

三、trim

1、说明

可以在自己包含的内容前加上某前缀,也可以在其后加上某些后缀,预制对应的属性是prefix和suffix;
可以把包含内容的首部某些内容过滤,即忽略,也可以把尾部的某些内容过滤,对应的属性是prxfixOverrides和suffixOverridex;
正因为trim有上述功能,所有我们也可以非常简单的利用trim里代替where和set元素的功能

2、语法结构

<trim prefix="WHERE" prefixOverrides="AND|OR">
...
</trim>
<!-等价于set元素>
<trim prefix="SET" prefixOverrides=",">
...
</trim>

属性

属性 说明
prefix 前缀
suffix 后缀
prefixoverride 去掉第一个“and”或者是 "or"
suffixoverride: 去掉最后标记的字符 如" ,"

3、栗子

查询

User findUserByTrimWhere(@Param("name") String name, @Param("phone") String phone);
    <select id="findUserByTrimWhere" resultMap="BaseResultMap">
        SELECT * FROM user
        <trim prefix="WHERE" prefixOverrides="AND | OR">
            <if test="name != null and name != '' ">
                AND NAME LIKE concat('%', #{name}, '%')
            </if>
            <if test="phone !=null and phone !=''">
                OR phone=#{phone}
            </if>
        </trim>
    </select>


<!--  SELECT * FROM user WHERE NAME LIKE concat('%', ?, '%')  -->

更新

int updateUserTrim(@Param("user") User user);
<update id="updateUserTrim">
  UPDATE user
  <trim prefix="SET" suffixOverrides=",">
    <if test="user.email != null and user.email != ''">email=#{user.email},</if>
    <if test="user.phone != null and user.phone != ''">phone=#{user.phone}</if>
  </trim>
  WHERE uid =#{user.uid}
</update>

相关文章

网友评论

      本文标题:七 . 动态sql-trim、where、set(mybatis

      本文链接:https://www.haomeiwen.com/subject/nsmxyctx.html