在Spring Boot中整合Redis,可以使用Spring Data Redis。以下是一个简单的示例,展示了如何配置和使用Redis。
1. 添加依赖在pom.xml文件中添加Spring Data Redis和Lettuce(Redis客户端)的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce.core</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
</dependencies> 2. 配置Redis连接在src/main/resources/application.properties文件中添加Redis的连接配置:
spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=yourpassword # 如果没有密码,可以省略此行3. 创建Redis配置类(可选)
如果需要更复杂的配置,可以创建一个配置类:
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory();
}
@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;
}
} 4. 使用RedisTemplate进行操作创建一个简单的服务类来演示如何使用RedisTemplate进行Redis操作:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class RedisService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void save(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public Object find(String key) {
return redisTemplate.opsForValue().get(key);
}
public void delete(String key) {
redisTemplate.delete(key);
}
} 5. 创建控制器创建一个控制器来演示如何调用RedisService:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisService redisService;
@PostMapping("/save")
public String save(@RequestParam String key, @RequestParam String value) {
redisService.save(key, value);
return "Saved";
}
@GetMapping("/find")
public Object find(@RequestParam String key) {
return redisService.find(key);
}
@DeleteMapping("/delete")
public String delete(@RequestParam String key) {
redisService.delete(key);
return "Deleted";
}
} 6. 启动应用创建应用程序的主类,确保它位于src/main/java/com/example/demo目录下:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
} 7. 运行应用运行Spring Boot应用程序,然后可以使用例如Postman或浏览器测试Redis的相关操作:
保存数据:POST http://localhost:8080/redis/save?key=mykey&value=myvalue查找数据:GET http://localhost:8080/redis/find?key=mykey删除数据:DELETE http://localhost:8080/redis/delete?key=mykey通过以上步骤,你可以在Spring Boot中成功整合Redis并进行基本的CRUD操作。
网友回复
如何用html写出网页滚动视频播放卡片视觉差异效果的代码?
程序员如何低成本搭建代理进行科学上网学习技术?
threejs如何做个三维搭积木的游戏?
three如何实现标记多个起始路过地点位置后选择旅行工具(飞机汽车高铁等),最后三维模拟行驶动画导出mp4?
ai实时驱动的3d数字人可视频聊天的开源技术有吗
swoole+phpfpm如何实现不同域名指向不同目录的多租户模式?
如何用go替换nginx实现请求phpfpm解析运行php脚本?
有没有浏览器离线运行进行各种文档、图片、视频格式转换的开源工具?
如何使用go语言搭建一个web防火墙?
linux如何检测特定网络协议比如http协议中报文是否包含特点关键词并阻止返回给客户?


