测试代码
@Test
public void test1() throws Exception {
SqlSession sqlSession = factory.openSession();
ItemsMapper mapper = sqlSession.getMapper(ItemsMapper.class);
Items items = mapper.selectByiid(100);
System.out.println(items);
//同样查询iid为100的商品
Items items1 = mapper.selectByiid(100);
System.out.println(items1);
sqlSession.close();
}
执行结果
说明
- Mybatis默认支持一级缓存,不需要再配置文件中进行配置
- 同一个session作用域里,第二次进行相同的查询时,直接获取缓存数据,不需要再执行sql到数据库里查
测试代码
@Test
public void test1() throws Exception {
SqlSession sqlSession = factory.openSession();
ItemsMapper mapper = sqlSession.getMapper(ItemsMapper.class);
Items items = mapper.selectByiid(100);
System.out.println(items);
//执行sqlSession.commit()
sqlSession.commit();
//同样查询iid为100的商品
Items items1 = mapper.selectByiid(100);
System.out.println(items1);
sqlSession.close();
}
执行结果
说明
- 同一个session指的是从获取session对象开始,至session.commit()结束