@Testpublic void test05() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession01 = sqlSessionFactory.openSession();try{EmployeeMapper mapper01 = sqlSession01.getMapper(EmployeeMapper.class);Employee emp01 = mapper01.getEmpById(1);System.out.println(emp01);SqlSession sqlSession02 = sqlSessionFactory.openSession();EmployeeMapper mapper02 = sqlSession02.getMapper(EmployeeMapper.class);Employee emp02 = mapper02.getEmpById(1);System.out.println(emp02);System.out.println(emp01==emp02);sqlSession02.close();}finally {sqlSession01.close();}}
@Testpublic void test05() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession01 = sqlSessionFactory.openSession();try{EmployeeMapper mapper01 = sqlSession01.getMapper(EmployeeMapper.class);Employee emp01 = mapper01.getEmpById(1);System.out.println(emp01);
Employee emp02 = mapper01.getEmpById(2);System.out.println(emp02);System.out.println(emp01==emp02);}finally {sqlSession01.close();}}
- 3.sqlSession相同,两次查询之间执行了增删改操作(这次增删改可能对当前数据有影响)
@Testpublic void test05() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession01 = sqlSessionFactory.openSession();try{EmployeeMapper mapper01 = sqlSession01.getMapper(EmployeeMapper.class);Employee emp01 = mapper01.getEmpById(1);System.out.println(emp01);
mapper01.addEmp(new Employee(null,"testCache","cache","1"));System.out.println("数据添加成功");Employee emp02 = mapper01.getEmpById(2);System.out.println(emp02);System.out.println(emp01==emp02);}finally {sqlSession01.close();}}
- 4.sqlSession相同,手动清除了一级缓存(缓存清空)
@Testpublic void test05() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession01 = sqlSessionFactory.openSession();try{EmployeeMapper mapper01 = sqlSession01.getMapper(EmployeeMapper.class);Employee emp01 = mapper01.getEmpById(1);System.out.println(emp01);
sqlSession01.clearCache();Employee emp02 = mapper01.getEmpById(2);System.out.println(emp02);System.out.println(emp01==emp02);}finally {sqlSession01.close();}}