生活随笔
收集整理的這篇文章主要介紹了
                                
[mybatis]缓存_一级缓存_一级缓存失效的四种情况
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
 
                                
                            
                            
                             
 
 @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();}}
 
                            總結
                            
                                以上是生活随笔為你收集整理的[mybatis]缓存_一级缓存_一级缓存失效的四种情况的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。