springboot整合redis
一、依赖
org.springframework.boot
spring-boot-starter-data-redis
redis.clients
jedis
3.2.0
com.alibaba
fastjson
1.2.70
二、配置
server:
port: 9090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot-vue?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8
username: root
password: password
oracleucp:
initial-pool-size: 5
redis:
# redis数据库索引(默认为0),我们使用索引为3的数据库,避免和其他数据库冲突
database: 3
# redis服务器地址(默认为loaclhost)
host: 192.168.43.55
# redis端口(默认为6379)
port: 6379
# redis访问密码(默认为空)
password: 123456
# redis连接超时时间(单位毫秒)
timeout: 0
# redis连接池配置
pool:
# 最大可用连接数(默认为8,负数表示无限)
max-active: 8
# 最大空闲连接数(默认为8,负数表示无限)
max-idle: 8
# 最小空闲连接数(默认为0,该值只有为正数才有用)
min-idle: 0
# 从连接池中获取连接最大等待时间(默认为-1,单位为毫秒,负数表示无限)
max-wait: -1
三、配置类
package com.redis.test.config;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate
四、测试(重点)
package com.redis.test.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisTestController {
//测试后,注销掉配置类也能运行
//注意,此时为**String**RedisTemplate
@Autowired
private StringRedisTemplate redisTemplate;
//@Resource
// private RedisTemplate redisTemplate;
@GetMapping("/redisTest")
public String redisTest(){
redisTemplate.opsForValue().set("name","J.J");
String name = (String) redisTemplate.opsForValue().get("name");
return name;
}
}
- 页面成功显示
blog.csdnimg.cn/6b77c6cba04545f1bcdddb86aa43225d.png#pic_center)