首页 > 数据库技术 > 详细

动态 SQL

时间:2021-06-15 16:25:13      阅读:16      评论:0      收藏:0      [点我收藏+]

动态 SQL

  • 什么是动态SQL:动态SQL就是指根据不同的条件生成不同的SQL语句

    利用动态 SQL 这一特性可以彻底摆脱这种痛苦。

动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。
if
choose (when, otherwise)
trim (where, set)
foreach
  • 导包
  • 编写配置文件
  • 编写实体类
@Data
public class Blog {
    private String id;
    private String title;
    private String author;
    private Date createTime;
    private int views;
}
  • 编写实体类对应的Mapper接口和Mappe.xml文件

  • ID工具类:UUID生产随机数

public class IdUtils {
    public static String getId(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }
    @Test
    public void test(){
        System.out.println(IdUtils.getId());
    }
}

if 标签

接口

List<Blog> queryBlogIf(Map map);

SQL xml

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

测试

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

choose、when、otherwise

  • 有时候,我们不想使用所有的条件,而只是想从多个条件中选择一个使用。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。

接口

List<Blog> queryBlogChoose(Map map);

SQL xml

<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 = #{title}
            </when>
            <otherwise>
                and views = #{views}
            </otherwise>
        </choose>
    </where>
</select>

测试

@Test
public void testQueryBlogChoose(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    HashMap map = new HashMap();
    map.put("title","Java");
    //map.put("author", "saxon");
    map.put("views", 9);
    List<Blog> blogs = mapper.queryBlogChoose(map);
    for (Blog blog : blogs) {
        System.out.println(blog);
    }
    sqlSession.close();
}

trim、where、set

  • *where*元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,*where* 元素也会将它们去除。

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

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>
  • *set* 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)。

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

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

注意,我们覆盖了后缀值设置,并且自定义了前缀值。

接口

//更新博客
int updateBlog(Map map);

SQL xml

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

测试

@Test
public void testUpdateBlog(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    HashMap map = new HashMap();
    map.put("title","Java2");
    map.put("author", "saxon");
    map.put("id", "66ba951820b5435fab8acbcbf2ff824a");
    mapper.updateBlog(map);
    sqlSession.close();
}

SQL 片段

  • 有的时候,可能会将一些功能的部分抽取出来,方便复用!
  • 使用步骤:
    • 使用SQL标签抽取公共的部分
    • 在需要使用的地方使用Include标签引用即可
  • 用例:
<sql id="if-title-author">
    <if test="title != null">
        title = #{title},
    </if>
    <if test="author != null">
        author = #{author}
    </if>
</sql>
<update id="updateBlog" parameterType="map">
    update mybatis.blog
    <set>
        <include refid="if-title-author"/>
    </set>
    where id = #{id}
</update>
  • 注意事项:
    • 最好基于单表来定义片段
    • 不要存在 where 标签

Foreach

select * from user where 1=1 and 
  <foreach item="id" collection="ids"
      open="(" separator="or" close=")">
        #{id}
  </foreach>
(id=1 or id=2 or id=3)
技术分享图片
<!--
        select * from mybatis.blog where 1=1 and (id=1 or id = 2 or id=3)
        我们现在传递一个万能的map , 这map中可以存在一个集合!
-->
<select id="queryBlogForeach" parameterType="map" resultType="blog">
    select * from blog
    <where>
        <foreach collection="ids" item="id" open="and (" close=")" separator="or">
            id = #{id}
        </foreach>
    </where>
</select>

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

建议:

  • 现在Mysql中写出完整的SQL,再对应的去修改成为我们的动态SQL实现通用即可!

动态 SQL

原文:https://www.cnblogs.com/saxonsong/p/14884711.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!