SpringBoot集成Redis的方法及步骤

搭建环境,开启Redis服务

1.首先导入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2.在application.yml中进行配置,配置如下,密码根据需求无密码可省略

redis:
  host: 127.0.0.1
  port: 6379
  database: 0
  #password: 123456

3.redis序列化器(可选)

主要是对 redis键值对进行序列化:一般只序列化键

import org.springframework.cache.annotation.CachingConfigurerSupport;
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.StringRedisSerializer;
​
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
​
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
​
        //默认的Key序列化器为:JdkSerializationRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer()); // key序列化
        //redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化
​
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}

4.在Controller中注入RedisTemplate即可

  @Autowired
    private RedisTemplate redisTemplate;

接下来就可以通过redisTemplate进行操作了。

以下是Spring Boot集成Redis的详细步骤: 1. 添加Redis依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置Redis连接信息 在application.properties文件中添加以下配置: ``` spring.redis.host=127.0.0.1 # Redis服务器IP spring.redis.port=6379 # Redis服务器端口 spring.redis.database=0 # Redis数据库序号 spring.redis.password= # Redis服务器密码 ``` 3. 创建RedisTemplate对象 在Spring Boot中,可以通过RedisTemplate对象来操作Redis。在配置类中创建RedisTemplate对象,并设置连接工厂。 ``` @Configuration public class RedisConfig { @Autowired private RedisConnectionFactory redisConnectionFactory; @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } } ``` 这里使用了Jackson库来序列化对象,可以避免手动序列化和反序列化的麻烦。 4. 在业务类中使用RedisTemplate对象 在业务类中通过@Autowired注解注入RedisTemplate对象,就可以使用RedisTemplate来操作Redis了。 ``` @Service public class UserService { @Autowired private RedisTemplate<String, Object> redisTemplate; public void saveUser(User user) { redisTemplate.opsForValue().set(user.getId(), user); } public User getUser(Integer id) { return (User) redisTemplate.opsForValue().get(id); } } ``` 这里使用了RedisTemplate的opsForValue()方法来操作Redis中的字符串数据,也可以使用opsForHash()、opsForList()等方法来操作其他类型的数据。 以上就是Spring Boot集成Redis的详细步骤
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ethan_Zhuo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值