Mybatis执行器(Executor)

这篇具有很好参考价值的文章主要介绍了Mybatis执行器(Executor)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Executor简介

Executor

Executor是MyBatis的核心接口之一,其中定义了数据库操作的基本方法。在实际应用中经常涉及的SqlSession接口的功能,都是基于Executor接口实现的。

BaseExecutor

BaseExecutor是一个实现了Executor接口的抽象类,它实现了Executor 接口的大部分方法。BaseExecutor 中主要提供了缓存管理和事务管理的基本功能。继承BaseExecutor 的子类只要实现四个基本方法来完成数据库的相关操作即可,这四个方法是:doUpdate()方法、doQuery()方法、doQueryCursor()方法、doFlushstatement()方法, 其余的功能在BaseExecutor中实现。

SimpleExecutor

SimpleExecutor 继承了BaseExecutor 抽象类,它是最简单的 Executor 接口实现。Executor使用了模板方法模式,一级缓存等固定不变的操作都封装到了BaseExecutor中,SimpleExecutor中就不必再关心一级缓存等操作,只需要专注实现4个基本方法的实现即可。

ReuseExecutor

在传统的JDBC编程中,重用Statement对象是常用的一种优化手段,该优化手段可以减少SQL预编译的开销以及创建和销毁 Statement 对象的开销,从而提高性能。
ReuseExecutor提供了Statement重用的功能,ReuseExecutor 中通过statementMap字段(HashMap<String, Statement>类型)缓存使用过的 Statement 对象,key是SQL 语句,value是SQL对应的Statement对象。
ReuseExecutor.doQuery()、doQueryCursor()、doUpdate()方法的实现与SimpleExecutor 中对应方法的实现一样,区别在于其中调用的 prepareStatement()方法,SimpleExecutor 每次都会通过JDBC Connection创建新的Statement对象,而ReuseExecutor则会先尝试重用StatementMap中缓存的Statement对象。

BatchExecutor

应用系统在执行一条SQL语句时,会将SQL语句以及相关参数通过网络发送到数据库系统。对于频繁操作数据库的应用系统来说,如果执行一条SQL语句就向数据库发送一次请求,很多时间会浪费在网络通信上。使用批量处理的优化方式可以在客户端缓存多条SQL语句,并在合适的时机将多条SQL语句打包发送给数据库执行,从而减少网络方面的开销,提升系统的性能。
不过有一点需要注意,在批量执行多条SQL语句时,每次向数据库发送的SQL语句条数是有上限的,如果超过这个上限,数据库会拒绝执行这些SQL语句并抛出异常。所以批量发送SQL语句的时机很重要。

执行器功能演示

代码准备

创建配置文件mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <properties>
        <property name="driver" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;allowPublicKeyRetrieval=true" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
    </properties>

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <environments default="default">
        <environment id="default">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="mapper/EmployeeMapper.xml" />
    </mappers>

</configuration>
创建实体类EmployeeDO
@Data
@NoArgsConstructor
@AllArgsConstructor
public class EmployeeDO {

    private Integer id;

    private String name;

    private Integer age;

    private String phone;
}
创建EmployeeMapper接口
public interface EmployeeMapper {

    EmployeeDO getEmployeeById(Integer id);

    int addEmployee(EmployeeDO employeeDO);
}
创建EmployeeMapper.xml
<?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.ys.mybatis.mapper.EmployeeMapper">

    <select id="getEmployeeById" resultType="com.ys.mybatis.DO.EmployeeDO">
        select * from employee where id = #{id}
    </select>

    <insert id="addEmployee" useGeneratedKeys="true" keyProperty="id">
       insert into employee(`name`, `age`, `phone`) VALUES (#{name},#{age},#{phone})
    </insert>

</mapper>
创建测试类EmployeeTest
@Slf4j
public class EmployeeTest1 {

    private SqlSessionFactory sqlSessionFactory;

    private Configuration configuration;

    @BeforeEach
    public void before() {
        InputStream inputStream = ConfigurationTest.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        configuration = sqlSessionFactory.getConfiguration();
    }

    @Test
    public void queryForSimpleExecutor() {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        EmployeeDO firstQuery = employeeMapper.getEmployeeById(1);
        EmployeeDO secondQuery = employeeMapper.getEmployeeById(2);
        System.out.println(firstQuery);
        System.out.println(secondQuery);
    }

    @Test
    public void queryForReuseExecutor() {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE);
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        EmployeeDO firstQuery = employeeMapper.getEmployeeById(1);
        EmployeeDO secondQuery = employeeMapper.getEmployeeById(2);
        System.out.println(firstQuery);
        System.out.println(secondQuery);
    }

    @Test
    public void batchProcessingForSimpleExecutor() {
        Random random = new Random();

        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

        long start = System.currentTimeMillis();

        for (int i = 0; i < 1000; i++) {
            EmployeeDO employee = new EmployeeDO(null, getRandomName(), random.nextInt(100), getRandomPhone());
            employeeMapper.addEmployee(employee);
        }

        sqlSession.commit();

        long end = System.currentTimeMillis();

        System.out.println("共耗时:" + (end - start) / 1000);

    }

    @Test
    public void batchProcessingForReuseExecutor() {
        Random random = new Random();

        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE);
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

        long start = System.currentTimeMillis();

        for (int i = 0; i < 1000; i++) {
            EmployeeDO employee = new EmployeeDO(null, getRandomName(), random.nextInt(100), getRandomPhone());
            employeeMapper.addEmployee(employee);
        }

        sqlSession.commit();

        long end = System.currentTimeMillis();

        System.out.println("共耗时:" + (end - start) / 1000);

    }


    @Test
    public void batchProcessingForBatchExecutor() {
        boolean autoCommit = false;
        Environment environment = configuration.getEnvironment();
        Transaction transaction = environment.getTransactionFactory().newTransaction(environment.getDataSource(), TransactionIsolationLevel.REPEATABLE_READ, autoCommit);
        BatchExecutor batchExecutor = new BatchExecutor(configuration, transaction);
        Random random = new Random();

        try (DefaultSqlSession sqlSession = new DefaultSqlSession(configuration, batchExecutor, autoCommit)) {
            EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

            long start = System.currentTimeMillis();

            for (int i = 0; i < 1000; i++) {
                EmployeeDO employee = new EmployeeDO(null, getRandomName(), random.nextInt(100), getRandomPhone());
                employeeMapper.addEmployee(employee);
            }

            sqlSession.commit();

            long end = System.currentTimeMillis();

            System.out.println("共耗时:" + (end - start) / 1000);
        }
    }


    private String getRandomName() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 5);
    }

    private String getRandomPhone() {
        StringBuilder sb = new StringBuilder("1");
        Random random = new Random();
        IntStream.range(0, 10).forEach(i -> sb.append(random.nextInt(10)));
        return sb.toString();
    }

}

查询 : SimpleExecutor vs ReuseExecutor

执行queryForSimpleExecutor

executor 执行器,mybatis,mybatis,java,后端

每执行一次查询,预编译一次

执行queryForReuseExecutor

executor 执行器,mybatis,mybatis,java,后端executor 执行器,mybatis,mybatis,java,后端

多次查询,只预编译一次

批量插入 : SimpleExecutor vs ReuseExecutor vs BatchExecutor

执行batchProcessingForSimpleExecutor

executor 执行器,mybatis,mybatis,java,后端

插入1000条数据耗时 : 55s

执行batchProcessingForReuseExecutor

executor 执行器,mybatis,mybatis,java,后端

插入1000条数据耗时 : 47s

执行batchProcessingForBatchExecutor

executor 执行器,mybatis,mybatis,java,后端

插入1000条数据耗时 : 21s

原因简析
  • ReuseExecutor  vs SimpleExecutor : 减少了预编译次数
  • BatchExecutor vs ReuseExecutor  : 减少了与数据库交互次数

PS : ReuseExecutor和SimpleExecutor在执行批量插入的时候其实性能差距不太大,要是执行ReuseExecutor的批量插入时刚好机器负载大,执行SimpleExecutor的批量插入时机器负载小,有可能出现SimpleExecutor的批量插入用时更少的情况

源码简析

SimpleExecutor.doQuery() vs ReuseExecutor.doQuery()

executor 执行器,mybatis,mybatis,java,后端

executor 执行器,mybatis,mybatis,java,后端

SimpleExecutor的doQuery()方法和ReuseExecutor的doQuery()方法基本上是一致的,主要差异就是prepareStatement这个方法的内部实现。SimpleExecutor每次都重新编译Statement,而ReuseExecutor则是将Statement缓存起来,以供下次查询使用

SimpleExecutor.doQuery() vs BatchExecutor.doQuery()

executor 执行器,mybatis,mybatis,java,后端

如果仅执行查询操作,SimpleExecutor的doQuery()方法和BatchExecutor的doQuery()方法的效果是一样的,只是BatchExecutor的doQuery()方法会多一个批量刷新的方法。BatchExecutor执行器执行插入或者更新操作的时候不会立即执行,而是封装成BatchResult对象,等执行flushStatements方法的时候才是真正执行相关操作。即BatchExecutor在执行更新操作中间执行了查询操作也会触发批处理,如果更新操作中间没有查询操作,那只有等到执行sqlSession的commit方法才会执行flushStatements

BatchExecutor的更新操作

executor 执行器,mybatis,mybatis,java,后端

BatchExecutor会拿本次执行的sql和MappedStatement与上一次的对比,如果是一致的,则从缓存中获取上一次使用的Statement,减少了预编译次数,这一点类似ReuseExecutor的查询。

PS : ReuseExecutor的doQuery是根据sql查找缓存,BatchExecutor的doUpdate是根据sql和MappedStatement查找缓存。个人理解:不同Statement的timeout和fetchSize可能不同,而更新操作是比较重要的,所以不能混用文章来源地址https://www.toymoban.com/news/detail-852873.html

到了这里,关于Mybatis执行器(Executor)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Junit执行器Runner探索之旅

    单元测试是每个程序员必备的技能,而Runner是每个单元测试类必有属性。本文通过解读Junit源码,介绍junit中每个执行器的使用方法,让读者在单元测试时,可以灵活的使用Runner执行器。 在今年的敏捷团队建设中,京东物流通过Suite执行器实现了一键自动化单元测试。Juint除了

    2024年02月08日
    浏览(28)
  • PgSQL-执行器机制-Unique算子

    PgSQL-执行器机制-Unique算子 PgSQL中输出去重的元组有多种方法,比如通过HashAgg或者GroupAgg。这里我们介绍第三种方法,通过Unique算子来完成这个功能。当然语句上可以是:select distinct(id1) from t; 执行器执行算子的函数都是ExecXXX,其中XXX代表某个算子。Unique算子的执行是由函数

    2024年02月07日
    浏览(23)
  • 机械臂速成小指南(五):末端执行器

    👨‍🏫🥰🥳需要机械臂相关资源的同学可以在评论区中留言哦🤖😽🦄 指南目录📖: 🎉🎉机械臂速成小指南(零点五):机械臂相关资源🎉🎉 机械臂速成小指南(零):指南主要内容及分析方法 机械臂速成小指南(一):机械臂发展概况 机械臂速成小指南(二):

    2024年02月03日
    浏览(28)
  • 【PostgreSQL内核学习(二十三)—— 执行器(ExecEndPlan)】

    声明 :本文的部分内容参考了他人的文章。在编写过程中,我们尊重他人的知识产权和学术成果,力求遵循合理使用原则,并在适用的情况下注明引用来源。 本文主要参考了 postgresql-10.1 的开源代码和《OpenGauss数据库源码解析》和《PostgresSQL数据库内核分析》一书   在这三

    2024年01月17日
    浏览(38)
  • 【PostgreSQL内核学习(二十一)—— 执行器(InitPlan)】

    声明 :本文的部分内容参考了他人的文章。在编写过程中,我们尊重他人的知识产权和学术成果,力求遵循合理使用原则,并在适用的情况下注明引用来源。 本文主要参考了 postgresql-10.1 的开源代码和《OpenGauss数据库源码解析》和《PostgresSQL数据库内核分析》一书   在【

    2024年01月16日
    浏览(35)
  • xxl-job执行器无法自动注册

    问题描述 在springboot项目里配置了xxl-job2.3.0,但是执行器无法自动注册 yaml配置如下: 执行器无法自动注册到xxl-job-admin 排查过程 经过debug发现,是spring没有加载xxlJobExecutor这个Bean debug流程(SpringApplication.run()–SpringApplication.refreshContext()–SpringApplication.refresh() --SpringApplication

    2024年02月16日
    浏览(24)
  • Camunda 7.x 系列【53】Job 执行器

    有道无术,术尚可求,有术无道,止于术。 本系列Spring Boot 版本 2.7.9 本系列Camunda 版本 7.19.0 源码地址:https://gitee.com/pearl-organization/camunda-study-demo Job Executor 即任务执行器,是 Camunda 中的一个调度组件,负责执行异步后台作业。 Job 表示 Job Executor 执行的某一作业,例如,在定

    2024年02月09日
    浏览(28)
  • Spring Boot 中的任务执行器是什么,如何使用

    Spring Boot 是一个非常流行的 Java 开发框架,它的核心理念是通过简单的配置和约定来提高开发效率。在很多情况下,我们需要在后台执行一些任务,比如异步处理、定时任务等等。为了简化这些任务的开发和管理,Spring Boot 提供了一个任务执行器(Task Executor)。 任务执行器

    2024年02月15日
    浏览(27)
  • 【源码分析】XXL-JOB的执行器的注册流程

    目的:分析xxl-job执行器的注册过程 流程: 获取执行器中所有被注解( @xxlJjob )修饰的 handler 执行器注册过程 执行器中任务执行过程 版本: xxl-job 2.3.1 建议:下载 xxl-job 源码,按流程图 debug 调试, 看堆栈信息并按文章内容理解执行流程 。 完整流程图: 部分流程图: 首先启

    2023年04月22日
    浏览(31)
  • 【微软】【ICLR 2022】TAPEX:通过学习神经 SQL 执行器进行表预训练

    重磅推荐专栏: 《大模型AIGC》;《课程大纲》 本专栏致力于探索和讨论当今最前沿的技术趋势和应用领域,包括但不限于ChatGPT和Stable Diffusion等。我们将深入研究大型模型的开发和应用,以及与之相关的人工智能生成内容(AIGC)技术。通过深入的技术解析和实践经验分享,

    2024年02月05日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包