动态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 @   枫叶红时  阅读(290)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示