|
1
2
3 |
1. 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 Cache 就将清空。2. 二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap存储,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如 Ehcache。3. 对于缓存数据更新机制,当某一个作用域(一级缓存Session/二级缓存Namespaces)的进行了 C/U/D 操作后,默认该作用域下所有 select 中的缓存将被clear。 |
|
1
2
3
4
5
6
7
8
9
10 |
/*用户表*/drop table if exists user;create table user( id int primary key auto_increment, username varchar(50) unique, password varchar(100), nickname varchar(50), salt varchar(100), locked boolean)engine=InnqDB default charset=utf8; |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 |
package com.mscncn.batis.model;public class User { private int id; private int age; private String userName; private String userAddress; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
|
1
2
3 |
public interface UserMapper { public User getUserById(int id);} |
|
1
2
3
4
5
6
7
8
9
10
11 |
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 这里namespace必须是PostsMapper接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry”--> <mapper namespace="com.mscncn.batis.mapper.UserMapper"> <!-- 这儿的resultType是配置在mybatis-config.xml中得别名 --> <select id="getUserById" parameterType="int" resultType="User"> select * from user where id=#{id} </select></mapper> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
@Test public void testgetUserById() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper mapper = sqlSession.getMapper(UserMapper.class); //注意,重写User的toString方法 User u1=mapper.getUserById(1); System.out.println(u1); User u2=mapper.getUserById(1); System.out.println(u2); sqlSession.commit();//这里一定要提交,不然数据进不去数据库中 } finally { sqlSession.close(); } } |
从以上结果中可以看出,两次调用getUserById方法,但是只有一次查询数据库的过程,这种现象产生的原因就是mybatis的一级缓存,并且一级缓存是默认开启的。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
@Test public void testCache2() { SqlSession sqlSession = sqlSessionFactory.openSession(); SqlSession sqlSession2 = sqlSessionFactory.openSession(); try { UserMapper mapper = sqlSession.getMapper(UserMapper.class); UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class); //注意,重写User的toString方法 User u1=mapper.getUserById(1); System.out.println(u1); User u2=mapper2.getUserById(1); System.out.println(u2); sqlSession.commit();//这里一定要提交,不然数据进不去数据库中 } finally { sqlSession.close(); } } |
两个session,分别查询id为1 的 User ,那么mybatis与数据库交互了两次,这样说明mybatis现在没有开启二级缓存,需要我们手动的开启。
|
1 |
public class User implements Serializable {...} |
|
1 |
org.apache.ibatis.cache.CacheException: Error serializing object. Cause: java.io.NotSerializableException: |
|
1
2
3 |
<settings><setting name="cacheEnabled" value="true"/></settings> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
@Test public void testCache2() { SqlSession sqlSession = sqlSessionFactory.openSession(); SqlSession sqlSession2 = sqlSessionFactory.openSession(); try { UserMapper mapper = sqlSession.getMapper(UserMapper.class); UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class); //注意,重写User的toString方法 User u1=mapper.getUserById(1); sqlSession.commit(); System.out.println(u1); User u2=mapper2.getUserById(1); System.out.println(u2); sqlSession.commit();//这里一定要提交,不然数据进不去数据库中 } finally { sqlSession.close(); } } |
|
1
2
3 |
<settings> <setting name="cacheEnabled" value="true" /> </settings> |
|
1 |
<cache /> |
|
1
2
3
4
5
6
7
8
9
10
11
12 |
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 这里namespace必须是PostsMapper接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry”--> <mapper namespace="com.mscncn.batis.mapper.UserMapper"> <cache /> <!-- 这儿的resultType是配置在mybatis-config.xml中得别名 --> <select id="getUserById" parameterType="int" resultType="User"> select * from user where id=#{id} </select></mapper> |