MyBatis动态sql语句

在映射文件中添加动态sql语句:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="为接口所在地方的名称">
         动态sql语句
</mapper>

1:if标签:用于判断条件是否成立,成立即执行,不成立即不执行属性test里放判断条件

2:if+where标签:“where”标签如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以and 或or 开头的,则它会去掉。

<select id="getUsers" resultType="com.zhiyou.zt.bean.Users">
        select 
        *
         from Users
        <where>
            <if test="name!=null">
                 and name=#{name}
            </if>
            <if test="age>0">
                 and age=#{age}
            </if>
        </where>
    </select>

3:if+set标签:这个“set”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘set’。此外,如果标签返回的内容是以,结尾的,则它会剔除掉,一般用于修改操作

<update id="updateUsers">
        update Users
        <set>
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="age>0">
                age=#{age},
            </if>
        </set>
        where id=#{id}
    </update>

4:choose+when+otherwise:有时候,我们用到的查询条件只想选择其中的一个,使用 choose 标签可以解决此类问题,类似于switch 语句

<select id="selectById" resultType="com.zhiyou.zt.bean.Users">
        select * from Users
        <where>
            <choose>
                <when test="name!=null">
                    and name=#{name}
                </when>
                <when test="age>0">
                    and age=#{age}
                </when>
                <otherwise>
                    and id=#{id}
                </otherwise>
            </choose>
        </where>
    </select>

5:trim标记是一个格式化的标记,可以在首位置或末位置添加字段,可以完成where和set功能,prefix:首位置添加;prefixOverrides:首位置覆盖;suffix:末位置添加;suffixOverrides:末位置覆盖

<select id="selectById" resultType="com.zhiyou.zt.bean.Users">
        select * from Users
        <trim prefix="where“ prefixOverrides="and | or">
            <choose>
                <when test="name!=null">
                    and name=#{name}
                </when>
                <when test="age>0">
                    and age=#{age}
                </when>
                <otherwise>
                    and id=#{id}
                </otherwise>
            </choose>
        </trim>
    </select>

 

<select id="selectById" resultType="com.zhiyou.zt.bean.Users">
        select * from Users
        <trim prefix="set" suffixOverrides=",">
            <choose>
                <when test="name!=null">
                    and name=#{name}
                </when>
                <when test="age>0">
                    and age=#{age}
                </when>
                <otherwise>
                    and id=#{id}
                </otherwise>
            </choose>
        </trim>
    </select>

6:sql片段,将抽出来的片段重复利用,sql标签写入抽取出来的片段,在需要的地方用include标签调用即可

<sql id="mysql">
    id,name,age
</sql>
    <select id="getUsers" resultType="com.zhiyou.zt.bean.Users">
        select 
        <include refid="mysql"></include>
         from Users
        <where>
            <if test="name!=null">
                 and name=#{name}
            </if>
            <if test="age>0">
                 and age=#{age}
            </if>
        </where>
    </select>

7:foreach:遍历sql语句的字段。collection:需要遍历的集合名(重定义后的名字)open:开头的拼接字符close:结束的拼接字符separator:遍历之间的分隔符

<select id="selectIds" resultType="com.zhiyou.zt.bean.Users">
        select <include refid="mysql"></include> from Users
        <where>
            <if test="ids!= null and ids.size() > 0" >
            <foreach collection="ids" open="id in (" close=")" separator="," item="id">
            #{id}
            </foreach>
            </if>
        </where>
    </select>

 

posted @ 2019-08-31 17:20  涮锅的小龙虾  阅读(174)  评论(0编辑  收藏  举报