Spring Boot2.0在2018年3月份正式发布,相比1.0仍是有比较多的改动,例如SpringBoot 自2.0起支持jdk1.8及以上的版本、第三方类库升级、响应式 Spring 编程支持等;整合Redis也有所变化,下面详细介绍下基于Spring Boot2.1.2.RELEASE版本整合redis的过程。html
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
redis:
host: 192.168.0.1
password: 1111
port: 6379
database: 0
timeout: 60s # 数据库链接超时时间,2.0 中该参数的类型为Duration,这里在配置的时候须要指明单位
# 链接池配置,2.0中直接使用jedis或者lettuce配置链接池
jedis:
pool:
# 最大空闲链接数
max-idle: 500
# 最小空闲链接数
min-idle: 50
# 等待可用链接的最大时间,负数为不限制
max-wait: -1s
# 最大活跃链接数,负数为不限制
max-active: -1
操做redis能够使用下面两个templateweb
代码以下:redis
package com.example.helloSpringBoot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisController {
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("/set")
public String HelloSpring (String key,String value){
redisTemplate.opsForValue().set(key,value);
return String.format("redis set成功!key=%2,value=%s",key,value);
}
@RequestMapping("/get")
public String HelloSpring (String key){
String value = (String) redisTemplate.opsForValue().get(key);
return "redis get结果 value=" + value;
}
}
截图以下:spring
截图以下:数据库
限时领取免费Java相关资料,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo/Kafka、Hadoop、Hbase、Flink等高并发分布式、大数据、机器学习等技术。编程
资料传送门:https://mp.weixin.qq.com/s/u2b_NVNuMuAPE0w4lc45fw并发
关注下方公众号便可免费领取:app
原文出处:https://www.cnblogs.com/haha12/p/10509944.html机器学习