mybatis first level cache

 

Mybatis level 1 cache: In the same sqlsession, the query statement is the same and the query parameters are the same, and the cached data is fetched in the second query

 

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.mgr.user.po.UserPO;

//mybatis first level cache
public class SessionTest {

 public static void main(String[] args)throws Exception{
  ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
  DefaultSqlSessionFactory factory = (DefaultSqlSessionFactory)app.getBean("sqlSessionFactory");
  
  mybatisFirstCache(factory); //Level 1 Cache: must be in the same session 
  
  //mybatisFirstCache2(factory); 
  //mybatisFirstCache3(factory);
  
 }
 
 /**
  * mybatis first-level cache: sql in the same session will be cached:
  * the query statement is the same and the query parameters are the same, The second time to get the cached result in the same session
  * @param factory
  */
 static void mybatisFirstCache(DefaultSqlSessionFactory factory){
  SqlSession session = factory.openSession();
  
  String statement = "cn.mgr.user.mapper.UserMapper.findByID ";
  UserPO user = new UserPO();
  user.setUserId(100);
  UserPO u = session.selectOne(statement, user);
  System.out.println("First query: "+u.getUserName());
  
  user = new UserPO();
  user.setUserId(100);
  u = session.selectOne(statement, user);
  System.out.println("Second query: "+u.getUserName());
  
  session.close();
 }
 
 /**
  * mybatis first level cache: sql in the same session will be cached:
  * the query statement is the same and the query parameters are different, the database will still be queried for the second time
  * @param factory
  */
 static void mybatisFirstCache2(DefaultSqlSessionFactory factory){
  SqlSession session = factory.openSession();
  
  String statement = "cn.mgr.user.mapper.UserMapper.findByID";
  UserPO user = new UserPO();
  user.setUserId(100);
  UserPO u = session.selectOne(statement, user);
  System.out.println("First query: "+u.getUserName());
  
  user = new UserPO();
  user.setUserId (101);
  u = session.selectOne(statement, user);
  System.out.println("Second query: "+u.getUserName());
  
  session.close();
 }
 
 /**
  * mybatis level 1 Cache: Different sessions
  * with the same query statement and different query parameters, will still query the database for the second time
  * @param factory
  */
 static void mybatisFirstCache3(DefaultSqlSessionFactory factory){
  SqlSession session = factory.openSession();
  
  String statement = "cn. mgr.user.mapper.UserMapper.findByID";
  UserPO user = new UserPO();
  user.setUserId(100);
  UserPO u = session.selectOne(statement, user);
  System.out.println("第一次查询:"+u.getUserName());
  session.close();
  
  session = factory.openSession();
  user = new UserPO();
  user.setUserId(100);
  u = session.selectOne(statement, user);
  System.out.println("第二次查询:"+u.getUserName());
  
  session.close();
 }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326528031&siteId=291194637