springboot配置redis需增加redis依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
对应yml配置信息
spring:
redis:
database: 6
host: XX.XX.XX.XX
port: 6379
password:
timeout: 5000```
然后对应写一个redis配置类RedisConfig
import org.springframework.beans.factory.annotation.Autowired;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
创建Redis读取工具RedisUtil
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
public class RedisUtil {
@Resource
private RedisTemplate<String, String> redisTemplate;
public boolean setMapValueAndExpire(String key, Map<String, String> map, Long expireTime) {
if (StringUtils.isEmpty(key) || map == null || map.isEmpty()) {
return false;
}
try {
redisTemplate.opsForHash().putAll(key, map);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean setMapValueAndExpireDate(String key, Map<String, String> map, Date expireTime) {
if (StringUtils.isEmpty(key) || map == null || map.isEmpty()) {
return false;
}
try {
redisTemplate.opsForHash().putAll(key, map);
redisTemplate.expireAt(key, expireTime);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean deleteByKey(String key) {
if (StringUtils.isEmpty(key)) {
return false;
}
try {
redisTemplate.delete(key);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public Map<Object, Object> getMapValueByKey(String key) {
if (StringUtils.isEmpty(key)) {
return null;
}
try {
return redisTemplate.opsForHash().entries(key);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String getStringFromMapBykey(String key, String mapKey) {
if (StringUtils.isEmpty(key)) {
return null;
}
try {
Object obj = redisTemplate.opsForHash().get(key, mapKey);
if (obj != null) {
return (String) obj;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
随后启动会报错误
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1213)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
... 22 common frames omitted
查询到还需要配置redis客户端框架,现已知的有3个:Jedis,Redisson,Lettuce,本次使用的Lettuce
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
三者官网:
Jedis api 在线网址:http://tool.oschina.net/uploads/apidocs/redis/clients/jedis/Jedis.html
redisson 官网地址:https://redisson.org/
redisson git项目地址:https://github.com/redisson/redisson
lettuce 官网地址:https://lettuce.io/
lettuce git项目地址:https://github.com/lettuce-io/lettuce-core