`
mrjeye
  • 浏览: 173911 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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,这些参数在必要时做相应调整。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics