网上有很多例子了,执行源码起码有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做缓存的更多相关文章

  1. spring boot集成mybatis(1)

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  2. spring boot集成mybatis(2) - 使用pagehelper实现分页

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  3. spring boot集成mybatis(3) - mybatis generator 配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  4. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

  5. Spring Boot集成MyBatis开发Web项目

    1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...

  6. 详解Spring Boot集成MyBatis的开发流程

    MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集. spring Boot是能支持快速创建Spring应用的Java框 ...

  7. spring boot集成MyBatis 通用Mapper 使用总结

    spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...

  8. 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 ...

  9. Spring Boot集成MyBatis的2种方式

    目录 写在前面 准备工作 配置数据库驱动 配置数据源 原生集成MyBatis 依赖配置 注册MyBatis核心组件 定义并使用映射器 通过MyBatis-Spring-Boot-Starter集成 默 ...

  10. spring boot 集成 Mybatis,JPA

    相对应MyBatis, JPA可能大家会比较陌生,它并不是一个框架,而是一组规范,其使用跟Hibernate 差不多,原理层面的东西就不多讲了,主要的是应用. Mybatis就不多说了,SSM这三个框 ...

随机推荐

  1. 20175316盛茂淞-Java第1周学习总结

    20175316盛茂淞 2018-2019-2 <Java程序设计>第1周学习总结 教材学习内容总结 Java入门 1.Java简介(地位,特点) 2.安装JDK,设置系统环境 3.编译J ...

  2. centos7 安装网卡

    1.虚拟机测试,先开启命令行 su systemctl set-default multi-user.target reboot 2.编辑网卡 虚拟机网络设置成桥接模式 vi /etc/sysconf ...

  3. react创建项目报错unexpected end of json while parsing near xxx

    报这个错,执行下面的命令,然后重新创建项目就可以. npm cache clean --force

  4. 03 编写URL规则

    3-1 URL编写规则 # 在每个App中设置独立的静态资源和模板文件并添加一个空白内容的urls.py文件. # 当程序收到用户请求的时候,首先在根目录的urls.py查找该URL属于哪个APP,然 ...

  5. vue中moudles的作用及使用方法

    1.作用:vuex允许把store分割为模块,每一个模块都有自己的state,actions,getters,mutations甚至是嵌套一些子模块,从上到下进行同样方式的分割 在文件src中创建一个 ...

  6. Calendar 得到前一天当前时间

    @Test public void test(){ //因为Calendar的构造方法是私有的,所以实例化一个Calendar对象用getInstance方法 Calendar calendar = ...

  7. Nodejs入门【转载】保留备用

    关于 本书致力于教会你如何用Node.js来开发应用,过程中会传授你所有所需的“高级”JavaScript知识.本书绝不是一本“Hello World”的教程. 状态 你正在阅读的已经是本书的最终版. ...

  8. Eclipse环境下如何配置tomcat,并且把项目部署到Tomcat服务器上

    打开Eclipse,单击“Window”菜单,选择下方的“Preferences”. 单击“Server”选项,选择下方的“Runtime Environments”.  点击“Add”添加Tomca ...

  9. Eclipse workspace 被占用问题

    eclipse 使用一段时间后,有时会因为一些故障自己就莫名奇妙的关闭了,再打开时有时没有问题,有时会提示错误 Workspace Unavailable: Workspace in use or c ...

  10. spark的shuffle机制

    对于大数据计算框架而言,Shuffle阶段的设计优劣是决定性能好坏的关键因素之一.本文将介绍目前Spark的shuffle实现,并将之与MapReduce进行简单对比.本文的介绍顺序是:shuffle ...