1.可以知道的是Mybatis的一级缓存SqlSession默认是开启的。
2.可以自己去开启二级缓存在config.xml文件中的properties下面可以加上这个配置:
3.可以知道缓存之后的同一个session,再次执行操作的时候是不用再次查询数据库的。
resources下面建立这几个文件:
db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/csjzjj
jdbc.username=root
jdbc.password=roothouzhicong
config.xml:
MyUserMapper:
package com.zjj.mapper;
import com.zjj.entity.MyUser;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @Program mybatis
* @Package com.zjj.mapper
* @Auther TeacherHuang
* @Date 2021/8/16 15:29
* @Version 1.0
*/
public interface MyUserMapper {
@Select(“select *from myuser”)
List selectAll() throws Exception;
@Select(“select *from myuser”)
List selectAll01() throws Exception;
}
Test:
import com.zjj.entity.MyUser;
import com.zjj.mapper.MyUserMapper;
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.ioIOException;
import java.io.InputStream;
import java.util.List;
/**
* @Program mybatis
* @Package PACKAGE_NAME
* @Auther TeacherHuang
* @Date 2021/8/16 15:31
* @Version 1.0
*/
public class Test {
public static void main(String[] args) throws Exception {
String resources=”config.xml”;
InputStream inputstream = Resources.getResourceAsStream(resources);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputstream);
SqlSession session = sqlSessionFactory.openSession(true);
System.out.println(session);
MyUserMapper myUserMapper = session.getMapper(MyUserMapper.class);
System.out.println(“第一次没有缓存数据的时候———————“);
List myUsers = myUserMapper.selectAll();
System.out.println(myUsers);
// session.close();
session.clearCache();
System.out.println(“第二次没有缓存数据的时候———————“);
MyUserMapper myUserMapper02 = session.getMapper(MyUserMapper.class);
List myUsers01 = myUserMapper.selectAll();
System.out.println(myUsers01);
}
}