spring boot 集成redis 实现CRUD
环境 IDEA 2019 03 Spring boot 版本 2.3.5
话不多说,直接上代码,可以先按照步骤跑一遍,等成功后可以自己研究扩展用法
1:添加pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
2:添加redis加载类
package cn.theone.tmp.redis.util;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis启动类
* @author : 朱刚超
* @date : 2020/1/2
* @versions : 1.0
*/
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@EnableAutoConfiguration
public class RedisConfig {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// value值的序列化采用fastJsonRedisSerializer
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
// key的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
3:添加redis Hash工具类
package cn.theone.redisdemo.utils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.*;
/**
* Hash缓存工具类
* @author : 朱刚超
* @date : 2020/1/2
* @qq:648267940
* @versions : 1.0
*/
@Repository
public class RedisHashUtil {
@Resource
private RedisTemplate<String, Object> redisTemplate;
/**
* 存储单个值至map中
* @param redisKey redisKey中的key
* @param mapKey map所对应的key
* @param value map所对应的值
*/
public void addMapOne(String redisKey,String mapKey,Object value) {
redisTemplate.opsForHash().put(redisKey, mapKey,value);
}
/**
* 存储整个map至redis
* @param key redis中存储的key
* @param map 需缓存的Map
*/
public void addMapAll(String key, Map<String,Object> map) {
redisTemplate.opsForHash().putAll(key, map);
}
/**
* 获取整个HashMap
* @param redisKey redis中存储的key
* @return 整个Map
*/
public Map<String,Object> getMapAll(String redisKey) {
Map<Object, Object> entries = redisTemplate.opsForHash().entries(redisKey);
Map<String, Object> retEntries = new HashMap<>();
for(Map.Entry<Object , Object> temp:entries.entrySet()){
Object key = temp.getKey();
Object value = temp.getValue();
retEntries.put(String.valueOf(key) , value);
}
return retEntries;
}
/**
* 获取redis中hash的所有value
* @param redisKey
* @return
*/
public List<Object> getMapValues(String redisKey) {
return redisTemplate.opsForHash().values(redisKey);
}
/**
* 删除Map中的某个键值对
* @param redisKey
* @param mapKey
* @return 返回影响数量
*/
public Long deleteMapVal(String redisKey , Object ... mapKey) {
return redisTemplate.opsForHash().delete(redisKey , mapKey);
}
/**
* 删除指定大Key
* @param key
* @return
*/
public Boolean deleteKey(String key){
return redisTemplate.delete(key);
}
/**
* 确定hashkey是否存在
* @param redisKey redis存储的key
* @param mapKey 需要确定的map对象key
* @return
*/
public boolean hasKey(String redisKey , String mapKey) {
return redisTemplate.opsForHash().hasKey(redisKey , mapKey);
}
/**
* 获取Map中具体的值
* @param redisKey redis存储的key
* @param mapKey 获取的map对象key
* @return
*/
public Object getMapVal(String redisKey, String mapKey) {
return redisTemplate.opsForHash().get(redisKey,mapKey);
}
/**
* 从哈希中获取给定key的值
* @param redisKey redis存储的key
* @param mapKeys 需要去出的key的集合
* @return 值列表
*/
public List<Object> multiGetHash(String redisKey , List<Object> mapKeys) {
return redisTemplate.opsForHash().multiGet(redisKey , mapKeys);
}
/**
* 获取所有map中的key
* @param redisKey
* @return
*/
public Set<String> getHashKeys(String redisKey) {
Set<Object> keys = redisTemplate.opsForHash().keys(redisKey);
Set<String> retKeys = new HashSet<>();
for (Object key : keys) {
retKeys.add(String.valueOf(key));
}
return retKeys;
}
/**
* 获取所有map中的key的数量
* @param redisKey redis中的key
* @return key的数量
*/
public int getHashSize(String redisKey) {
Set<Object> keys = redisTemplate.opsForHash().keys(redisKey);
if(keys == null){
return 0;
}
return keys.size();
}
}
编辑application.yml文件
server:
port: 9999
spring:
redis:
password: hosredis # 没设置过密码不用管 空着就行
host: 127.0.0.1
port: 6379
编写测试用例:
package cn.theone.tmp;
import cn.theone.tmp.redis.util.RedisHashUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TmpApplicationTests {
@Autowired
private RedisHashUtil redisHashUtil;
@Test
public void contextLoads() {
// System.out.println(redisHashUtil.getMapVal("keys","name"));
// redisHashUtil.addMapOne("keys","name","朱刚超");
// redisHashUtil.deleteMapVal("keys","name");
}
}
可以看到已经插入成功了
redis工具类还有List Set Value 等等,有需要的可以评论,上面只是封装了几个常用得Hash得方法