mybatis与redis整合

redis与ehcache都是专业的缓存框架,redis与mybatis的整合方式与ehcache类似,不同的是,redis需要先搭建自己的redis服务,用来存放缓存数据。

这里主要演示mybatis与redis的整合过程,redis服务的搭建可以参考 Java连接redis简单demo示例,这里使用本地的Windows环境的redis服务,与mybatis进行整合。

1、添加redis依赖

	<!-- 增加mybatis-redis依赖,这里只有beta版本 -->
	<dependency>
	    <groupId>org.mybatis.caches</groupId>
	    <artifactId>mybatis-redis</artifactId>
	    <version>1.0.0-beta2</version>
	</dependency>

2、配置redis

我们使用本地的redis服务,在mybatis工程中,src/main/resources目录下增加redis.properties文件

host=localhost
port=6379
connectionTimeout=5000
soTimeout=5000
password=123456
database=0
clientName=

上面是redis-cache项目提供的可以配置的参数,有服务器地址、端口、超时时间等。

3、UserMapper.xml配置

redis-cache提供了一个mybatis的缓存实现类,org.mybatis.caches.redis.RedisCache

修改UserMapper.xml为

<mapper namespace="cn.mybatis.xml.mapper.UserMapper">

	<!-- redis配置项 -->
	<cache type="org.mybatis.caches.redis.RedisCache" /> 
    ...

这里使用偷懒的方式,所有的redis配置均使用默认项

扫描二维码关注公众号,回复: 12685275 查看本文章

测试过程

1、启动redis服务

去C盘redis目录,启动redis服务

C:\Users\PC>cd C:\redis-64.3.0.503

C:\redis-64.3.0.503>redis-server.exe redis.windows.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.503 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1080
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[1080] 02 Jan 20:20:48.612 # Server started, Redis version 3.0.503
[1080] 02 Jan 20:20:48.632 * DB loaded from disk: 0.015 seconds
[1080] 02 Jan 20:20:48.636 * The server is now ready to accept connections on port 6379

2、进行Junit测试

可以看到日志

Cache Hit Ratio [cn.mybatis.xml.mapper.UserMapper]: 0.0
Opening JDBC Connection
Created connection 626742236.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@255b53dc]
==>  Preparing: select id,user_name,user_password,user_email,user_info,head_img,create_time from sys_user where id = ? 
==> Parameters: 1001(Long)
<==    Columns: id, user_name, user_password, user_email, user_info, head_img, create_time
<==        Row: 1001, test, 123456, [email protected], <<BLOB>>, <<BLOB>>, 2017-04-01 12:00:01.0
<==      Total: 1
test
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@255b53dc]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@255b53dc]
Returned connection 626742236 to pool.
Cache Hit Ratio [cn.mybatis.xml.mapper.UserMapper]: 0.5
test

证明,第二次查询时走的是redis缓存,没有查询数据库。

我们再次执行Junit测试语句,再次执行时,依然会重新连接数据库,获取session,执行结果

Cache Hit Ratio [cn.mybatis.xml.mapper.UserMapper]: 1.0
test
Cache Hit Ratio [cn.mybatis.xml.mapper.UserMapper]: 1.0
test

第二次的执行结果证明,将redis作为缓存服务器,它缓存的数据与数据和程序的启动无关,redis的缓存并不会因为应用的关闭而失效。所以,第二次执行测试语句时,所有的查询都是走的redis缓存,没有走数据库。

再打开一个cmd窗口,登录到redis后台,查看缓存对象,可以看到redis服务中有UserMapper对象

C:\Users\PC>cd c:\redis-64.3.0.503

c:\redis-64.3.0.503>redis-cli.exe -h 127.0.0.1 -p 6379 -a 123456
127.0.0.1:6379> keys *
1) "cn.mybatis.xml.mapper.UserMapper"
127.0.0.1:6379>

证明,缓存是成功的。

以上,mybatis与redisz整合的简单demo,纯属学习使用,供大家参考。

附:项目代码路径  mybatis与redis整合示例

猜你喜欢

转载自blog.csdn.net/magi1201/article/details/85635878