1. 引入依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.data.redis.version>2.0.8.RELEASE</spring.data.redis.version><!--1.8.7.RELEASE-->
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring.data.redis.version}</version>
</dependency> <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency> </dependencies>

2. application.yml中添加配置

spring:
redis:
#数据库索引
database: 0
host: 127.0.0.1
port: 6379
password:
jedis:
pool:
#最大连接数
max-active: 8
#最大阻塞等待时间(负数表示没限制)
max-wait: -1
#最大空闲
max-idle: 8
#最小空闲
min-idle: 0
#连接超时时间
timeout: 10000

3. RedisConfiguration

@Configuration
@EnableCaching
public class RedisConfiguration extends CachingConfigurerSupport { @Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
} // @Bean
// public CacheManager cacheManager(RedisTemplate redisTemplate) {
// RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
// return redisCacheManager;
// }
//
//
// @Bean
// public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
// ////解决键、值序列化问题
// StringRedisTemplate template = new StringRedisTemplate(factory);
// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
// ObjectMapper om = new ObjectMapper();
// om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
// jackson2JsonRedisSerializer.setObjectMapper(om);
// template.setValueSerializer(jackson2JsonRedisSerializer);
// template.afterPropertiesSet();
// return template;
// } /*
spring-data-redis 版本不同,方法也不一样
上面是1.5
下面是2.0
*/ @Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory).build();
return redisCacheManager;
} /**
* @Description: 防止redis入库序列化乱码的问题
* @return 返回类型
* @date 2018/4/12 10:54
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());//key序列化
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class)); //value序列化 redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.afterPropertiesSet();
return redisTemplate;
}

4. RedisService

@Component
public class RedisService<HK, V> { // 在构造器中获取redisTemplate实例, key(not hashKey) 默认使用String类型
private RedisTemplate<String, V> redisTemplate;
// 在构造器中通过redisTemplate的工厂方法实例化操作对象
private HashOperations<String, HK, V> hashOperations;
private ListOperations<String, V> listOperations;
private ZSetOperations<String, V> zSetOperations;
private SetOperations<String, V> setOperations;
private ValueOperations<String, V> valueOperations; // IDEA虽然报错,但是依然可以注入成功, 实例化操作对象后就可以直接调用方法操作Redis数据库
@Autowired
public RedisService(RedisTemplate<String, V> redisTemplate) {
this.redisTemplate = redisTemplate;
this.hashOperations = redisTemplate.opsForHash();
this.listOperations = redisTemplate.opsForList();
this.zSetOperations = redisTemplate.opsForZSet();
this.setOperations = redisTemplate.opsForSet();
this.valueOperations = redisTemplate.opsForValue();
} public void hashPut(String key, HK hashKey, V value) {
hashOperations.put(key, hashKey, value);
} public Map<HK, V> hashFindAll(String key) {
return hashOperations.entries(key);
} public V hashGet(String key, HK hashKey) {
return hashOperations.get(key, hashKey);
} public void hashRemove(String key, HK hashKey) {
hashOperations.delete(key, hashKey);
} public Long listPush(String key, V value) {
return listOperations.rightPush(key, value);
} public Long listUnshift(String key, V value) {
return listOperations.leftPush(key, value);
} public List<V> listFindAll(String key) {
if (!redisTemplate.hasKey(key)) {
return null;
}
return listOperations.range(key, 0, listOperations.size(key));
} public V listLPop(String key) {
return listOperations.leftPop(key);
} public void setValue(String key, V value) {
valueOperations.set(key, value);
} public void setValue(String key, V value, long timeout) {
ValueOperations<String, V> vo = redisTemplate.opsForValue();
vo.set(key, value, timeout, TimeUnit.MILLISECONDS);
} public V getValue(String key) {
return valueOperations.get(key);
} public void remove(String key) {
redisTemplate.delete(key);
} public boolean expire(String key, long timeout, TimeUnit timeUnit) {
return redisTemplate.expire(key, timeout, timeUnit);
}

5. test

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
@Autowired
RedisService redisService; @Test
public void findAllUsers() {
redisService.setValue("key","hello");
} @Test
public void findAllUsers2() {
System.out.println("get key value:"+ redisService.getValue("key"));
}
}

说明

    1. 以上是具体的配置,如果没有引入redis.clients.jedis 依赖,则会报No beans of 'RedisConnectionFactory' type found.
      版本则2.9.0 如果 版本低,则会报其它错误
    2. spring-data-redis 这个版本分为 1.x 和 2.x 版本变动挺多,不同版本方法也不一样

29. SpringBoot Redis 非注解的更多相关文章

  1. SpringBoot + redis + @Cacheable注解实现缓存清除缓存

    一.Application启动类添加注解 @EnableCaching 二.注入配置 @Bean public CacheManager cacheManager(RedisTemplate redi ...

  2. springboot + redis + 注解 + 拦截器 实现接口幂等性校验

    一.概念 幂等性, 通俗的说就是一个接口, 多次发起同一个请求, 必须保证操作只能执行一次 比如: 订单接口, 不能多次创建订单 支付接口, 重复支付同一笔订单只能扣一次钱 支付宝回调接口, 可能会多 ...

  3. Springboot + redis + 注解 + 拦截器来实现接口幂等性校验

    Springboot + redis + 注解 + 拦截器来实现接口幂等性校验   1. SpringBoot 整合篇 2. 手写一套迷你版HTTP服务器 3. 记住:永远不要在MySQL中使用UTF ...

  4. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  5. Springboot使用Cookie,生成cookie,获取cookie信息(注解与非注解方式)

    先 创建一个控制类吧, 其实我没有分层啊,随便做个例子: MyGetCookieController: @RestControllerpublic class MyGetCookieControlle ...

  6. Spring Boot 2.x 缓存应用 Redis注解与非注解方式入门教程

    Redis 在 Spring Boot 2.x 中相比 1.5.x 版本,有一些改变.redis 默认链接池,1.5.x 使用了 jedis,而2.x 使用了 lettuce Redis 接入 Spr ...

  7. springboot基础、注解等

    SpringBoot 1.springboot概念 Spring Boot是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. ...

  8. 补习系列(14)-springboot redis 整合-数据读写

    目录 一.简介 二.SpringBoot Redis 读写 A. 引入 spring-data-redis B. 序列化 C. 读写样例 三.方法级缓存 四.连接池 小结 一.简介 在 补习系列(A3 ...

  9. springboot +redis配置

    springboot +redis配置 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...

随机推荐

  1. 【bfs】仙岛求药

    [题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...

  2. 「SCOI2014」方伯伯的玉米田 解题报告

    #2211. 「SCOI2014」方伯伯的玉米田 发现是取一个最长不下降子序列 我们一定可以把一个区间加的右端点放在取出的子序列的最右边,然后就可以dp了 \(dp_{i,j}\)代表前\(i\)个玉 ...

  3. Jarvis OJ [XMAN]level1 write up

    首先 老规矩,把软件拖到Ubuntu里checksec一下文件 然后知道了软件位数就放到IDA32里面... 熟悉的函数名... 缘真的妙不可言... 然后看了下vulnerable_function ...

  4. mysql 替换字符中部分字符,替换使用指定字符

    update table_name set name= replace(name,'我是','是');

  5. VMware配置centos虚拟机静态ip

    1. 安装centos,这个自己安装就好了 2. 配置配置虚拟机静态ip桥接器 配置ip地址 2. 配置网络共享中心 这里面的默认网关填写之前我们配置的网络网关ip默认为192.168.6.2 3. ...

  6. jquery 追加元素/jquery文档处理,插入、修改、移动、删除指定的DOM元素.

    jquery 追加元素 $("#content").append("..."); // 添加到元素内部最后面 $("#content").p ...

  7. 洛谷P1224 向量内积

    什么毒瘤...... 题意:给定n个d维向量,定义向量a和b的内积为 求是否存在两个向量使得它们的内积为k的倍数,并给出任一种方案.k <= 3. 解:很容易想到一个暴力是n2d的.显然我们不能 ...

  8. 将本地html文件拖到IE8浏览器无法打开,直接弹出一个下载的对话框

    查看一下注册表[HKEY_CLASSES_ROOT\.htm]和[HKEY_CLASSES_ROOT\.html]的ContentType值是否都为“text/html”

  9. easyUI 两个grid表格数据左移右移代码

    做项目中经常遇到选择已有数据,移动到选择好数据grid的场景,比如为项目添加员工,左侧grid是待选择员工,选好后移动到右侧grid,这里我用的jquery-easyui-1.4.2,整理出一份gri ...

  10. Laravel 下生成验证码的类

    <?php namespace App\Tool\Validate; //验证码类 class ValidateCode { private $charset = 'abcdefghkmnprs ...