Caching Executor的作用就是实现二级缓存,使用的是装饰者模式。
装饰者模式:在不改变原有类继承结构的情况下,新建一个对象来扩展原有功能。
Executor具体实现maven依赖
org.mybatis mybatis3.5.7 mysql mysql-connector-java8.0.20
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");
}
@Test
public void simpleTest() throws Exception {
SimpleExecutor executor = new SimpleExecutor(configuration,jdbcTransaction);
List
执行结果
simpleTest:每次都会进行新的预处理
SimpleExecutor为默认执行器
reuseTest:相同的sql只进行一次预处理 batchTest:相同的批量更新操作,会组合成一次sql处理 baseTest:调用baseExecutor的query方法(或者update方法),最终会执行子类中的doQuery方法(或者doUpdate方法)


