前言
springboot整合系列之Springboot整合redis教程,这是继 Springboot整合RabbitMQ
后的第二篇springboot整合案例。
###简单的springboot 整合 redis ,有以下几个方法(基本可以满足小型后台应用): ##### 1.保存字符串 ##### 2.根据key获取字符串 ##### 3.保存对象 ##### 4.根据key获取对象 ##### 5.从redis中删除 ##### 6.从redis中批量删除
1.新建一个springboot过程
此处不做多说。。。
注:RedisCacheObject 和 RedisObjectSerializer 在本次讲解中用不到,此处也不做说明。
2.加入Redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3.配置application.yml
spring:
devtools:
restart:
enabled: false
redis:
database: 1
host: 127.0.0.1
port: 6379
timeout: 6000 # 连接超时时长(毫秒)
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
password:
server:
port: 90
4.配置RedisConfig
package com.example.redis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
/**
* @author zhuzhe
* @date 2018/5/20 16:12
* @email 1529949535@qq.com
*/
@Configuration
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}
}
5.RedisUtils
package com.example.redis.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Collection;
/**
* @author zhuzhe
* @date 2018/5/20 16:22
* @email 1529949535@qq.com
*/
@Component
public class RedisUtils {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final ObjectMapper mapper = new ObjectMapper();
/**
* 保存字符串
* @param key
* @param value
*/
public void saveString(String key,String value){
stringRedisTemplate.opsForValue().set(key, value);
}
/**
* 根据key获取字符串
* @param key
*/
public String getString(String key){
return stringRedisTemplate.opsForValue().get(key);
}
/**
* 保存对象
* @param key
* @param obj
* @throws JsonProcessingException
*/
public void saveBean(String key,Object obj) throws JsonProcessingException {
saveString(key,mapper.writeValueAsString(obj));
}
/**
* 根据key获取对象
* @param key
* @return RedisCacheObject
*/
public <T>T getBean(String key,Class<T> clazz) throws IOException {
String value = getString(key);
if (value == null) {
return null;
}
return mapper.readValue(value, clazz);
}
/**
* 从redis中删除
* @param key
*/
public void delete(String key){
stringRedisTemplate.delete(key);
}
/**
* 从redis中批量删除
* @param keys
*/
public void delete(Collection<String> keys){
stringRedisTemplate.delete(keys);
}
}
6.测试
package com.example.redis.domain;
import lombok.Data;
import java.io.Serializable;
/**
* @author zhuzhe
* @date 2018/5/20 17:13
* @email 1529949535@qq.com
*/
@Data
public class User implements Serializable {
private static final long serialVersionUID = -1L;
private String username;
private Integer age;
public User(String username, Integer age) {
this.username = username;
this.age = age;
}
public User() {}
}
package com.example.redis.test;
import com.example.redis.common.RedisCacheObject;
import com.example.redis.domain.User;
import com.example.redis.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
/**
* @author zhuzhe
* @date 2018/5/20 18:16
* @email 1529949535@qq.com
*/
@RestController
public class ApplicationTests {
@Autowired
private RedisUtils redisUtils;
@RequestMapping("/test")
public String test() throws IOException {
User user = new User("zzz",12);
redisUtils.saveString("zhuzhe","zhuzhe123");
redisUtils.saveBean("aaa",user);
String zhuzhe = redisUtils.getString("zhuzhe");
User aaa = redisUtils.getBean("aaa", User.class);
System.out.println(aaa);
return zhuzhe;
}
}
git 地址: https://github.com/zhuzhegithub/springboot-redis
转载请务必保留此出处(原作者):https://blog.csdn.net/zhuzhezhuzhe1
版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。
https://blog.csdn.net/zhuzhezhuzhe1