MyBatis框架的一级缓存机制(SqlSession级别)

 代码:

User.java

package com.bean;

import java.io.Serializable;

public class User implements Serializable {
	private Integer id;
	private String name;
	private String sex;
	private Integer age;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String name, String sex, Integer age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}
	
}

FKSqlSessionFactory.java

package com.factory;

import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class FKSqlSessionFactory {
	
	private static SqlSessionFactory sqlSessionFactory = null;
	
	// 初始化创建SqlSessionFactory对象
	static{
		try {
			// 读取mybatis-config.xml文件
			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
			sqlSessionFactory = new SqlSessionFactoryBuilder()
					.build(inputStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 获取SqlSession对象的静态方法
	public static SqlSession getSqlSession(){
		return sqlSessionFactory.openSession();
	}

	// 获取SqlSessionFactory的静态方法
	public static SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}
}

UserMapper.java

package com.mapper;

import java.util.List;

import com.bean.User;

public interface UserMapper {
	// 根据id查询User
	User selectUserById(Integer id);
	
	// 查询所有User
	List selectAllUser();
		
	// 根据id删除User
	void deleteUserById(Integer id);
}

UserMapper.xml





  
  
  
  
  
  
  
  
  	 delete from tb_user3 where id = #{id}
  

mybatis-config.xml



  

	
	
	
		
	
	
	
		
	
	
	
    
    
      
      
      
        
        
        
        
      
    
  
  
  
  	
  

TestOneLevelCache.java

package com.test;

import org.apache.ibatis.session.SqlSession;

import com.bean.User;
import com.factory.FKSqlSessionFactory;
import com.mapper.UserMapper;

public class TestOneLevelCache {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        TestOneLevelCache test = new TestOneLevelCache();
      //test.testCache1();
      //test.testCache2();
        test.testCache3();
	}

	/*
	 * 一级缓存: 也就Session级的缓存(默认开启)
	 */
	public void testCache1 (){
		// 使用工厂类获得SqlSession对象
		SqlSession session=FKSqlSessionFactory.getSqlSession();
		// 获得UserMapping对象
		UserMapper um=session.getMapper(UserMapper.class);
		// 查询id为1的User对象,会执行select语句
		User user=um.selectUserById(1);
		System.out.println(user);
		// 再次查询id为1的User对象,因为是同一个SqlSession,所以会从之前的一级缓存中查找数据
		User user2=um.selectUserById(1);
		System.out.println(user2);
		session.close();
	}
	
	public void testCache2 (){
		// 使用工厂类获得SqlSession对象
		SqlSession session = FKSqlSessionFactory.getSqlSession();
		// 获得UserMapping对象
		UserMapper um = session.getMapper(UserMapper.class);
		// 查询id为1的User对象,会执行select语句
		User user = um.selectUserById(1);
		System.out.println(user);
		// 执行delete操作
		um.deleteUserById(4);
		session.commit();
		// 再次查询id为1的User对象,因为DML操作会清空SqlSession缓存,所以会再次执行select语句
		User user2 = um.selectUserById(1);
		System.out.println(user2);
		session.close();
	}
	
	public void testCache3 (){
		// 使用工厂类获得SqlSession对象
		SqlSession session = FKSqlSessionFactory.getSqlSession();
		// 获得UserMapping对象
		UserMapper um = session.getMapper(UserMapper.class);
		// 查询id为1的User对象,会执行select语句
		User user = um.selectUserById(1);
		System.out.println(user);
		// 关闭一级缓存
		session.close();
		// 再次访问,需要再次获取一级缓存,然后才能查找数据,否则会抛出异常
		session = FKSqlSessionFactory.getSqlSession();
		// 再次获得UserMapping对象
		um = session.getMapper(UserMapper.class);
		// 再次访问,因为现在使用的是一个新的SqlSession对象,所以会再次执行select语句
		User user2 = um.selectUserById(1);
		System.out.println(user2);
		// 关闭SqlSession对象
		session.close();
	}
}

截图:


你可能感兴趣的:(MyBatis,框架学习笔记)