Mybatis中动态SQL语句

一、Mybatis中的动态sql语句
动态sql语句有如下标签:
if
choose (when, otherwise)
trim (where, set)
foreach
1.if元素的使用
示例如下:
    <select id="getOneUseIf" resultType="com.lazy.mybatis.bean.Employee">
        select id,last_name lastName,email from employee
        where
            <if test="id!=null">
                id = #{id} and
            </if>
            <if test="lastName!=null and lastName!=''">
                 last_name = #{lastName} and
            </if>
            <if test="email!=null and email.trim()!=''">
                 email = #{email}
            </if>
    </select>
if元素中只有一个test属性,在该属性内我们使用OGNL表达式进行判断,OGNL表达式的使用同JSTL表达式类似
2.where元素使用
如上if元素的使用中,如果email属性为空,当时id或lastName属性不同时为空则sql会呈现如下情况:
    select id, last_name lastName, email from employee where id = ? and
where元素的主要用来解决上述问题,但是注意此时只能解决语句前端冗余的情况
    <select id="getOneUseIf" resultType="com.lazy.mybatis.bean.Employee">
        select id,last_name lastName,email from employee
        <where>
            <if test="id!=null">
                id = #{id} 
            </if>
            <if test="lastName!=null and lastName!=''">
                and last_name = #{lastName} 
            </if>
            <if test="email!=null and email.trim()!=''">
                and email = #{email}
            </if>
        </where>
    </select>
3.trim元素的使用
sql语句中后面出现了一个多余的and,而where语句无法解决,所以我们使用trim元素来解决这个问题,示例如下:
    <select id="getOneUseIf" resultType="com.lazy.mybatis.bean.Employee">
        select id,last_name lastName,email from employee
        <trim prefix="where" suffixOverrides="and">
            <if test="id!=null">
                id = #{id} and
            </if>
            <if test="lastName!=null and lastName!=''">
                 last_name = #{lastName} and
            </if>
            <if test="email!=null and email.trim()!=''">
                 email = #{email}
            </if>
        </trim>
    </select>
trim元素中:
    prefix属性:表示在trim中语句拼接完成后在语句前拼接一个prefix属性中的值;
    prefixOverrides:属性表示在trim语句拼接完成后将语句前面与prefixOverrides值对应的字符串覆盖;
    suffix属性:表示在trim中语句拼接完成后在语句后拼接一个suffix属性中的值;
    suffixOverrides属性:表示在trim语句拼接完成后将语句后面与suffixOverrides值对应的字符串覆盖;
4.choose(when,otherwise)元素的使用
choose等元素用于分支判断,示例如下:
<!--  choose元素的使用:
        根据一个字段进行查询,如果一个存在就不使用
  -->
<!--    List<Employee> getEmpsUseChoose(Employee employee);-->
    <select id="getEmpsUseChoose" resultType="com.lazy.mybatis.bean.Employee">
        select id, last_name lastName, email from employee
        <where>
            <choose>
                <when test="id != null">
                    id = #{id}
                </when>
                <when test="lastName != null and lastName != ''">
                    last_name like #{lastName}
                </when>
                <when test="email != null and email.trim() != ''">
                    email = #{email}
                </when>
            </choose>
        </where>
    </select>
choose元素中:
    使用when元素来对应不同的情况,test属于用于判断条件;
    使用otherwise来对应除when中情况的所有情况;
    choose元素的使用类似于,java中的if/else if/else语句;
5.set元素的使用
set元素的使用类似于where元素,set元素是用来为了解决update语句中的语句冗余问题,示例如下:
    <update id="updateOneUseSet">
        update employee
        <set>
            <if test="lastName != null">
                last_name = #{lastName},
            </if>
            <if test="email != null">
                email = #{email}
            </if>
        </set>
        where id = #{id}
    </update>
使用set元素结合if元素解决以前不能更新局部的更新记录的问题
6.foreach元素的使用
foreach元素使用类似于JSTL中的C:foreach元素,能够解决批量插入的问题,实例如下:
    <insert id="batchInsertUseForeach">
        insert into employee(last_name,email)
        values
        <foreach collection="list" item="employee" separator=",">
            (#{employee.lastName},#{employee.email})
        </foreach>
    </insert>
foreach元素用来遍历,
collection表示要遍历的集合,其中填写参数的别名,如果是Collection类型中的List类型,别名为list,也可以自己命名;
item表示集合中每个元素的变量名;
separator表示分隔符,用来解决sql语句的冗余问题;
index表示在collection中为list时表示,索引对应的变量名,在collection中为map时,key对应的变量名;
open表示拼接一个开头;
end表示在拼接一个结尾;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值