pom:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${org.mybatis-version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.0-RC1</version>
</dependency>
log4j:
<logger name="org.apache.ibatis">
<level value="debug" />
</logger>
<logger name="com.mysql.jdbc">
<level value="debug" />
</logger>
<logger name="org.mybatis">
<level value="debug" />
</logger>
<!-- 显示sql -->
<logger name="java.sql">
<level value="debug" />
</logger>
mapper:
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import cn.wolaidian.db.domain.Content;
@CacheNamespace(implementation=org.mybatis.caches.ehcache.EhcacheCache.class)
public interface ContentMapper {
@Insert("insert into content values(default, #{typeName}, #{title}, #{titlePic}, #{content}, #{source}, #{author}, default, default, default, #{state})")
@Options(flushCache=true)
public void saveContent(Content content);
@Delete("delete from content where id = #{contentID}")
@Options(flushCache=true)
public void deleteContent(int contentID);
@Select("select * from content where id = #{contentID}")
@Results({
@Result(property = "typeName", column = "type_name"),
@Result(property = "titlePic", column = "title_pic"),
@Result(property = "createTime", column = "create_time")
})
public Content getContent(int contentID);
@Options(flushCache=true)
public void updateContent(Content content);
}
结果:
DEBUG: org.mybatis.spring.SqlSessionUtils - Creating SqlSession from SqlSessionFactory
DEBUG: java.sql.Connection - ooo Connection Opened
DEBUG: org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession
DEBUG: org.apache.ibatis.cache.decorators.LoggingCache - Cache Hit Ratio [cn.wolaidian.db.mapper.ContentTypeMapper]: 0.0
DEBUG: java.sql.PreparedStatement - ==> Executing: select * from content_type
DEBUG: java.sql.PreparedStatement - ==> Parameters:
DEBUG: java.sql.ResultSet - <== Columns: id, name, title, detail, create_time, state
DEBUG: java.sql.ResultSet - <== Row: 1, news, ????, ????, 2011-08-10 10:16:41.0, 1
DEBUG: java.sql.ResultSet - <== Row: 2, products, ????, ????, 2011-08-10 10:44:47.0, 1
DEBUG: java.sql.ResultSet - <== Row: 3, examples, ????, ????, 2011-08-10 10:45:09.0, 1
DEBUG: java.sql.ResultSet - <== Row: 4, knowledge, ???, ???, 2011-08-10 10:45:32.0, 1
DEBUG: java.sql.ResultSet - <== Row: 5, industry, ????, ????, 2011-08-10 10:48:56.0, 1
DEBUG: java.sql.ResultSet - <== Row: 6, activity, ????, ????, 2011-08-10 10:53:33.0, 1
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization committed SqlSession
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization closed SqlSession
DEBUG: org.mybatis.spring.SqlSessionUtils - Creating SqlSession from SqlSessionFactory
DEBUG: java.sql.Connection - ooo Connection Opened
DEBUG: org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession
DEBUG: org.apache.ibatis.cache.decorators.LoggingCache - Cache Hit Ratio [cn.wolaidian.db.mapper.ContentTypeMapper]: 0.0
DEBUG: java.sql.PreparedStatement - ==> Executing: select id, type_name, title, title_pic, substring(replace(replace(content, '<p>', ''), '</p>', ''), 1, 150) as content, create_time , source, author, state from content where type_name = ? limit 0, ?
DEBUG: java.sql.PreparedStatement - ==> Parameters: industry(String), 8(Integer)
DEBUG: java.sql.ResultSet - <== Columns: id, type_name, title, title_pic, content, create_time, source, author, state
DEBUG: java.sql.ResultSet - <== Row: 7, industry, ????1, null,
????1
, 2011-08-10 10:51:26.0, ????1, ????1, 1
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization committed SqlSession
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization closed SqlSession
DEBUG: org.mybatis.spring.SqlSessionUtils - Creating SqlSession from SqlSessionFactory
DEBUG: java.sql.Connection - ooo Connection Opened
DEBUG: org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession
DEBUG: org.apache.ibatis.cache.decorators.LoggingCache - Cache Hit Ratio [cn.wolaidian.db.mapper.ContentTypeMapper]: 0.0
DEBUG: java.sql.PreparedStatement - ==> Executing: select id, type_name, title, title_pic, substring(replace(replace(content, '<p>', ''), '</p>', ''), 1, 150) as content, create_time , source, author, state from content where type_name = ? limit 0, ?
DEBUG: java.sql.PreparedStatement - ==> Parameters: knowledge(String), 8(Integer)
DEBUG: java.sql.ResultSet - <== Columns: id, type_name, title, title_pic, content, create_time, source, author, state
DEBUG: java.sql.ResultSet - <== Row: 5, knowledge, ???1, null,
???1
, 2011-08-10 10:47:18.0, ???1, ???1, 1
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization committed SqlSession
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization closed SqlSession
DEBUG: org.mybatis.spring.SqlSessionUtils - Creating SqlSession from SqlSessionFactory
DEBUG: java.sql.Connection - ooo Connection Opened
DEBUG: org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession
DEBUG: org.apache.ibatis.cache.decorators.LoggingCache - Cache Hit Ratio [cn.wolaidian.db.mapper.ContentTypeMapper]: 0.25
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization committed SqlSession
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization closed SqlSession
DEBUG: org.mybatis.spring.SqlSessionUtils - Creating SqlSession from SqlSessionFactory
DEBUG: java.sql.Connection - ooo Connection Opened
DEBUG: org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession
DEBUG: org.apache.ibatis.cache.decorators.LoggingCache - Cache Hit Ratio [cn.wolaidian.db.mapper.ContentTypeMapper]: 0.4
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization committed SqlSession
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization closed SqlSession
DEBUG: org.mybatis.spring.SqlSessionUtils - Creating SqlSession from SqlSessionFactory
DEBUG: java.sql.Connection - ooo Connection Opened
DEBUG: org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession
DEBUG: org.apache.ibatis.cache.decorators.LoggingCache - Cache Hit Ratio [cn.wolaidian.db.mapper.ContentTypeMapper]: 0.5
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization committed SqlSession
DEBUG: org.mybatis.spring.SqlSessionUtils - Transaction synchronization closed SqlSession
注:缓存默认为可读写,刷新间隔为3600000ms,缓存大小为1000,这些参数在必要时做相应调整。
分享到:
相关推荐
MyBatis缓存(一级缓存、二级缓存)
mybatis支持缓存,如果我们查找数据库中某一条记录时,先从缓存中获取,如果缓存中不存在该记录,则从数据库中获取,在放入到缓存中。该文档是关于mybatis使用一级或二级缓存的介绍
通过log4j打印来查看mybatis缓存实现的机制, 原博客http://blog.csdn.net/zouxucong/article/details/68947052
Mybatis系列教程Mybatis缓存共17页.pdf.zip
Mybatis缓存开源架构源码2021.pdf
【MyBatis学习笔记八】——MyBatis缓存.zip 博客地址:https://blog.csdn.net/weixin_43817709/article/details/117601742
MyBatis缓存机制深度解剖[收集].pdf
介绍:一个很简单的Maven项目,对Mybatis的缓存进行测试
主要介绍了Mybatis 缓存原理及失效情况解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
springmvc整合Mybatis,Redis;实现将查询的数据进行二级缓存处理
主要介绍了MyBatis缓存实现原理及代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
mybatis的缓存配置文件,用于复制黏贴到配置文件中 yes
个人学习mybatis源码总结
学习MyBatis框架的一级缓存和二级缓存,明确缓存的工作机制,并实现MyBatis框架与第三方缓存EhCache的整合。
NULL 博文链接:https://jysemel.iteye.com/blog/2018922
主要介绍了MyBatis缓存功能原理及实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要处理mybatis层对数据进行缓存处理问题,在数据底层进行优化
MyBatis缓存分为一级缓存和二级缓存一级缓存,本文给大家介绍mybatis缓存知识,非常不错具有参考借鉴价值,感兴趣的朋友一起学习吧
MyBatis整合Ehcache第三方缓存。