Mybatis源码学习一Executor

导读:本篇文章讲解 Mybatis源码学习一Executor,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Executor架构

Mybatis源码学习一Executor

Caching Executor的作用就是实现二级缓存,使用的是装饰者模式。

装饰者模式:在不改变原有类继承结构的情况下,新建一个对象来扩展原有功能。

Executor具体实现

maven依赖

<dependency>
    <groupId> org.mybatis </groupId>
    <artifactId> mybatis </artifactId>
    <version> 3.5.7 </version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
</dependency>

public interface UserMapper {

    @Select("select name from user where id = #{id}")
    String selectById(@Param("id") int id);

    @Update("update user set name=#{arg1} where id=#{arg0}")
    void updateById(int id,String name);
}
public class ExecutorTest {

    private Configuration configuration;

    private Connection connection;

    private JdbcTransaction jdbcTransaction;

    private  MappedStatement ms;

    @Before
    public void init() throws Exception {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        configuration = factory.getConfiguration();
        connection = DriverManager.getConnection(JDBC.URL,JDBC.USERNAME,JDBC.PASSWORD);
        jdbcTransaction = new JdbcTransaction(connection);
        ms = configuration.getMappedStatement("dao.UserMapper.selectById");
    }

    /**
     * 简单执行器
     * @throws Exception
     */
    @Test
    public void simpleTest() throws Exception {
        SimpleExecutor executor = new SimpleExecutor(configuration,jdbcTransaction);

        List<Object> list = executor.doQuery(ms,1,                 
           RowBounds.DEFAULT,SimpleExecutor.NO_RESULT_HANDLER,ms.getBoundSql(1));
        executor.doQuery(ms,1, 
           RowBounds.DEFAULT,SimpleExecutor.NO_RESULT_HANDLER,ms.getBoundSql(1));
        System.out.println(list.get(0));
    }

    /**
     * 可重用执行器
     * @throws Exception
     */
    @Test
    public void reuseTest() throws Exception {
        ReuseExecutor executor = new ReuseExecutor(configuration,jdbcTransaction);
        MappedStatement ms = 
             configuration.getMappedStatement("dao.UserMapper.selectById");
        List<Object> list = executor.doQuery(ms,1, 
             RowBounds.DEFAULT,ReuseExecutor.NO_RESULT_HANDLER,ms.getBoundSql(1));
        executor.doQuery(ms,1, 
             RowBounds.DEFAULT,ReuseExecutor.NO_RESULT_HANDLER,ms.getBoundSql(1));
        System.out.println(list.get(0));
    }

    /**
     * 批处理执行器
     * @throws Exception
     */
    @Test
    public void batchTest() throws Exception {
        BatchExecutor executor = new BatchExecutor(configuration,jdbcTransaction);
        MappedStatement ms = 
          configuration.getMappedStatement("dao.UserMapper.updateById");
        Map param = new HashMap();
        param.put("arg0",1);
        param.put("arg1","rrrteeeo");
        Map param1= new HashMap();
        param1.put("arg0",1);
        param1.put("arg1","rrrcco");
        executor.doUpdate(ms,param);
        executor.doUpdate(ms,param1);
        executor.doFlushStatements(false);
    }

    /**
     * 基础执行器
     * @throws Exception
     */
    @Test
    public void baseTest() throws Exception {
        Executor executor = new SimpleExecutor(configuration,jdbcTransaction);
        executor.query(ms,1, RowBounds.DEFAULT,Executor.NO_RESULT_HANDLER);
        executor.query(ms,1, RowBounds.DEFAULT,Executor.NO_RESULT_HANDLER);

    }
}

 执行结果

simpleTest:每次都会进行新的预处理

SimpleExecutor为默认执行器

Mybatis源码学习一Executor

reuseTest:相同的sql只进行一次预处理

Mybatis源码学习一Executor

batchTest:相同的批量更新操作,会组合成一次sql处理

Mybatis源码学习一Executor

baseTest:调用BaseExecutor的query方法(或者update方法),最终会执行子类中的doQuery方法(或者doUpdate方法)

Mybatis源码学习一Executor

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/1269.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!