1. 添加依赖
在pom.xml
中添加必要的依赖:
<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>
2. 配置Redis
在application.properties
或application.yml
中配置Redis:
spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=yourpassword
3. 创建Redis配置类
创建一个Redis配置类,用于配置RedisTemplate
:
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.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } }
4. 创建服务类
创建一个服务类,用于与Redis进行交互:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class UserService { private final RedisTemplate<String, Object> redisTemplate; @Autowired public UserService(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } public void saveUser(String userId, User user) { //保存到数据库中 userDao.save(user); } public User getUser(String userId) { User user = (User) redisTemplate.opsForValue().get("USER_" + userId); if(user != null) return user; User user = userDao.getById(userId); redisTemplate.opsForValue().set("USER_" + userId, user); // 设置过期时间(可选) redisTemplate.expire("USER_" + userId, 10, TimeUnit.MINUTES); return user; } public void deleteUser(String userId) { redisTemplate.delete("USER_" + userId); } }
5. 创建用户实体类
创建一个简单的用户实体类:
import java.io.Serializable; public class User implements Serializable { private Long id; private String name; private int age; // Getters and Setters public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
6. 使用服务类
在你的控制器或其他服务类中使用UserService
:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/users") public class UserController { private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @PostMapping public void createUser(@RequestBody User user) { userService.saveUser(user.getId().toString(), user); } @GetMapping("/{id}") public User getUser(@PathVariable String id) { return userService.getUser(id); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable String id) { userService.deleteUser(id); } }
总结
通过上述步骤,你可以在Spring Boot项目中集成Redis进行数据查询和存储,而不依赖缓存机制。这样可以利用Redis的高性能特性,提高数据存取速度,特别适合需要快速读取和写入的数据场景。