Mybatis学习17 动态标签之Where,set,trim语句

<select id="queryBlogIf" 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 纯属就是为了拼接SQL语句而加上的,这样好吗,这样不好~~

1. Mybatis–动态SQL之Where语句 where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除,说白了就是Mybatis帮我们智能的拼接SQL语句

1.1 Mapper接口

BlogMapper.java

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

1.2 Mapper.xml

<select id="queryBlogIf" parameterType="map" resultType="Blog">
        select * from mybatis.blog
        <where>
            <if test="title != null">
                title = #{title}
            </if>
            <!--   第二个以后开始要加and 保证sql语句拼接的正确性-->
            <if test="author != null">
                and author = #{author}
            </if>
        </where>
    </select>

1.3 测试

@Test
    public void queryBlogIf04() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map map = new HashMap();
        map.put("author", "天天天");
        List<Blog> blogs = mapper.queryBlogIf(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
    }
            // 关闭sqlSession
        sqlSession.close()
}

结果

@Test
    public void queryBlogIf05() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map map = new HashMap();
        map.put("title", "微服务");
        List<Blog> blogs = mapper.queryBlogIf(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
    }

结果

2. Mybatis–动态SQL之set语句

set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)。

2.1 Mapper接口

BlogMapper.java

void updateBlog(Map map);

2.2 Mapper.xml

<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}
    </update>

2.3 测试

@Test
    public void updateBlog() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map map = new HashMap();
        map.put("id", "dc15c4a77e7b475db40abe4e26ee91bc");
        map.put("title", "动态SQL");
        mapper.updateBlog(map);

        sqlSession.commit();
        // 关闭sqlSession
        sqlSession.close();
    }

结果

3. Mybatis–动态SQL之trim语句

如果 where 元素与你期望的不太一样,你也可以通过自定义 trim 元素来定制 where 元素的功能。比如,和 where 元素等价的自定义 trim 元素为:

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

与 set 元素等价的自定义 trim 元素吧:

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

4. 小结

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

经验分享 程序员 微信小程序 职场和发展