mybatis一二级缓存

0x00:项目工程图

1.jpg

0x01:mybati.xml全局配置文件




    
    
        
        
        
    
    
        
    
    
        
            
            
                
                
                
                
            
        
    
    
        
    

  • 全局取消一级缓存

0x02:user.xml




    
    

    
    
    
        insert into user(name,age,sex) values (#{name},#{age},#{sex})
    
    
    
        delete from user where nid=#{nid}
    
    
    
        update user
        
            name=#{name},
            age=#{age},
            sex=#{sex},
        
        where nid=#{nid}
    
    
    /*flushCache="true"  局部取消一级缓存*/
    

    
  • readOnly设置为false会报错,序列化异常
  • flushCache="true" 局部取消一级缓存

0x04:IUserDao

package com.it.peach.dao;

import com.it.peach.model.User;

import java.util.List;

/**
 * 接口路径名必须与xml的namespace相同
 * 方法名称必须与xml的id相同
 * 方法的参数类型必须与xml中的parameType相同
 * 方法的返回值必须和xml的resultType或者resultMap相同
 * */
public interface IUserDao {
    /*返回值 方法名(参数类型)*/
    int insertUser(User user);
    int delUser(int id);
    int updateUser(User user);
    List equrysUser(User user);
}
  • 接口路径名必须与xml的namespace相同
  • 方法名称必须与xml的id相同
  • 方法的参数类型必须与xml中的parameType相同
  • 方法的返回值必须和xml的resultType或者resultMap相同

0x05:UserTest.java

public class UserTest {
        SqlSessionFactory sqlSessionFactory = null;

        @Before
        public void init() throws IOException {
            String resourse = "mybatis.xml";
            InputStream resourceAsStream = Resources.getResourceAsStream(resourse);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        }

        @Test
        public void testAdd() {/*增加数据*/
            try (SqlSession session = sqlSessionFactory.openSession()) {//打开一个链接
                User user = new User(null, "王五2", 17, "女");
                // getMapper会通过反射代理生成一个实现类
                IUserDao userDao = session.getMapper(IUserDao.class);
                int rows = userDao.insertUser(user);
                System.out.println("受影响的行数"+rows);
                session.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Test
        public void testDel() {/*删除数据*/
            try (SqlSession session = sqlSessionFactory.openSession()) {//打开一个链接
                IUserDao userDao = session.getMapper(IUserDao.class);
                int rows = userDao.delUser(3);
                System.out.println("删除书籍的行数"+rows);
                session.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Test
        public void testUpdate() {/*修改数据*/
            try (SqlSession session = sqlSessionFactory.openSession()) {//打开一个链接
                User user = new User(2, "李四1", 16, "女");
                IUserDao userDao = session.getMapper(IUserDao.class);
                int rows = userDao.updateUser(user);
                session.commit();
                System.out.println("受影响的行数"+rows);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Test
        public void testFirstLevelCache() {/*一级缓存查询数据*/
            try (SqlSession session = sqlSessionFactory.openSession()) {//打开一个链接
                User user1 = new User(null, "李", null, null);
                IUserDao userDao = session.getMapper(IUserDao.class);
                List list=userDao.equrysUser(user1);
                list.forEach(us -> System.out.println(us));
                System.out.println("分割线=================");
                User user2 = new User(null, "李", null, null);
                userDao = session.getMapper(IUserDao.class);
                List list1=userDao.equrysUser(user2);
                list1.forEach(us -> System.out.println(us));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    @Test
    public void testSecondLevelCache() {/*一级缓存查询数据*/
            SqlSession session1 = sqlSessionFactory.openSession();//打开一个链接
            User user1 = new User(null, "李", null, null);
            IUserDao userDao = session1.getMapper(IUserDao.class);
            List list=userDao.equrysUser(user1);
            list.forEach(us -> System.out.println(us));
            session1.close();// 只有session关闭的时候会把数据同步刷新到二级缓存
            System.out.println("分割线=================");
            SqlSession session2 = sqlSessionFactory.openSession();
            User user2 = new User(null, "李", null, null);
            userDao = session2.getMapper(IUserDao.class);
            List list1=userDao.equrysUser(user2);
            list1.forEach(us -> System.out.println(us));
    }
}
  • session1.close();// 只有session关闭的时候会把数据同步刷新到二级缓存

0x06:mybatis有关缓存的详细说明

https://mybatis.org/mybatis-3/zh/sqlmap-xml.html#cache
mybatis官网:
https://mybatis.org/mybatis-3/zh/index.html

你可能感兴趣的:(mybatis一二级缓存)