mybatis一级和二级缓存
- package com.example.mapper;
-
- import com.example.pojo.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 org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
-
- import java.io.IOException;
- import java.io.InputStream;
-
- public class TestUsersMapper {
-
- //SqlSession对象
- SqlSession sqlSession;
-
- //mybatis动态代理对象
- UsersMapper usersMapper;
-
- //获取SqlSession
- @Before
- public void getSqlSession() throws IOException {
- //读取核心配置文件
- InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
- //创建SqlSessionFactory对象
- SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
- //获取SqlSession
- sqlSession = factory.openSession();
- //获取mybatis动态代理对象
- usersMapper = sqlSession.getMapper(UsersMapper.class);
- }
-
- //归还SqlSession
- @After
- public void closeSession(){
- sqlSession.close();
- }
-
- //测试mybatis一级缓存
- @Test
- public void testOneLevelCache(){
- //两次查询,id相同时
- User u1 = usersMapper.getById(1);
- System.out.println("第1次查询结果: " + u1);
- System.out.println("----------------------");
- User u2 = usersMapper.getById(1);
- System.out.println("第2次查询结果: " + u2);
- System.out.println("------------------------");
- if(u1 == u2){
- System.out.println("两个对象的内存地址相同");
- }else{
- System.out.println("两个对象的内存地址不同");
- }
- System.out.println("**************************************************");
- //两次查询,id不同时
- User u3 = usersMapper.getById(2);
- System.out.println("第1次查询结果: " + u3);
- System.out.println("----------------------");
- User u4 = usersMapper.getById(5);
- System.out.println("第2次查询结果: " + u4);
- System.out.println("------------------------");
- if(u3 == u4){
- System.out.println("两个对象的内存地址相同");
- }else{
- System.out.println("两个对象的内存地址不同");
- }
- }
- }
- ==> Preparing: select id, username, birthday, sex, address from users where id=?
- ==> Parameters: 1(Integer)
- <== Columns: id, username, birthday, sex, address
- <== Row: 1, 荷包蛋, 2002-08-23, 女, 黑河
- <== Total: 1
- 第1次查询结果: Users{id=1, userName='荷包蛋', birthday=Fri Aug 23 00:00:00 CST 2002, sex='女', address='黑河'}
- ----------------------
- 第2次查询结果: Users{id=1, userName='荷包蛋', birthday=Fri Aug 23 00:00:00 CST 2002, sex='女', address='黑河'}
- ------------------------
- 两个对象的内存地址相同
- **************************************************
- ==> Preparing: select id, username, birthday, sex, address from users where id=?
- ==> Parameters: 2(Integer)
- <== Columns: id, username, birthday, sex, address
- <== Row: 2, 小王, 2001-07-12, 1, 芜湖市
- <== Total: 1
- 第1次查询结果: Users{id=2, userName='小王', birthday=Thu Jul 12 00:00:00 CST 2001, sex='1', address='芜湖市'}
- ----------------------
- ==> Preparing: select id, username, birthday, sex, address from users where id=?
- ==> Parameters: 5(Integer)
- <== Columns: id, username, birthday, sex, address
- <== Row: 5, 段, 2001-03-10, 1, 太原
- <== Total: 1
- 第2次查询结果: Users{id=5, userName='段', birthday=Sat Mar 10 00:00:00 CST 2001, sex='1', address='太原'}
- ------------------------
- 两个对象的内存地址不同
- //获取SqlSession
- sqlSession = factory.openSession();
- public void testOneLevelCache() throws ParseException {
- //第1次查询,两次查询的id相同
- User u1 = usersMapper.getById(1);
- System.out.println("第1次查询结果: " + u1);
- System.out.println("----------------------");
-
- //中间发生commit操作
- int num = usersMapper.insert(new User(8, "小涵", new SimpleDateFormat("yyyy-MM-dd").parse("2002-08-23"), "女", "黑河"));
- if(num == 1){
- System.out.println("数据插入成功!");
- sqlSession.commit();
- }else{
- System.out.println("数据插入失败!");
- }
- System.out.println("---------------------------");
- //第二次查询,与第一次查询的id相同
- User u2 = usersMapper.getById(1);
- System.out.println("第2次查询结果: " + u2);
- System.out.println("------------------------");
- if(u1 == u2){
- System.out.println("两个对象的内存地址相同");
- }else{
- System.out.println("两个对象的内存地址不同");
- }
- }
- ==> Preparing: select id, username, birthday, sex, address from users where id=?
- ==> Parameters: 1(Integer)
- <== Columns: id, username, birthday, sex, address
- <== Row: 1, 荷包蛋, 2002-08-23, 女, 黑河
- <== Total: 1
- 第1次查询结果: Users{id=1, userName='荷包蛋', birthday=Fri Aug 23 00:00:00 CST 2002, sex='女', address='黑河'}
- ----------------------
- ==> Preparing: insert into users(username, birthday, sex, address) values(?, ?, ?, ?)
- ==> Parameters: 小涵(String), 2002-08-23 00:00:00.0(Timestamp), 女(String), 黑河(String)
- <== Updates: 1
- 数据插入成功!
- Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5c18016b]
- ---------------------------
- ==> Preparing: select id, username, birthday, sex, address from users where id=?
- ==> Parameters: 1(Integer)
- <== Columns: id, username, birthday, sex, address
- <== Row: 1, 荷包蛋, 2002-08-23, 女, 黑河
- <== Total: 1
- 第2次查询结果: Users{id=1, userName='荷包蛋', birthday=Fri Aug 23 00:00:00 CST 2002, sex='女', address='黑河'}
- ------------------------
- 两个对象的内存地址不同