@Test
void test1() throws Exception {
//取2个不同的 sqlSession
SqlSession sqlSession1 = factory.openSession(true);
SqlSession sqlSession2 = factory.openSession(true);
EmpMapper mapper1 = sqlSession1.getMapper(EmpMapper.class);
EmpMapper mapper2 = sqlSession2.getMapper(EmpMapper.class);
Emp emp = mapper1.queryOneEmp(1001);
System.out.println("sqlSession1第一次查询"+emp);
Emp emp3 = mapper1.queryOneEmp(1001);
System.out.println("sqlSession1第二次查询"+emp3);
//sqlSession2 进行了数据修改
Emp empParam = new Emp();
empParam.setEmpno(1001);
empParam.setEname("zhaoyun3333");
mapper2.updateOneEmp(empParam);
Emp emp4 = mapper1.queryOneEmp(1001);
System.out.println("sqlSession1第三次次查询"+emp4);
Emp emp2 = mapper2.queryOneEmp(1001);
System.out.println("sqlSession2第一次查询"+emp2);
}
结果:sqlSession1 第一次从数据库读取 zhaoyun2222
sqlSesson1第二次从缓存中读取zhaoyun2222
sqlSession2修改了数据 zhaoyun3333
sqlSesson1第三次从缓存中读取zhaoyun2222
sqlSesson2第一次从缓存中读取zhaoyun3333
但是如果修改的对象为 sqlSession1查询出来的对象,那么sqlSession2修改为zhaoyun1111,会直接改变sqlSession1的缓存
@Test
void test1() throws Exception {
//取2个不同的 sqlSession
SqlSession sqlSession1 = factory.openSession(true);
SqlSession sqlSession2 = factory.openSession(true);
EmpMapper mapper1 = sqlSession1.getMapper(EmpMapper.class);
EmpMapper mapper2 = sqlSession2.getMapper(EmpMapper.class);
Emp emp = mapper1.queryOneEmp(1001);
System.out.println("sqlSession1第一次查询"+emp);
Emp emp3 = mapper1.queryOneEmp(1001);
System.out.println("sqlSession1第二次查询"+emp3);
//sqlSession2 进行了数据修改
//emp 为 mapper1.queryOneEmp(1001); 查询得到
emp.setEname("zhaoyun111111");
mapper2.updateOneEmp(emp);
Emp emp4 = mapper1.queryOneEmp(1001);
System.out.println("sqlSession1第三次次查询"+emp4);
Emp emp2 = mapper2.queryOneEmp(1001);
System.out.println("sqlSession2第一次查询"+emp2);
}