从问题引发对ORM(对象关系映射)框架-Mybatis源码的认识>>>Mybatis的一级缓存默认是开启的吗?默认采用的执行器是?
回答这类似的问题,众说得来终觉浅,绝知此事要Debug,上来就是Debug<可条件断点>源码,可按下面三点去一探究竟:
1、数据源>>> xml<environment>java Environment::
>org.apache.ibatis.session.SqlSessionFactoryBuilder.build(java.io.InputStream)
>org.apache.ibatis.builder.xml.XMLConfigBuilder
>org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration
>org.apache.ibatis.builder.xml.XMLConfigBuilder.environmentsElement
>org.apache.ibatis.builder.xml.XMLConfigBuilder.dataSourceElement
>org.apache.ibatis.datasource.DataSourceFactory.getDataSource
2、sql解析>>>sql>MappedStatement::
>org.apache.ibatis.session.SqlSessionFactoryBuilder.build(java.io.InputStream)
>org.apache.ibatis.builder.xml.XMLMapperBuilder
>org.apache.ibatis.builder.xml.XMLMapperBuilder.parse
>org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(List<XNode>)
>org.apache.ibatis.builder.xml.XMLStatementBuilder
>org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode
3、操作库>>> SimpleExecutor 执行器::
>org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession()
>org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession.openSessionFromDataSource
>org.apache.ibatis.session.Configuration.newExecutor()
>org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne()
>org.apache.ibatis.session.Configuration.queryFromDataBase
>org.apache.ibatis.executor.SimpleExecutor.doQuery
Summary:
(其中加载mapper的方式可分:package、resource、url、class)
从源码可知Mybatis的一级缓存(SqlSession)是默认开启的,二级缓存(mapper级别,默认不开启),默认采用的执行器是SimpleExecutor。
其中,
SimpleExecutor:获取声明处理类StatementHandler、获取声明类prepareStatement()、处理声明,并执行sql处理。
ReuseExecutor:重用声明(sql一样,但仅仅是同一个session内的有效)。
BatchExecutor:重点在flushStatements(批量更新提交),若不开启批量模式,则是没有使用批量更新的>sessionFactory.openSession(ExecutorType.BATCH, false)。
CachingExecutor:缓存执行器(全局缓存),configuration里面的cacheEnabled参数决定,默认为true,也就是默认是开启全局缓存,其缓存机制主要设计模式<装饰模式、责任链模式>,其CachingExecutor类<增强了其他的执行器,加缓存可借鉴☞>,不同的是其先执行缓存处理>,与此同时,只有在每个mapper上须配置是否使用缓存后才会生效。