mybatis的缓存机制

一级缓存:

MyBatis的一级缓存指的是在一个Session域内,session为关闭的时候执行的查询会根据SQL为key被缓存(跟mysql缓存一样,修改任何参数的值都会导致缓存失效)

 

 

 

package cn.itcast.mybatis.dao;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import cn.itcast.mybatis.pojo.User;

public class UserDAOTest2 {

    private static IUser userDAO = null;
    
    private static SqlSession sqlSession = null;

    @Before
    public void init() {
        try {
            // 构造SqlSessionFactory
            // 定义配置文件路径
            String resource = "mybatis-config.xml";
            // 读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
            // 通过SqlSessionFactoryBuilder构建一个SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"test");
            
            this.sqlSession = sqlSessionFactory.openSession(false);//设为false时,必须手动提交:sqlSession.commit();
//            this.sqlSession.close();
            this.userDAO = sqlSession.getMapper(IUser.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    static{
        
          // 构造SqlSessionFactory
        // 定义配置文件路径
        String resource = "mybatis-config.xml";
        // 读取配置文件
        InputStream inputStream;
        try {
            inputStream = Resources.getResourceAsStream(resource);
             // 通过SqlSessionFactoryBuilder构建一个SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"test");
            
           sqlSession = sqlSessionFactory.openSession(false);//设为false时,必须手动提交:sqlSession.commit();
//            this.sqlSession.close();//当关闭的时候,在下面的方法中都会报错
           userDAO = sqlSession.getMapper(IUser.class);
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }

    @Test
    public void testQueryUserByUserName() {
        User user = this.userDAO.queryUserByUserName("zhangsan");
        System.out.println(user);
    }
    
    @Test
    public void testQueryByTabkeName() {
    
       List <HashMap<String, Object>> list2= this.userDAO.queryByTabkeName("tb_order");
      for (HashMap<String, Object> hashMap : list2) {
          System.out.println(hashMap);
        
    }
    }
    
    @Test
    public void testQueryUserByUserNameAndPassword() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userName", "zhangsan");
        map.put("password", "123456");
//        User user = this.userDAO.queryUserByUserNameAndPassword(map);
        User user = this.userDAO.queryUserByUserNameAndPassword("zhangsan","123456");
        System.out.println(user);
    }

    @Test
    public void testSaveUser() {
       User user = new User();
       user.setAge(20);
       user.setBirthday(new Date());
       user.setName("test_name_9");
       user.setPassword("123456");
       user.setSex(true);
       user.setUserName("test_username_9");
       
       this.userDAO.saveUser(user);
       
       System.out.println(user);
       // 提交
       this.sqlSession.commit();
    }

    @Test
    public void testUpdateUser() {
        User user = new User();
        user.setAge(20);
        user.setBirthday(new Date());
        user.setName("test_name_1");
        user.setPassword("123qwe");
        user.setSex(true);
        user.setId(6L);
        
        this.userDAO.updateUser(user);
        this.sqlSession.commit();
    }

    @Test
    public void testDeleteUserById() {
        this.userDAO.deleteUserById(6L);
    }

}

 

二级缓存:

Mybatis的二级缓存的作用域是一个mapper的namespace,同一个namespace中查询sql可以从缓存中命中。

二级缓存是可以跨session的。

 

开启二级缓存:

在mapper.xml文件中加入 <cache /> 。

 

posted @ 2016-09-19 11:58  小啊菜鸡  阅读(4444)  评论(0编辑  收藏  举报