测试 一级缓存默认执行 session级别的 当进行增删改的时候会清除session 再次执行查询sql
package com.mybatis.test;
import com.mybatis.mapper.UserMapper;
import com.mybatis.model.Orders;
import com.mybatis.model.User;
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 java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
/**
* 一级缓存
*/
public class Demo21 {
private static SqlSession sqlSession;
static {
// a)读取配置文件;
InputStream is = null;
try {
is = Resources.getResourceAsStream("SqlMapConfig.xml");
//b)通过SqlSessionFactoryBuilder创建SqlSessionFactory会话工厂。
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 一级缓存默认执行 session级别的
* 当进行增删改的时候会清除session 再次执行查询sql
* @param args
*/
public static void main(String[] args) {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.findUser(1);
System.out.println(user);
mapper.saveUser(new User("胖胖","1", new Date(),"南京"));
User user1 = mapper.findUser(1);
System.out.println(user1);
}
}