[mybatis]缓存_一级缓存_一级缓存失效的四种情况

Posted 唐火

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[mybatis]缓存_一级缓存_一级缓存失效的四种情况相关的知识,希望对你有一定的参考价值。

  • 1.sqlSession不同
 @Test
    public 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();

        
    

  • 2.sqlSession相同,查询条件不同
    @Test
    public 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 =   mapper01.getEmpById(2);

            System.out.println(emp02);

            System.out.println(emp01==emp02);


//            sqlSession02.close();

        finally 

            sqlSession01.close();

        
    

  • 3.sqlSession相同,两次查询之间执行了增删改操作(这次增删改可能对当前数据有影响)
 @Test
    public 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);

            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);


//            sqlSession02.close();

        finally 

            sqlSession01.close();

        
    

  • 4.sqlSession相同,手动清除了一级缓存(缓存清空)
    @Test
    public 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);

//            mapper01.addEmp(new Employee(null,"testCache","cache","1"));

            System.out.println("数据添加成功");

            sqlSession01.clearCache();

              Employee emp02 =   mapper01.getEmpById(2);

            System.out.println(emp02);

            System.out.println(emp01==emp02);


//            sqlSession02.close();

        finally 

            sqlSession01.close();

        
    

以上是关于[mybatis]缓存_一级缓存_一级缓存失效的四种情况的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis 一级缓存,Mybatis 二级缓存,Mybatis 缓存失效

MyBatis缓存看这一篇就够了(一级缓存+二级缓存+缓存失效+缓存配置+工作模式+测试)

[mybatis]缓存_一级缓存

MyBatis:缓存

TZ_02MyBatis_一级缓存和二级缓存

[mybatis]缓存机制介绍_一级缓存&二级缓存