动态sql常用标签

<where></where>标签的作用就在于,若你拼接的第一条语句前面有and则自动去除and

1,<if title=””></if>

 

    <select id="queryBlogIF" parameterType="Map" resultType="Blog">

        select * from mybatis.blog

        <where>

            <if test="title != null">

                and title = #{title}

            </if>

            <if test="author != null">

                and author = #{author}

            </if>

        </where>

</select>

if判断若满足则拼接

 

2,

<choose>

<when title=””>

</when>

<otherwise title =””>

</ otherwise >

</choose>

 

    <select id="queryBlogChoose" parameterType="Map" resultType="Blog">

        select * from mybatis.blog

        <where>

            <choose>

                <when test="title!=null">

                    title = #{title}

                </when>

                <when test="author!=null">

                    and author = #{author}

                </when>

                <otherwise>

                    and views = #{views}

                </otherwise>

            </choose>

        </where>

</select>

When判断,只会拼接第一条满足的语句,若均不满足,则拼接otherwise语句

 

3,<set></set>标签为sql语句动态添加set关键字,并去除无关的逗号

    <update id="updateBlog" parameterType="Map">

        update mybatis.blog

        <set>

            <if test="title!=null">

                title=#{title}

            </if>

        </set>

        where id=#{id}

</update>

 

posted @ 2022-04-13 20:06  枫叶红时  阅读(315)  评论(0)    收藏  举报