mybatis 缓存

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,这些参数在必要时做相应调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值