Mybatis动态SQL

12、动态SQL

动态SQL就是根据不同的条件生成不同的SQL

//如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器,你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。

if
choose (when, otherwise)
trim (where, set)
foreach

12.1、if

测试

    public void queryBlogIF(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
      //  map.put("title","java如此简单");
        map.put("author","maple");
        List<Blog> blogs = mapper.queryBlogIf(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

Mapper.xml

 <select id="queryBlogIf" resultType="Blog" parameterType="map">
        SELECT * FROM blog WHERE 1=1
        <if test="title!=null">
            and title=#{title}
        </if>
        <if test="author!=null">
            and author=#{author}
        </if>
    </select>

接口

    //查询博客
    List<Blog> queryBlogIf(Map map);

12.2、choose (when, otherwise)

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

12.3.trim (where, set)

    <select id="queryBlogIf" resultType="Blog" parameterType="map">
        SELECT * FROM blog
        <where>
        <if test="title!=null">
            title=#{title}
        </if>
        <if test="author!=null">
            and author=#{author}
        </if>
        </where>
    </select>
<update id="updateBlog" parameterType="map">
        UPDATE blog
        <set>
            <if test="title!=null">
                title=#{title},
            </if>
            <if test="author!=null">
                author=#{author}
            </if>
            where id=#{id}
        </set>
    </update>

12.4、SQL片段

有的时候我们会吧一些公共的片段提取出来

  1. 使用SQL标签提取公共部分
  2. 在需要使用的地方使用include即可
    
    <select id="queryBlogIf" resultType="Blog" parameterType="map">
        SELECT * FROM blog
        <where>
            <include refid="if-title-author"></include>
        </where>
    </select>
    <sql id="if-title-author">
        <if test="title!=null">
            title=#{title}
        </if>
        <if test="author!=null">
            and author=#{author}
        </if>
    </sql>

注意事项:

  • 最好基于单表来定义SQL片段
  • 不要存在where标签

12.5、foreach

<!--传递一个map,这个map可以存在一个集合-->
    <select id="queryBlogForeach" resultType="Blog" parameterType="map">
        SELECT * FROM blog
        <where>
            <foreach collection="ids" item="id" open="and (" close=")" separator="or">
                id=#{id}
            </foreach>
        </where>
    </select>
@org.junit.Test
    public void queryBlogForeach(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        ArrayList<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        map.put("ids",ids);
        List<Blog> blogs = mapper.queryBlogForeach(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值