Dynamic sql of mybatis - if, where, foreach, choose, set

Purpose

Simplify the string operation of sql statements.

if statement

    <select id="findAllEmp"  resultType="com.xzy.bean.EmpInfo">
        select * from  emp where
        /*test="" 编写判断条件,
        id!=null :取出Javabean属性当中的id值,判断是否为空
        */
        <if test="id!=null">
            id > #{id} and
        </if>
        /*判断属性是否为空*/
        <if test="name!=null">
            name like #{name} and
        </if>
    </select>

Note:
If the last condition is not met by using the if statement, there will be many ands. At this time, the sql statement is incorrect.
Also pay attention to the escape of special characters in xml.

where tag

    <select id="findAllEmp"  resultType="com.xzy.bean.EmpInfo">
        select * from  emp
        /*where标签可以帮我们自动去除掉前面的and*/
        <where>
            /*test="" 编写判断条件,
            id!=null :取出Javabean属性当中的id值,判断是否为空
            */
            <if test="id!=null">
                id > #{id}
            </if>
            /*判断属性是否为空*/
            <if test="name!=null">
                and  name like #{name}
            </if>
        </where>

    </select>

The where tag can help us automatically remove the redundant and in front of the SQL after the condition is met.

trim intercepts a string

prefix="": add a prefix to the following sql as a whole
prefixOverrides="": remove the extra characters in front of the sql statement
suffix="": add a suffix to the whole
suffixOverrides="": you can remove the extra characters behind

    <select id="findAllEmp" resultType="com.xzy.bean.EmpInfo">
        select * from emp
        /*trim:截取字符串
        prefix=""           :为下面的sql整体添加一个前缀
        prefixOverrides=""  :去除sql语句中前面多余的字符
        suffix=""           :为整体添加一个后缀
        suffixOverrides=""  :可以去掉后面多余的字符
        */
        <trim prefix="where" prefixOverrides="and" suffixOverrides="and">
            /*test="" 编写判断条件,
            id!=null :取出Javabean属性当中的id值,判断是否为空
            */
            <if test="id!=null">
                id > #{id}
            </if>
            /*判断属性是否为空*/
            <if test="name!=null">
                and name like #{name}
            </if>
        </trim>
 </select>

foreach traverses the collection

    <select id="findAllEmp" resultType="com.xzy.bean.EmpInfo">
        select * from emp where id in
        /*
        collection="" :指定要遍历的集合
        close="" :以什么结束
        open="" :以什么结束
        index="" :如果遍历的是list,index保存当前遍历元素的索引
        如果是map,index保存到就是key
        item="" :遍历取出的元素,如果是map,取出的就是value
        separator="" 以什么分割
        */
        <foreach collection="list" close=")" index="" item="id_item" open="(" separator=",">
            #{id_item}
        </foreach>
    </select>

choose label

    <select id="findAllEmp" resultType="com.xzy.bean.EmpInfo">
        select * from emp
        <where>
            /*
            满足一个条件之后就直接拼接sql语句,相当于分支选择
            */
            <choose>
                <when test=""></when>
                <when test=""></when>
                <when test=""></when>
                <otherwise></otherwise>
            </choose>
        </where>
    </select>

set tag dynamic update

Will automatically remove extra commas, mainly for dynamic updates.

    <update id="updateEmp">
        UPDATE emp SET
        <set>
            <if test="id!=null" >
                    id=#{id},
            </if>
            <if test="name!=null" >
                name=#{name},
            </if>
            <if test="age!=null" >
                age=#{age},
            </if>
        </set>
    </update>

Guess you like

Origin blog.csdn.net/weixin_42643321/article/details/107825639