24、动态SQL常用标签

 

注:

24、动态SQL常用标签

 

24、动态SQL常用标签

 

只查title没问题

24、动态SQL常用标签

结果

24、动态SQL常用标签

 

只插入author就有问题了

24、动态SQL常用标签

结果

24、动态SQL常用标签

注end

 

### trim (where,set)

select * from mybatis.blog

<where>

    <if test="title != null">

        title = #{title}

    </if>

    <if test="author != null">

        and author = #{author}

    </if>

</where>

 

24、动态SQL常用标签

测试

这里就很智能的把and去除了

24、动态SQL常用标签

 

 

### choose (when, otherwise)

    <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>

24、动态SQL常用标签

 

测试

24、动态SQL常用标签

结果

24、动态SQL常用标签

 

24、动态SQL常用标签

 

 

24、动态SQL常用标签

 

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

    update mybatis.blog

    <set>

        <if test="title != null">

            title = #{title},

        </if>

        <if test="author != null">

            author = #{author}

        </if>

    </set>

    where id = #{id}

 

24、动态SQL常用标签

 

24、动态SQL常用标签

 

测试

24、动态SQL常用标签

ok

24、动态SQL常用标签

 

 

==**所谓的动态SQL,本质还是SQL语句 , 只是我们可以在SQL层面,去执行一个逻辑代码**==

if

where , set  , choose ,when

 

 

 

 

相关文章: