spring-boot-starter-data-redis使用简介
spring-boot
spring-projects/spring-boot: 是一个用于简化Spring应用开发的框架。适合用于需要快速开发企业级Java应用的项目。特点是可以提供自动配置、独立运行和内置的Tomcat服务器,简化Spring应用的构建和部署。
项目地址:https://gitcode.com/gh_mirrors/sp/spring-boot
·
Redis单机版配置
-
1、引入坐标
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>
-
2、修改配置文件
spring:
#redis 连接信息
redis:
host: 127.0.0.1
port: 6379
password: password
timeout: 1000
-
3、注入RedisTemplate
/**
* redis
*/
@Resource
private RedisTemplate redisTemplate;
Redis 哨兵模式配置
spring:
redis:
sentinel:
master: mymaster # 注意修改成主节点的名字
nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
################ Redis的Java驱动包,使用lettuce连接池 ################
lettuce:
pool:
max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接 (默认为8)
min-idle: 0 # 连接池中的最小空闲连接
<!--redis依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
RedisTemplate使用
1、基本操作
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
2、序列化方式
启动类上添加
/**
* 修改redis默认序列化
* @param redisConnectionFactory redis 连接
* @return redisTemplate
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
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);
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
spring-projects/spring-boot: 是一个用于简化Spring应用开发的框架。适合用于需要快速开发企业级Java应用的项目。特点是可以提供自动配置、独立运行和内置的Tomcat服务器,简化Spring应用的构建和部署。
最近提交(Master分支:4 个月前 )
5cc0e4b7
Closes gh-48956
1 天前
5f5217d5
Update `JavaConventions` and `EclipseConventions` to align the
Eclipse import with the regular Gradle Build.
Imported projects now use Java 25 for the JRE but have
compatibility set to Java 17 and a project configuration
that uses the `--release` flag.
This update also removes the calls to `setSourceCompatibility`
and `setTargetCompatibility` on the `JavaPluginExtension`
in favor of just using `compile.options.release`.
We now also enforce that Java 25 or above is being used to
build the project.
Since buildship doesn't offer a way to set the `--release`
JDT project setting, a custom task is used. Projects may need
to be reimported or cleaned to actually have the setting apply
correctly.
Closes gh-48929
1 天前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)