Java教程

mybatis05

本文主要是介绍mybatis05,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

动态SQL

1、条件查询

<select id="getBlogIf" parameterType="Map" resultType="Blog">
        select * from mybatis.blog where 1=1
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author =#{author}
        </if>
 </select>

如果有条件,会直接拼接在后面,实现动态,即使一个条件也没有,也有where 1=1

2、优化<where>标签

<select id="getBlogChose" parameterType="Map" resultType="Blog">
        select * from mybatis.blog
        <where>
            <if test="title != null">
                 title = #{title}
            </if>
            <if test="author != null">
                and author =#{author}
            </if>
        </where>

</select>

将where 1=1 去掉,加上<where>标签,他能够智能的决定加不加“and”

3、<choose>

<select id="getBlogChose" 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>

choose标签的作用与switch差不多,选择,但是有顺序,如果第一个、第二个条件满足,只会走第一个条件

本质是个trim

<trim prefix="WHERE" prefixOverrides="AND |OR ">
    ... 
</trim>

set同理

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

要持续学习的是:

        mysql引擎

        InnoDB底层原理

        索引

        索引优化

这篇关于mybatis05的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!