前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MyBatis:动态SQL

MyBatis:动态SQL

作者头像
愷龍
发布2023-01-10 14:14:07
6150
发布2023-01-10 14:14:07
举报
文章被收录于专栏:愷龍的Blog愷龍的Blog
image
image

目录

动态SQL介绍

动态SQL指的是根据不同的查询条件 , 生成不同的Sql语句.

官网描述: MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。 虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。 动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。

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

之前写的 SQL 语句都比较简单,如果有比较复杂的业务,需要写复杂的 SQL 语句,往往需要拼接,而拼接 SQL ,稍微不注意,由于引号,空格等缺失可能都会导致错误。

那么怎么去解决这个问题呢?这就要使用 mybatis 动态SQL,通过 if, choose, when, otherwise, trim, where, set, foreach等标签,可组合成非常灵活的SQL语句,从而在提高 SQL 语句的准确性的同时,也大大提高了开发人员的效率。

搭建环境

新建一个数据库表:blog

字段:id,title,author,create_time,views

代码语言:javascript
复制
CREATE TABLE `blog` (
`id` varchar(50) NOT NULL COMMENT '博客id',
`title` varchar(100) NOT NULL COMMENT '博客标题',
`author` varchar(30) NOT NULL COMMENT '博客作者',
`create_time` datetime NOT NULL COMMENT '创建时间',
`views` int(30) NOT NULL COMMENT '浏览量'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
  1. 按照之前的搭建MyBatis测试环境
  2. 新建IDUtil工具类
代码语言:javascript
复制
public class IDUtil {

   public static String genId(){
       return UUID.randomUUID().toString().replaceAll("-","");
  }

}
  1. 实体类
代码语言:javascript
复制
public class Blog {

   private String id;
   private String title;
   private String author;
   private Date createTime;
   private int views;
   //set,get....
}
  1. 编写Mapper接口及xml文件
代码语言:javascript
复制
public interface BlogMapper {
}
代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kailong.mapper.BlogMapper">

</mapper>
  1. mybatis核心配置文件,下划线驼峰自动转换
代码语言:javascript
复制
<settings>
   <setting name="mapUnderscoreToCamelCase" value="true"/>
   <setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--注册Mapper.xml-->
<mappers>
 <mapper resource="com/kailong/mapper/BlogMapper.xml"/>
</mappers>
  1. 插入初始数据 编写接口
代码语言:javascript
复制
/**
     * 新增一个博客
     * @param blog
     * @return
     */
int addBlog(Blog blog);

sql配置文件

代码语言:javascript
复制
<insert id="addBlog" parameterType="com.kailong.pojo.Blog">
    insert into blog (id, title, author, create_time, views)
    values (#{id},#{title},#{author},#{createTime},#{views});
</insert>

初始化博客方法

代码语言:javascript
复制
    @Test
    public void addInitBlog(){
        SqlSession session = MybatisUtils.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);

        Blog blog = new Blog(null,null,null,null,0);
        blog.setId(IDUtil.genId());
        blog.setTitle("MyBatis实现增删改查");
        blog.setAuthor("愷龍");
        blog.setCreateTime(new Date());
        blog.setViews(9999);

        mapper.addBlog(blog);

        blog.setId(IDUtil.genId());
        blog.setTitle("MyBatis-ResultMap");
        mapper.addBlog(blog);

        blog.setId(IDUtil.genId());
        blog.setTitle("MyBatis日志工厂");
        mapper.addBlog(blog);

        blog.setId(IDUtil.genId());
        blog.setTitle("MyBatis分页实现");
        mapper.addBlog(blog);

        session.close();
    }

if语句

需求:根据作者名字和博客名字来查询博客!如果作者名字为空,那么只根据博客名字查询,反之,则根据作者名来查询

  1. 编写接口类
代码语言:javascript
复制
List<Blog> queryBlogIf(Map map);
  1. 编写SQL语句
代码语言:javascript
复制
<!--
	根据作者名字和博客名字来查询博客!
	如果作者名字为空,那么只根据博客名字查询,反之,则根据作者名来查询
	select * from blog where title = #{title} and author = #{author}
-->
<select id="queryBlogIf" parameterType="map" resultType="com.kailong.pojo.Blog">
    select * from blog where
    <if test="title != null">
        title = #{title}
    </if>
    <if test="author != null">
        and author = #{author}
    </if>
</select>
  1. 测试
代码语言:javascript
复制
@Test
public void testQueryBlogIf(){
    SqlSession session = MybatisUtils.getSession();
    BlogMapper mapper = session.getMapper(BlogMapper.class);

    HashMap<String, String> map = new HashMap<String, String>();
    map.put("title","MyBatis实现增删改查");
    map.put("author","愷龍");
    List<Blog> blogs = mapper.queryBlogIf(map);

    System.out.println(blogs);

    session.close();
}

测试结果:

image
image

这样写可以看到,如果 author 等于 null,那么查询语句为 select * from user where title=#{title},但是如果title为空呢?那么查询语句为 select * from user where and author=#{author},这是错误的 SQL 语句,如何解决呢?请看下面的 where 语句

Where

修改上面的SQL语句:

代码语言:javascript
复制
<select id="queryBlogIf" parameterType="map" resultType="com.kailong.pojo.Blog">
    select * from blog
    <where>
        <if test="title != null">
            title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </where>
</select>

这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。

Set

同理,上面的对于查询 SQL 语句包含 where 关键字,如果在进行更新操作的时候,含有 set 关键词,该怎么处理呢?

  1. 编写接口方法
代码语言:javascript
复制
int updateBlog(Map map);
  1. sql配置文件
代码语言:javascript
复制
<!--注意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>
  1. 测试
代码语言:javascript
复制
    @Test
    public void testUpdateBlog(){
        SqlSession session = MybatisUtils.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("title","动态SQL");
        map.put("author","愚生浅末");
        map.put("id","452217aa51e84c6a8acc721d95e861de");
        mapper.updateBlog(map);
        session.close();
    }

测试结果:

image
image

choose语句

有时候,不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java 的 switch 语句

  1. 编写接口方法
代码语言:javascript
复制
List<Blog> queryBlogChoose(Map map);
  1. sql配置文件
代码语言:javascript
复制
<select id="queryBlogChoose" parameterType="map" resultType="com.kailong.pojo.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>
  1. 测试类
代码语言:javascript
复制
@Test
public void testQueryBlogChoose(){
    SqlSession session = MybatisUtils.getSession();
    BlogMapper mapper = session.getMapper(BlogMapper.class);

    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("title","MyBatis日志工厂");
    map.put("author","愷龍");
    map.put("views",9999);
    List<Blog> blogs = mapper.queryBlogChoose(map);

    System.out.println(blogs);

    session.close();
}

测试结果:

image
image

SQL片段

有时候可能某个 sql 语句用的特别多,为了增加代码的重用性,简化代码,需要将这些代码抽取出来,然后使用时直接调用。

提取SQL片段:

代码语言:javascript
复制
<sql id="if-title-author">
   <if test="title != null">
      title = #{title}
   </if>
   <if test="author != null">
      and author = #{author}
   </if>
</sql>

引用SQL片段:

代码语言:javascript
复制
<select id="queryBlogIf" parameterType="map" resultType="com.kailong.pojo.Blog">
  select * from blog
   <where>
       <!-- 引用 sql 片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace -->
       <include refid="if-title-author"></include>
       <!-- 在这里还可以引用其他的 sql 片段 -->
   </where>
</select>

注意:

①最好基于 单表来定义 sql 片段,提高片段的可重用性

②在 sql 片段中不要包括 where

Foreach

将数据库中数据的id修改为1,2,3,4;

image
image

查询 blog 表中 id 分别为1,2,3的博客信息

  1. 编写接口
代码语言:javascript
复制
List<Blog> queryBlogForeach(Map map);
  1. 编写SQL语句
代码语言:javascript
复制
<select id="queryBlogForeach" parameterType="map" resultType="com.kailong.pojo.Blog">
    select * from blog
    <where>
        <!--
            collection:指定输入对象中的集合属性
            item:每次遍历生成的对象
            open:开始遍历时的拼接字符串
            close:结束时拼接的字符串
            separator:遍历对象之间需要拼接的字符串
            select * from blog where 1=1 and (id=1 or id=2 or id=3)
          -->
        <foreach collection="ids"  item="id" open="and (" close=")" separator="or">
            id=#{id}
        </foreach>
    </where>
</select>
  1. 测试
代码语言:javascript
复制
@Test
public void testQueryBlogForeach(){
    SqlSession session = MybatisUtils.getSession();
    BlogMapper mapper = session.getMapper(BlogMapper.class);

    HashMap map = new HashMap();
    List<Integer> ids = new ArrayList<Integer>();
    ids.add(1);
    ids.add(2);
    ids.add(3);
    map.put("ids",ids);

    List<Blog> blogs = mapper.queryBlogForeach(map);

    System.out.println(blogs);

    session.close();
}

测试结果:

image
image

[Blog{id='1', title='动态SQL', author='愚生浅末', createTime=Mon Jan 09 10:56:17 CST 2023, views=9999}, Blog{id='2', title='MyBatis-ResultMap', author='愷龍', createTime=Mon Jan 09 10:56:17 CST 2023, views=9999}, Blog{id='3', title='MyBatis日志工厂', author='愷龍', createTime=Mon Jan 09 10:56:17 CST 2023, views=9999}]

总结

​ 其实动态 sql 语句的编写往往就是一个拼接的问题,为了保证拼接准确,最好首先要写原生的 sql 语句出来,然后在通过 mybatis 动态sql 对照着改,防止出错。多在实践中使用才是熟练掌握它的技巧。

公众号本文地址:https://mp.weixin.qq.com/s/CxDvqRZIbR0voUmWJMk7_g 欢迎关注公众号:愚生浅末。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-01-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 动态SQL介绍
  • 搭建环境
  • if语句
  • Where
  • Set
  • choose语句
  • SQL片段
  • Foreach
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档