以查询User信息为例
在IUserDao接口种加入注释
@CacheNamespace(blocking = true)//开启二级缓存
public interface IUserDao {
......
}
验证:
public class SecondLevelCatchTest {
private InputStream in;
private SqlSessionFactory factory;
private IAccountDao accountDao;
@Before
public void init()throws Exception{
in= Resources.getResourceAsStream("SqlMapConfig.xml");
factory=new SqlSessionFactoryBuilder().build(in);
}
@After
public void destroy()throws Exception{
in.close();
}
@Test
public void testFindOne(){
SqlSession session=factory.openSession();
IUserDao userDao=session.getMapper(IUserDao.class);
User user=userDao.findById(57);
System.out.println(user);
session.close();//释放一级缓存
SqlSession session1=factory.openSession();//再次打开session
IUserDao userDao1=session1.getMapper(IUserDao.class);
User user1=userDao1.findById(57);
System.out.println(user1);
}
}
未启动二级缓存,会显示两次查询,结果为
启动二级缓存,只查询一次,结果如下: