Springboot配置redis
Posted lchzlp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot配置redis相关的知识,希望对你有一定的参考价值。
- 配置pom.xml文件,引入如下依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
- 配置application文件
2.1 如果是application.yml文件,则配置如下
server:
port: 8080
spring:
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: lch123
driver-class-name: com.mysql.jdbc.Driver
redis:
host: 127.0.0.1
port: 6379
database: 1
timeout: 20000
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.ocean.pojo
2.2 如果是application.properties文件,则配置如下
# redis相关配置
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
#项目服务端口号配置
server.port=8810
3.添加配置类,配置类代码如下
package com.ocean.redis;
import java.lang.reflect.Method;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class RedisConfig 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();
}
};
}
}
4.写一个方法进行测试
/**
* <p>Title: TestRedis.java</p>
* <p>Description: TODO</p>
* <p>Copyright: Copyright (c) 2018</p>
* @author lch
* @date 2019年4月6日
*/
package com.ocean.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>Title: TestRedis.java</p>
* <p>Description: TODO</p>
* @author lch
* @time 2019年4月6日 下午5:08:20
* @version 1.0
*/
@RestController
@RequestMapping("/redis")
public class TestRedis {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping(value = {
"/test"
}, produces = {
"application/json;charset=UTF-8"
}, method = RequestMethod.GET)
public boolean getAllUsers() {
stringRedisTemplate.opsForValue().set("aaa", "111");
System.out.println("11");
return true;
}
}
5.调接口测试
localhost:8080/redis/test
6.结果
we1. 配置pom.xml文件,引入依赖
!--[endif]-->!--[if>
以上是关于Springboot配置redis的主要内容,如果未能解决你的问题,请参考以下文章
全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段
Springboot 手动搭建项目 --redis配置&日志完善+用户名