package cn.mybatis.xml;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import cn.mybatis.xml.model.Country;
public class CountryMapperTest extends BaseMapperTest{
/**
* 一级缓存测试
* 测试缓存后,再查询
*/
@Test
public void testCache1() {
SqlSession sqlSession = getSqlSession();
try {
// 第一次查询
Country country = sqlSession.selectOne("selectCountryById", 2l);
System.out.println(country.getCountryname() + ":" + country.getCountrycode());
// 通过日志可以发现,第二次查询并未到数据库查数据,说明第二次走的是缓存
Country country2 = sqlSession.selectOne("selectCountryById", 2l);
System.out.println(country2.getCountryname() + ":" + country2.getCountrycode());
} finally {
sqlSession.close();
}
}
/**
* 一级缓存测试
* 测试缓存后,增删改查,再查询
*/
@Test
public void testCache2() {
SqlSession sqlSession = getSqlSession();
try {
// 第一次查询
Country country = sqlSession.selectOne("selectCountryById", 2l);
System.out.println(country.getCountryname() + ":" + country.getCountrycode());
Country country2 = new Country();
country2.setId(7);
country2.setCountrycode("TW");
country2.setCountryname("中国台湾");
int result = sqlSession.insert("addCountry", country2);
if (result == 1) {
System.out.println("** insert success **");
}
// 由于进行了insert操作,第二次查询没有走缓存,直接走的数据库查询
Country country3 = sqlSession.selectOne("selectCountryById", 2l);
System.out.println(country3.getCountryname() + ":" + country3.getCountrycode());
} finally {
sqlSession.commit();
sqlSession.close();
}
}
/**
* 一级缓存测试
* 测试select查询,不存入缓存,再查询
*/
@Test
public void testCache3() {
SqlSession sqlSession = getSqlSession();
try {
// 第一次查询,但是SQL设置了flushCache="true",即查询结果不会缓存
Country country = sqlSession.selectOne("selectCountryById", 2l);
System.out.println(country.getCountryname() + ":" + country.getCountrycode());
// 通过日志可以发现,第二次查询依然查询了数据库,查询出来的结果依然不会缓存
Country country2 = sqlSession.selectOne("selectCountryById", 2l);
System.out.println(country2.getCountryname() + ":" + country2.getCountrycode());
} finally {
sqlSession.close();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
mybatis-cache.rar (37个子文件)
mybatis-cache
.project 565B
pom.xml 2KB
target
test-classes
cn
mybatis
xml
BaseMapperTest.class 1KB
CountryMapperTest.class 2KB
UserMapperTest.class 2KB
classes
cn
mybatis
xml
app
Main.class 2KB
model
SysUser.class 2KB
Country.class 955B
mapper
CountryMapper.class 453B
CountryMapper.xml 1KB
UserMapper.xml 1KB
UserMapper.class 300B
log4j.properties 310B
META-INF
maven
xml
mybatis-xml
pom.xml 2KB
pom.properties 228B
MANIFEST.MF 104B
mybatis-config.xml 2KB
jdbc.properties 167B
DB.sql 2KB
.settings
org.eclipse.core.resources.prefs 296B
org.eclipse.jdt.core.prefs 664B
org.eclipse.m2e.core.prefs 90B
src
test
resources
java
cn
mybatis
xml
BaseMapperTest.java 966B
CountryMapperTest.java 3KB
UserMapperTest.java 2KB
main
resources
cn
mybatis
xml
mapper
CountryMapper.xml 1KB
UserMapper.xml 1KB
log4j.properties 310B
mybatis-config.xml 2KB
jdbc.properties 167B
DB.sql 2KB
java
cn
mybatis
xml
app
Main.java 1KB
model
Country.java 542B
SysUser.java 2KB
mapper
CountryMapper.java 682B
UserMapper.java 420B
.classpath 1KB
共 37 条
- 1
资源评论
姜小白-
- 粉丝: 1973
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功