Mybatis与Ehcache整合

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Mybatis与Ehcache整合
Mybatis与Ehcache整合可以提高性能,降低数据库压力。

查询百度发现整合Mybatis与Ehcache 其实非常简单的。

1.
下载mybatis相关包与ehcache相关包
下载地址为:https://github./mybatis/ehcache-cache/releases
作者下载的是
mybatis-ehcache-1.0.3版本
其中自带了
mybatis-ehcache-1.0.3.jar
ehcache-core-2.6.8.jar
slf4j-api-1.6.1.jar
将这三个包导入到项目的jar文件下,就可以使用ehcache功能了。

当然前提是保证mybatis没有引入ehcache前,项目也能正常运行。

作者用的mybatis3.2版本,框架用了SpringMVC3.2等等。

现在就试试效果吧。

在Map文件中打开echached效果,account-mapper.xml文件内容如下,(有的文章说还需要配置ehcache.xml,作者测试根本不需要这个配置):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace=".springdemo.mapper.AccountMapper">
<!--mybatis ehcache缓存配置-->
<!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志--><cache type="org.mybatis.caches.ehcache.LoggingEhcache" />
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
<!-- 以下与实体类的中字段一致-->
<sql id="selectId">
id,
accountName,
(select group_concat(name) from ly_role
where ly_role.id
in (SELECT role_id FROM acc_role WHERE
acc_id=account.id) ) roleName,
password,
description,
state,
createTime
</sql>
<!--resultType="Account" 每返回一条结果封装到Account里-->
<select id="queryAll" resultType="Account" parameterType="Account"> select
<include refid="selectId" />
from account
<where>
<if test="accountName != null and accountName != ''">
accountName like '%${accountName}%'
</if>
</where>
</select>
<select id="isExist" resultType="Account" parameterType="String"> select <include refid="selectId" />
from account
where accountName = #{accountName}
</select>
<!--resultType="Account" 每返回一条结果封装到Account里-->
<select id="query" resultType="Account" parameterType="Java.util.HashMap"> select
<include refid="selectId" />
from account
<where>
<if test="t.accountName != null and t.accountName != ''">
accountName like '%${t.accountName}%'
</if>
</where>
<if test="paging.startPage != null ">
limit #{paging.startPage} , #{paging.pageSize}
</if>
</select>
<!--resultType="Account" 记录条数,用于翻页查询-->
<select id="queryCount" resultType="long" parameterType="java.util.HashMap"> select count(*)
from account
<where>
<if test="t.accountName != null and t.accountName != ''">
accountName like '%${t.accountName}%'
</if>
</where>
</select>
<select id="queryNoMatch" resultType="Account" parameterType="java.util.HashMap"> select
a.id,
a.accountName,
a.password,
a.accountType, a.description,
a.state,
a.createTime,
(SELECT from department dp where dp.id =
d.subdep_id) depName
from account a LEFT JOIN dep_account d on
a.id=d.account_id
<where>
<if test="t.accountName != null and t.accountName != ''"> accountName like '%${t.accountName}%'
</if>
</where>
</select>
<!-- 增加用户-->
<insert id="add" parameterType="Account">
insert into account (
accountName,
password,
description,
state )
values (#{accountName},
#{password}, #{description},
#{state})
</insert>
<delete id="delete" parameterType="String">
delete from account where
id=#{id}
</delete>
<select id="getById" parameterType="String" resultType="Account"> select
<include refid="selectId" />
from account where id=#{id}
</select> <update id="update" parameterType="Account"> update account
<set>
<if test="accountName != null and accountName != ''"> accountName=#{accountName},
</if>
<if test="password != null and password != ''">
password=#{password},
</if>
<if test="description != null and description != ''">
description=#{description},
</if>
<if test="state != null and state != ''">
state=#{state},
</if>
<if test="createTime != null and createTime != ''">
createTime=#{createTime}
</if>
</set>
where id=#{id}
</update>
<!-- 验证用户登陆-->
<select id="countAccount" parameterType="Account" resultType="Account"> select
<include refid="selectId" />
from account where
accountName=#{accountName} and password=#{password}
</select>
<!-- 根据用户名查出id -->
<select id="querySingleAccount" parameterType="String"
resultType="Account">
select
<include refid="selectId" />
from account where accountName=#{accountName} </select>
</mapper>
然后运行Account的查询程序,未启用cache的日志如下:
DEBUG 2014-10-11 14:07:16,712
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 DispatcherServlet with name 'spring3' processing GET request for [/AnnExp2/background/account/list.do] DEBUG 2014-10-11 14:07:16,712
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-1 Looking up handler method for path /background/account/list.do DEBUG 2014-10-11 14:07:16,722
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-1 Returning handler method [public
ng.String ermgr.controller.AccountController.list(org.springframework.ui.
Model,ermgr.vo.Resources,ng.String)]
DEBUG 2014-10-11 14:07:16,722
org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-1 Returning cached instance of singleton bean 'accountController'
DEBUG 2014-10-11 14:07:16,722
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 Last-Modified value for [/AnnExp2/background/account/list.do] is: -1
3
DEBUG 2014-10-11 14:07:16,742
org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-1 Invoking afterPropertiesSet() on bean with name 'background/account/list'
DEBUG 2014-10-11 14:07:16,742
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 Rendering view [org.springframework.web.servlet.view.JstlView: name 'background/account/list'; URL
[/WEB-INF/view/background/account/list.jsp]] in DispatcherServlet with name 'spring3' DEBUG 2014-10-11 14:07:16,742
org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-1 Added model object
'resources' of type [ermgr.vo.Resources] to request in view with name
'background/account/list'
DEBUG 2014-10-11 14:07:16,742
org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-1 Added model object
'org.springframework.validation.BindingResult.resources' of type
[org.springframework.validation.BeanPropertyBindingResult] to request in view with name
'background/account/list'
DEBUG 2014-10-11 14:07:16,742
org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-1 Forwarding to resource [/WEB-INF/view/background/account/list.jsp] in InternalResourceView
'background/account/list'
2
DEBUG 2014-10-11 14:07:16,762
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 Successfully pleted request
DEBUG 2014-10-11 14:07:16,762
org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-1 Returning cached instance of singleton bean 'sqlSessionFactory'
DEBUG 2014-10-11 14:07:16,995
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 DispatcherServlet
with name 'spring3' processing POST request for [/AnnExp2/background/account/query.do] DEBUG 2014-10-11 14:07:16,995
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-1 Looking up handler method for path /background/account/query.do DEBUG 2014-10-11 14:07:17,005
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-1 Returning handler method
[public .lanyuan.pulgin.mybatis.plugin.PageView ermgr.controller.AccountCont roller.query(ermgr.vo.Account,ng.String,ng.String)]
DEBUG 2014-10-11 14:07:17,005
org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-1 Returning cached instance of singleton bean 'accountController'
DEBUG 2014-10-11 14:07:17,005 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1 Creating a new SqlSession
DEBUG 2014-10-11 14:07:17,005 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1 SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession1a891095] was not registered for synchronization because synchronization is not active
DEBUG 2014-10-11 14:07:17,049 .springdemo.mapper.AccountMapper.http-bio-8080-exec-1 Cache Hit Ratio [.springdemo.mapper.AccountMapper]: 0.0DEBUG 2014-10-11 14:07:17,050 org.springframework.jdbc.datasource.DataSourceUtils.http-bio-8080-exec-1 Fetching JDBC Connection from DataSource
DEBUG 2014-10-11 14:07:17,051
org.mybatis.spring.transaction.SpringManagedTransaction.http-bio-8080-exec-1 JDBC Connection [jdbc:MySQL://localhost:3306/test, UserName=rootlocalhost, mysql-AB JDBC Driver] will not be managed by Spring
DEBUG 2014-10-11
14:07:17,051 .springdemo.mapper.AccountMapper.queryCount.http-bio-8080-exec-1
==> Preparing: select count(*) from account
DEBUG 2014-10-11
14:07:17,051 .springdemo.mapper.AccountMapper.queryCount.http-bio-8080-exec-1 ==> Parameters:
DEBUG 2014-10-11
14:07:17,052 .springdemo.mapper.AccountMapper.queryCount.http-bio-8080-exec-1
<== Total: 1
DEBUG 2014-10-11 14:07:17,053 net.sf.ehcache.store.disk.Segment.http-bio-8080-exec-1 put added 0 on heap
DEBUG 2014-10-11 14:07:17,053 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1
Closing non transactional SqlSession
[org.apache.ibatis.session.defaults.DefaultSqlSession1a891095]
DEBUG 2014-10-11 14:07:17,053
org.springframework.jdbc.datasource.DataSourceUtils.http-bio-8080-exec-1 Returning JDBC Connection to DataSource
DEBUG 2014-10-11 14:07:17,053 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1 Creating a new SqlSession
DEBUG 2014-10-11 14:07:17,054 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1 SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession271ffe2f] was not registered for synchronization because synchronization is not active
DEBUG 2014-10-11 14:07:17,058 .springdemo.mapper.AccountMapper.http-bio-8080-exec-1 Cache Hit Ratio [.springdemo.mapper.AccountMapper]: 0.0DEBUG 2014-10-11 14:07:17,058 org.springframework.jdbc.datasource.DataSourceUtils.http-bio-8080-exec-1 Fetching JDBC Connection from DataSource
DEBUG 2014-10-11 14:07:17,059
org.mybatis.spring.transaction.SpringManagedTransaction.http-bio-8080-exec-1 JDBC Connection [jdbc:mysql://localhost:3306/test, UserName=rootlocalhost, MySQL-AB JDBC Driver] will not be managed by SpringDEBUG 2014-10-11
14:07:17,060 .springdemo.mapper.AccountMapper.query.http-bio-8080-exec-1
==> Preparing: select id, accountName, (select group_concat(name) from ly_role where
ly_role.id in (SELECT role_id FROM acc_role WHERE acc_id=account.id) ) roleName, password, description, state, createTime from account limit ? , ?
DEBUG 2014-10-11
14:07:17,060 .springdemo.mapper.AccountMapper.query.http-bio-8080-exec-1 ==> Parameters: 0(Integer), 5(Integer)
DEBUG 2014-10-11
14:07:17,066 .springdemo.mapper.AccountMapper.query.http-bio-8080-exec-1 <== Total: 5
DEBUG 2014-10-11 14:07:17,066 net.sf.ehcache.store.disk.Segment.http-bio-8080-exec-1 put added 0 on heap
DEBUG 2014-10-11 14:07:17,067 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1 Closing non transactional SqlSession
[org.apache.ibatis.session.defaults.DefaultSqlSession271ffe2f]
DEBUG 2014-10-11 14:07:17,067
org.springframework.jdbc.datasource.DataSourceUtils.http-bio-8080-exec-1 Returning JDBC Connection to DataSource
DEBUG 2014-10-11 14:07:17,078
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProc essor.http-bio-8080-exec-1 Written [.lanyuan.pulgin.mybatis.plugin.PageView380b2e5d] as "application/json;charset=UTF-8" using
[org.springframework.http.converter.json.MappingJacksonHttpMessageConverter1da6205a] 3
DEBUG 2014-10-11 14:07:17,078
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 Null ModelAndView returned to DispatcherServlet with name 'spring3': assuming HandlerAdapter pleted request handling
2
DEBUG 2014-10-11 14:07:17,078
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 Successfully pleted request
DEBUG 2014-10-11 14:07:17,078
org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-1 Returning cached instance of singleton bean 'sqlSessionFactory'
DEBUG 2014-10-11 14:07:17,099
net.sf.ehcache.store.disk.Segment.%002espringdemo%002emapper%002e%0041ccount%0 04dapper.data fault removed 0 from heap
DEBUG 2014-10-11 14:07:17,099
net.sf.ehcache.store.disk.Segment.%002espringdemo%002emapper%002e%0041ccount%0 04dapper.data fault added 0 on disk
DEBUG 2014-10-11 14:07:17,100
net.sf.ehcache.store.disk.Segment.%002espringdemo%002emapper%002e%0041ccount%0 04dapper.data fault removed 0 from heap
DEBUG 2014-10-11 14:07:17,101
net.sf.ehcache.store.disk.Segment.%002espringdemo%002emapper%002e%0041ccount%0 04dapper.data fault added 0 on disk启用cache的命中后日志如下:
DEBUG 2014-10-11 14:07:52,907
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 DispatcherServlet with name 'spring3' processing GET request for [/AnnExp2/background/account/list.do] DEBUG 2014-10-11 14:07:52,907
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-5 Looking up handler method for path /background/account/list.do DEBUG 2014-10-11 14:07:52,917
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht
tp-bio-8080-exec-5 Returning handler method [public
ng.String ermgr.controller.AccountController.list(org.springframework.ui. Model,ermgr.vo.Resources,ng.String)]
DEBUG 2014-10-11 14:07:52,917
org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-5 Returning cached instance of singleton bean 'accountController'
DEBUG 2014-10-11 14:07:52,917
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 Last-Modified value for [/AnnExp2/background/account/list.do] is: -1
3
DEBUG 2014-10-11 14:07:52,917
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 Rendering view [org.springframework.web.servlet.view.JstlView: name 'background/account/list'; URL
[/WEB-INF/view/background/account/list.jsp]] in DispatcherServlet with name 'spring3' DEBUG 2014-10-11 14:07:52,917
org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-5 Added model object
'resources' of type [ermgr.vo.Resources] to request in view with name
'background/account/list'
DEBUG 2014-10-11 14:07:52,917
org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-5 Added model object
'org.springframework.validation.BindingResult.resources' of type
[org.springframework.validation.BeanPropertyBindingResult] to request in view with name
'background/account/list'
DEBUG 2014-10-11 14:07:52,917
org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-5 Forwarding to resource [/WEB-INF/view/background/account/list.jsp] in InternalResourceView
'background/account/list'
2
DEBUG 2014-10-11 14:07:52,917
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 Successfully pleted request
DEBUG 2014-10-11 14:07:52,917
org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-5 Returning cached instance of singleton bean 'sqlSessionFactory'
DEBUG 2014-10-11 14:07:53,121
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 DispatcherServlet with name 'spring3' processing POST request for [/AnnExp2/background/account/query.do]
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-5 Looking up handler method for path /background/account/query.do DEBUG 2014-10-11 14:07:53,131
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-5 Returning handler method
[public .lanyuan.pulgin.mybatis.plugin.PageView ermgr.controller.AccountCont roller.query(ermgr.vo.Account,ng.String,ng.String)]
DEBUG 2014-10-11 14:07:53,131
org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-5 Returning cached instance of singleton bean 'accountController'
DEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 Creating a new SqlSession
DEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession61b38786] was not registered for synchronization because synchronization is not active DEBUG 2014-10-11 14:07:53,131 .springdemo.mapper.AccountMapper.http-bio-8080-exec-5 Cache Hit Ratio [.springdemo.mapper.AccountMapper]: 0.25
DEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 Closing non transactional SqlSession
[org.apache.ibatis.session.defaults.DefaultSqlSession61b38786]
DEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 Creating a new SqlSession
DEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession5d94e73a] was not registered for synchronization because synchronization is not active DEBUG 2014-10-11 14:07:53,131 .springdemo.mapper.AccountMapper.http-bio-8080-exec-5 Cache Hit Ratio [.springdemo.mapper.AccountMapper]: 0.4
DEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 Closing non transactional SqlSession
[org.apache.ibatis.session.defaults.DefaultSqlSession5d94e73a]
DEBUG 2014-10-11 14:07:53,131
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProc essor.http-bio-8080-exec-5 Written [.lanyuan.pulgin.mybatis.plugin.PageView4c5c0e8b] as "application/json;charset=UTF-8" using
[org.springframework.http.converter.json.MappingJacksonHttpMessageConverter1da6205a] 3
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 Null ModelAndView returned to DispatcherServlet with name 'spring3': assuming HandlerAdapter pleted request handling
2
DEBUG 2014-10-11 14:07:53,131
org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 Successfully pleted request
DEBUG 2014-10-11 14:07:53,131
org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-5 Returning cached instance of singleton bean 'sqlSessionFactory'可以看到前后明显差异,命中cache后,查询效率要明显提高。

相关文档
最新文档