SpringBoot-项目引入Redis依赖
    		       		warning:
    		            这篇文章距离上次修改已过428天,其中的内容可能已经有所变动。
    		        
        		                
                在Spring Boot项目中引入Redis依赖,你需要在项目的pom.xml文件中添加Spring Data Redis和Jedis的依赖。以下是一个示例:
<dependencies>
    <!-- Spring Data Redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
 
    <!-- Jedis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
    </dependency>
 
    <!-- 其他依赖... -->
</dependencies>添加依赖后,你需要在application.properties或application.yml配置文件中配置Redis连接信息,例如:
# application.properties
spring.redis.host=localhost
spring.redis.port=6379或者使用YAML格式:
# application.yml
spring:
  redis:
    host: localhost
    port: 6379然后,你可以在你的Spring Boot应用中注入StringRedisTemplate或RedisTemplate来使用Redis服务。例如:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
 
@Component
public class RedisService {
 
    @Autowired
    private StringRedisTemplate redisTemplate;
 
    public void setKeyValue(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }
 
    public String getValueByKey(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}以上代码展示了如何在Spring Boot应用中简单地使用Redis。
评论已关闭