spring-boot集成mybatis,用redis做缓存
网上有很多例子了,执行源码起码有3个,都是各种各样的小问题。
现在做了个小demo,实现spring-boot 用redis做缓存的实例,简单记录下思路,分享下源码。
缓存的实现,分担了数据库的压力,在CRUD中
C:需要同时更新redis和mysql中的数据。
R:看redis中 有无数据,有,就从缓存中取,没有就从数据库中取,同时更新缓存。
U:删除redis中的数据,并update数据库。
D:删除redis和mysql的数据。
1、基本步骤
(1)redis安装 http://download.csdn.net/download/xsx531588644/9565831
(2)配置pom.xml
(3)添加工具类
(4)添加项目启动扫描类
(5)修改controller
2、实现
(1)安装redis
有一个redis图形管理工具很好用RedisManager,可以一起装上。
引入操作redis的客户端包jedis,见下方的总结。
(2)配置pom.xml
添加代码
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency> <!-- redis cache related.....start -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
完整pom.xml代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sun</groupId>
<artifactId>spring-boot-test</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent> <dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency> <!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency> <!-- redis cache related.....start -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
(3)添加redis工具类
package com.sun.beans; import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service; @Service
public class RedisCacheUtil<T>
{ @Autowired @Qualifier("jedisTemplate")
public RedisTemplate redisTemplate; /**
* 缓存基本的对象,Integer、String、实体类等
* @param key 缓存的键值
* @param value 缓存的值
* @return 缓存的对象
*/
public <T> ValueOperations<String,T> setCacheObject(String key,T value)
{ ValueOperations<String,T> operation = redisTemplate.opsForValue();
operation.set(key,value);
return operation;
} /**
* 获得缓存的基本对象。
* @param key 缓存键值
* @param operation
* @return 缓存键值对应的数据
*/
public <T> T getCacheObject(String key/*,ValueOperations<String,T> operation*/)
{
ValueOperations<String,T> operation = redisTemplate.opsForValue();
return operation.get(key);
} /**
* 缓存List数据
* @param key 缓存的键值
* @param dataList 待缓存的List数据
* @return 缓存的对象
*/
public <T> ListOperations<String, T> setCacheList(String key,List<T> dataList)
{
ListOperations listOperation = redisTemplate.opsForList();
if(null != dataList)
{
int size = dataList.size();
for(int i = 0; i < size ; i ++)
{ listOperation.rightPush(key,dataList.get(i));
}
} return listOperation;
} /**
* 获得缓存的list对象
* @param key 缓存的键值
* @return 缓存键值对应的数据
*/
@SuppressWarnings({ "unchecked", "hiding" })
public <T> List<T> getCacheList(String key)
{
List<T> dataList = new ArrayList<T>();
ListOperations<String,T> listOperation = redisTemplate.opsForList();
Long size = listOperation.size(key); for(int i = 0 ; i < size ; i ++)
{
dataList.add((T) listOperation.leftPop(key));
} return dataList;
} /**
* 缓存Set
* @param key 缓存键值
* @param dataSet 缓存的数据
* @return 缓存数据的对象
*/
@SuppressWarnings({ "unchecked", "hiding" })
public <T> BoundSetOperations<String,T> setCacheSet(String key,Set<T> dataSet)
{
BoundSetOperations<String,T> setOperation = redisTemplate.boundSetOps(key);
/*T[] t = (T[]) dataSet.toArray();
setOperation.add(t);*/ Iterator<T> it = dataSet.iterator();
while(it.hasNext())
{
setOperation.add(it.next());
} return setOperation;
} /**
* 获得缓存的set
* @param key
* @param operation
* @return
*/
public Set<T> getCacheSet(String key/*,BoundSetOperations<String,T> operation*/)
{
Set<T> dataSet = new HashSet<T>();
BoundSetOperations<String,T> operation = redisTemplate.boundSetOps(key); Long size = operation.size();
for(int i = 0 ; i < size ; i++)
{
dataSet.add(operation.pop());
}
return dataSet;
} /**
* 缓存Map
* @param key
* @param dataMap
* @return
*/
public <T> HashOperations<String,String,T> setCacheMap(String key,Map<String,T> dataMap)
{ HashOperations hashOperations = redisTemplate.opsForHash();
if(null != dataMap)
{ for (Map.Entry<String, T> entry : dataMap.entrySet()) { /*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */
hashOperations.put(key,entry.getKey(),entry.getValue());
} } return hashOperations;
} /**
* 获得缓存的Map
* @param key
* @param hashOperation
* @return
*/
public <T> Map<String,T> getCacheMap(String key/*,HashOperations<String,String,T> hashOperation*/)
{
Map<String, T> map = redisTemplate.opsForHash().entries(key);
/*Map<String, T> map = hashOperation.entries(key);*/
return map;
} /**
* 缓存Map
* @param key
* @param dataMap
* @return
*/
public <T> HashOperations<String,Integer,T> setCacheIntegerMap(String key,Map<Integer,T> dataMap)
{
HashOperations hashOperations = redisTemplate.opsForHash();
if(null != dataMap)
{ for (Map.Entry<Integer, T> entry : dataMap.entrySet()) { /*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */
hashOperations.put(key,entry.getKey(),entry.getValue());
} } return hashOperations;
} /**
* 获得缓存的Map
* @param key
* @param hashOperation
* @return
*/
public <T> Map<Integer,T> getCacheIntegerMap(String key/*,HashOperations<String,String,T> hashOperation*/)
{
Map<Integer, T> map = redisTemplate.opsForHash().entries(key);
/*Map<String, T> map = hashOperation.entries(key);*/
return map;
}
}
(4)添加redis监听,项目启动时自动缓存数据
package com.sun.beans;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.annotation.Resource; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service; import com.sun.dao.UserMapper;
import com.sun.model.User; /*
* 监听器,用于项目启动的时候初始化信息
*/
@Service
public class StartAddCacheListener implements ApplicationListener<ContextRefreshedEvent>
{
@Resource
private RedisCacheUtil<Object> redisCache; @Resource
private UserMapper userMapper; @Override
public void onApplicationEvent(ContextRefreshedEvent event)
{
//spring 启动的时候缓存user等信息
if(event.getApplicationContext().getParent() == null)
{
System.out.println("\n\n\n_________\n\n缓存数据 \n\n ________\n\n\n\n");
List<User> userList = userMapper.selectAll(); Map<Integer,User> userMap = new HashMap<Integer,User>(); int cityListSize = userList.size(); for(int i = 0 ; i < cityListSize ; i ++ )
{
userMap.put(userList.get(i).getId(), userList.get(i));
} redisCache.setCacheIntegerMap("userMap", userMap);
}
} }
(5)修改TestController添加如下代码:
@Autowired
private RedisCacheUtil<User> redisCache; @RequestMapping("/testGetCache")
public String testGetCache()
{ System.out.println("------------user");
Map<Integer,User> userMap = redisCache.getCacheIntegerMap("userMap");
for(int key : userMap.keySet())
{
System.out.println("key = " + key + ",value=" + userMap.get(key));
}
return userMap.toString();
}
注意:这里要序列化User实体类,也就是public class User implements Serializable{
3、总结
(1)redis是用客户端jedis操作的。所以jedis包我都是手动添加,工程根节点building path-》


这样添加的。如果你有更好的方式,请留言。版本号是2.9.0。
而且做这个redis的时候我发现版本号的问题十分讨厌,在网上下了redis的windows安装版本,但是安装了却没有jar包,
所以又在网上下了redis的3.2.100版本,但是却在工程里不知道版本号怎么写,后来使用的例子代码多了,发现maven管理的jar包中最高有1.8的,

就一直这么写了,有大神可以分析下为什么吗?
(2)redis的Windows版是自启动的,这样我改了redis-server.conf密码后,在项目中仍然设置了密码,提示auth错误。
注释掉项目中的密码设置就可以了。
(3)在applicationContext.xml中设置redis的配置文件,查资料说2种方式都可以,但是发现一种是报错的,只有
<context:property-placeholder location="classpath*:redis.properties" ignore-unresolvable="true" /> 这样可行,不知道为什么?有大神解答么?
(4)添加了几个配置文件applicationContext.xml和redis.properties和spring-redis.xml。
需要注意在启动类中的扫描代码:
@ImportResource(locations={"classpath:applicationContext.xml"})
最后给一下源码https://github.com/sunfengjiajia/spring-boot-test
spring-boot集成mybatis,用redis做缓存的更多相关文章
- spring boot集成mybatis(1)
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot集成mybatis(2) - 使用pagehelper实现分页
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot集成mybatis(3) - mybatis generator 配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- Spring Boot集成MyBatis开发Web项目
1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...
- 详解Spring Boot集成MyBatis的开发流程
MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集. spring Boot是能支持快速创建Spring应用的Java框 ...
- spring boot集成MyBatis 通用Mapper 使用总结
spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...
- spring boot集成mybatis只剩两个sql 并提示 Cannot obtain primary key information from the database, generated objects may be incomplete
前言 spring boot集成mybatis时只生成两个sql, 搞了一个早上,终于找到原因了 找了很多办法都没有解决, 最后注意到生成sql的时候打印了一句话: Cannot obtain pri ...
- Spring Boot集成MyBatis的2种方式
目录 写在前面 准备工作 配置数据库驱动 配置数据源 原生集成MyBatis 依赖配置 注册MyBatis核心组件 定义并使用映射器 通过MyBatis-Spring-Boot-Starter集成 默 ...
- spring boot 集成 Mybatis,JPA
相对应MyBatis, JPA可能大家会比较陌生,它并不是一个框架,而是一组规范,其使用跟Hibernate 差不多,原理层面的东西就不多讲了,主要的是应用. Mybatis就不多说了,SSM这三个框 ...
随机推荐
- c++中的log函数
引入#include<cmath> 以e为底:log(exp(n)) 以10为底:log10(n) 以m为底:log(n)/log(m)
- oracle in 多个字段
oracle 使用in的时候使用多个字段 这个也是刚需啊. 最近有个需求,在一堆商品里面过滤出一些特定的商品类型.供应商的商品(同时满足). 需要的数量不多,但是可能会变化,所以做了两个配置表. 商品 ...
- Codeforces 1111 简要题解
文章目录 A题 B题 C题 D题 E题 传送门 A题 传送门 题意简述:把262626个英文字母分成两类A,BA,BA,B,AAA类字符可以转成AAA类字符,BBB类字符可以转成BBB类字符,问给出的 ...
- App测试基本流程详解(汇总整理)
前言 看过许多大神对APP测试的理解,博主总结了一下我们平时测试APP应该注意的一些测试点并结合大神的理解,总结出这篇文章. 一.测试周期 测试周期一般为两周,根据项目情况以及版本质量可适当缩短或延长 ...
- mysql mybatis useGeneratedKeys Field 'ID' doesn't have a default value的问题
原因是:创建表时没有让id自动增长: CREATE TABLE `STORAGE_VIRTUAL` ( `ID` ) NOT NULL AUTO_INCREMENT, `STORAGE_ID` ) N ...
- excel中vba求摩尔圆包线
Dim f As Double, f1 As Double, f2 As Double, df As Double, oxy() As Double, R() As Double, k As Doub ...
- understand试用笔记一阅读VS2010项目
一.查看vs2010项目 打开understand,File—New—Project...—Next—Next [向导第三步,选“Import Visual Sudio project files”] ...
- eclipse中tomcat启动设置参数
今天新接触一个项目,使用java+flex做的单机项目,由于模块太多,打成war后就有300M左右了.所以,启动的时候,比较慢,超过了e eclipse中默认的45s,当我进行修改启动事件后,有一 ...
- 简单的dialog,类似alert弹框
function alertPop(content){ $.dialog({ title:"提示", width:400, height:100, padding:'5px', c ...
- Arcgis Server Manager发布ArcGISTiledMapServiceLayer服务
用ArcgisServer manager发布一个ArcGISDynamicMapServiceLayer服务: Add New Service->填写name,type 选择Map servi ...