标签:
CREATE TABLE c_user(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20),
age INT
);
INSERT INTO c_user(NAME, age) VALUES(‘Tom‘, 12);
INSERT INTO c_user(NAME, age) VALUES(‘Jack‘, 11);
public class CUser implements Serializable{
private int id;
private String name;
private int age;
}
<select id="getUser" parameterType="int" resultType="CUser">
select * from c_user where id=#{id}
</select>
<update id="updateUser" parameterType="CUser">
update c_user set name=#{name}, age=#{age} where id=#{id}
</update>
@Test
public void testCacheOne1() {
SqlSessionFactory factory = MybatisUtils.getFactory();
SqlSession session = factory.openSession();
String statement = "cn.imentors.mybatis.test9.userMapper.getUser";
CUser user = session.selectOne(statement, 1);
System.out.println(user);
//默认一级缓冲是开启的
user = session.selectOne(statement, 1);
System.out.println(user);
System.out.println("----------------");
session.close();
}
@Test
public void testCacheOne2() {
SqlSessionFactory factory = MybatisUtils.getFactory();
SqlSession session = factory.openSession();
String statement = "cn.imentors.mybatis.test9.userMapper.getUser";
CUser user = session.selectOne(statement, 1);
System.out.println(user);
//执行了session.clearCache(),清空缓存
session.clearCache();
user = session.selectOne(statement, 1);
System.out.println(user);
System.out.println("----------------");
session.close();
}
@Test
public void testCacheOne3() {
SqlSessionFactory factory = MybatisUtils.getFactory();
SqlSession session = factory.openSession();
String statement = "cn.imentors.mybatis.test9.userMapper.getUser";
CUser user = session.selectOne(statement, 1);
System.out.println(user);
//执行CUD操作
session.update("cn.imentors.mybatis.test9.userMapper.updateUser", new CUser(1, "Tom", 13));
session.commit();
user = session.selectOne(statement, 1);
System.out.println(user);
System.out.println("----------------");
session.close();
}
@Test
public void testCacheOne4() {
SqlSessionFactory factory = MybatisUtils.getFactory();
SqlSession session = factory.openSession();
String statement = "cn.imentors.mybatis.test9.userMapper.getUser";
CUser user = session.selectOne(statement, 1);
System.out.println(user);
//不是同一个Session对象
session.close();
session = factory.openSession();
user = session.selectOne(statement, 1);
System.out.println(user);
System.out.println("----------------");
session.close();
}
<mapper namespace="cn.imentors.mybatis.test9.userMapper">
<cache/>
@Test
public void testCacheTwo() {
SqlSessionFactory factory = MybatisUtils.getFactory();
SqlSession session1 = factory.openSession();
SqlSession session2 = factory.openSession();
String statement = "cn.imentors.mybatis.test9.userMapper.getUser";
CUser user = session1.selectOne(statement, 1);
session1.commit();
System.out.println(user);
user = session2.selectOne(statement, 1);
session2.commit();
System.out.println(user);
}
<cache
eviction="FIFO" //回收策略为先进先出
flushInterval="60000" //自动刷新时间60s
size="512" //最多缓存512个引用对象
readOnly="true"/> //只读
标签:
原文地址:http://www.cnblogs.com/mentorStudio/p/4813294.html