MyBatis-3 动态Sql语句

动态Sql语句

需要用到的数据库字段如下:

1.1 if 标签

<!--通常的sql语句写法-->
<select id="selectAll" resultType="com.hw.lb.bean.Users" >
    select * from users where name=#{name} and sex=#{sex} <!--如果#{name}或#{age}为空是,会导致查询的结果也为空-->
</select>
<!--通过if标签可以判断来解决-->
<select id="selectAll" resultType="com.hw.lb.bean.Users">
    select * from users where
    <if tese="name!=null and name!=''">
        name=#{name}
    </if>
    <if test="sex!=null and sex!=''">
        and sex=#{sex}
    </if>
</select>

1.2 if+where 标签

<!--当name=null时通过if标签判断,则会sql语句语法错误(select * from where and sex),所以我们可以用新的if+where标签来进行判断。当第一个满足条件成立时,如果返回值的开头为AND或OR,则会将它们去除,在开头添加where-->
<select id="selectAll" resultType="com.hw.lb.bean.Users" >
    select * from users
    <where>
        <if tese="name!=null and name!=''">
            name=#{name}
        </if>
        <if test="sex!=null and sex!=''">
            and sex=#{sex}
        </if>
    </where>
</select>

1.3 if+set 标签

<!--if+set标签与if+where标签用法相似,set用于更新操作-->
<update id="updateById">
    update users 
    <set>
        <if tese="name!=null and name!=''">
            name=#{name},
        </if>
        <if test="sex!=null and sex!=''">
            sex=#{sex}
        </if>
    </set>
    where id=#{id}
</update>

1.4 choose(when,otherwise) 标签

<!--有时用到的查询条件只想满足一个,则可以使用 choose(类似 java 中的 switch)-->
<select id="findByChoose">
    select * from users
    <where>
        <choose>
               <when tese="name!=null and name!=''">
            name=#{name}
        </when>
        <otherwise test="sex!=null and sex!=''">
            and sex=#{sex}
        </otherwise>
        </choose>
    </where>
</select>

1.5 trim 标签

<!--使用 trim 标签可以完成 where 与 set 标签的功能-->
<select id="selectAll" resultType="com.hw.lb.bean.Users" >
    select * from users
    <trim prefix="where" prefixoverride="and | or">
        <if tese="name!=null and name!=''">
            name=#{name}
        </if>
        <if test="sex!=null and sex!=''">
            and sex=#{sex}
        </if>
    </trim>
</select>

<update id="updateById">
    update users
    <trim prefix="set" suffixoverride=",">
        <if tese="name!=null and name!=''">
            name=#{name},
        </if>
        <if test="sex!=null and sex!=''">
            sex=#{sex}
        </if>
    </set>
    where id=#{id}
</update>
<!--trim标签中的属性:prefix:前缀 prefixoverride:去除前缀 suffix:后缀 suffixoverride:去除后缀-->

1.6动态sql片段

<!--在一个Sql语句查询时要显示许多字段,就造成代码重复,而sql标签可以解决这个问题-->
<sql id="UsersSql">id,name,sex,age</sql>
<select id="selectAll" resultType="com.hw.lb.bean.Users" >
    <!--<inculde/>引用sql标签-->
    select <include refid="UsersSql"/> from users
    <trim prefix="where" prefixoverride="and | or">
        <if tese="name!=null and name!=''">
            name=#{name}
        </if>
        <if test="sex!=null and sex!=''">
            and sex=#{sex}
        </if>
    </trim>
</select>

1.7 foreach 标签

<!--当需要查询id分别为1,2,3的用户时-->
<select id="selectByIds"  resultType="com.hw.lb.bean.Users">
    select * from user
    <where>
    <!--
                collection:指定输入对象中的集合属性
                item:每次遍历生成的对象
                open:开始遍历时的拼接字符串
                close:结束时拼接的字符串
                separator:遍历对象之间需要拼接的字符串
                select * from user where 1=1 and id in (1,2,3)
    -->
        <foreach collection="ids" item="id" open="and id in (" close=") " separator=",">
             #{id}
        </foreach>
    </where>
</select>
<!--UsersMapper.java操作类中方法-->
package com.huawei.lb.dao;
import java.util.List;
import com.huawei.lb.bean.Users;
public interface UsersMapper {
    List<Users> selectByIds(@Param("ids")List<Integr> ids)
}

原文地址:https://www.cnblogs.com/DT-Demo/p/11439153.html