动态SQL

什么是动态SQL?

  动态SQL就是指根据不同条件生成不同的sql语句。

动态SQL元素和JSTL或基于类似XML的文本处理器相似。
在Mybatis之前的版本中,有很多元素需要花时间了解。
Mybatis3大大简化了元素种类,现在只需学习原来一半的元素即可。
Mybatis采用功能强大的基于OGNL的表达式来淘汰其它大部分元素。
if
choose(when,otherwise)
trim(where,set)
foreach

动态SQL之if标签

  数据库表结果如下:
在这里插入图片描述
在这里插入图片描述
查询语句如下:

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

动态SQL之where标签

  where 1=1在实际开发当中使用是不合规范的,建议使用where标签:

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

动态SQL之choose(when,otherwise)标签

<select id="queryBlogChoose" parameterType="map" resultType="Blog">
        select * from 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>

动态SQL之set标签

   <update id="updateBlog" parameterType="map">
        update blog
        <set>
            <if test="title !=null">
                title = #{title},
            </if>
            <if test="author !=null">
                author = #{author},
            </if>
        </set>
        where id = #{id}
    </update>

SQL片段

  有的时候,我们可能会将某些sql语句的部分功能抽取出来,方便复用!

  1. 使用sql标签抽取公共部分;
    <sql id ="if_title-author">
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </sql>
  • 在需要使用的地方使用include标签引用即可。
    <select id="queryBlogIF" parameterType="map" resultType="Blog">
        select * from blog
        <where>
           <include refid="if_title-author"/>
        </where>
    </select>

提取sql片段注意事项:

  • 最好基于单表来定义SQL片段;
  • 提取的片段不要包含where标签内容。

动态SQL之foreach标签

<select id="queryBlogForeach" parameterType="map" resultType="Blog">
        select * from blog
        <where>
            <foreach collection="authors" item="Author" open="and (" close=")" separator="or">
                author = #{Author}
            </foreach>
        </where>
 </select>
@org.junit.Test
    public void testQueryBlogForeach(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        Map map = new HashMap();
        ArrayList<String> authors = new ArrayList<>();
        authors.add("cyj2");
        authors.add("hpf");
        map.put("authors",authors);
        List<Blog> blogs = sqlSession.getMapper(BlogMapper.class).queryBlogForeach(map);
        for(Blog blog : blogs)
            System.out.println(blog.toString());
        sqlSession.close();
    }

总结:
  我们可以简单理解动态SQL语句:动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性,按照SQL语句的格式去排列组合就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值