Spring Boot 整合 Redis 注解实现简单 CRUD
可以关注:小坏说Java 公众号
零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目
一、项目搭建
零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目
1.1 添加依赖
<!-- pom.xml --><dependencies><!-- Spring Boot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot Redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- Lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies>1.2 配置文件
# application.ymlspring:redis:host:localhostport:6379database:0server:port:8080二、实体类
零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目
packagecom.example.entity;importlombok.Data;importorg.springframework.data.annotation.Id;importorg.springframework.data.redis.core.RedisHash;importorg.springframework.data.redis.core.index.Indexed;importjava.io.Serializable;/** * @RedisHash: 声明实体类,value指定Redis中的key前缀 * 存储格式: user:{id} */@Data@RedisHash("user")publicclassUserimplementsSerializable{@IdprivateLongid;// 主键@IndexedprivateStringusername;// 创建索引,可以按username查询privateStringemail;privateIntegerage;}三、Repository 接口
packagecom.example.repository;importcom.example.entity.User;importorg.springframework.data.repository.CrudRepository;importorg.springframework.stereotype.Repository;importjava.util.List;importjava.util.Optional;/** * CrudRepository 提供基本的CRUD方法 * 可以根据方法名自动生成查询 */@RepositorypublicinterfaceUserRepositoryextendsCrudRepository<User,Long>{// 根据用户名查询(精确匹配)List<User>findByUsername(Stringusername);// 根据邮箱查询Optional<User>findByEmail(Stringemail);// 根据年龄范围查询List<User>findByAgeBetween(IntegerminAge,IntegermaxAge);// 删除指定用户名的用户LongdeleteByUsername(Stringusername);}四、Service 层
packagecom.example.service;importcom.example.entity.User;importcom.example.repository.UserRepository;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cache.annotation.*;importorg.springframework.stereotype.Service;importjava.util.List;importjava.util.Optional;@Slf4j@Service@CacheConfig(cacheNames="user")// 类级别缓存配置publicclassUserService{@AutowiredprivateUserRepositoryuserRepository;/** * @Cacheable: 查询缓存 * 1. 先查缓存,有则直接返回 * 2. 无则执行方法,将结果存入缓存 */@Cacheable(key="#id")publicUserfindById(Longid){log.info("查询数据库,用户ID: {}",id);returnuserRepository.findById(id).orElse(null);}/** * 查询所有用户 */publicList<User>findAll(){return(List<User>)userRepository.findAll();}/** * 根据用户名查询 */publicList<User>findByUsername(Stringusername){returnuserRepository.findByUsername(username);}/** * @CachePut: 更新缓存 * 每次都会执行方法,并将结果更新到缓存 */@CachePut(key="#user.id")publicUsersave(Useruser){log.info("保存用户: {}",user.getUsername());returnuserRepository.save(user);}/** * @CacheEvict: 删除缓存 * 方法执行后删除指定key的缓存 */@CacheEvict(key="#id")publicvoiddeleteById(Longid){log.info("删除用户,ID: {}",id);userRepository.deleteById(id);}/** * 更新用户 */@Caching(put=@CachePut(key="#user.id"),evict=@CacheEvict(key="'list'")// 清除列表缓存)publicUserupdate(Useruser){log.info("更新用户: {}",user.getId());returnuserRepository.save(user);}/** * 缓存用户列表 */@Cacheable(key="'list'")publicList<User>findAllWithCache(){log.info("查询所有用户(带缓存)");return(List<User>)userRepository.findAll();}}五、Controller 层
零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目
packagecom.example.controller;importcom.example.entity.User;importcom.example.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;importjava.util.List;@RestController@RequestMapping("/api/users")publicclassUserController{@AutowiredprivateUserServiceuserService;// 新增用户@PostMappingpublicUsercreateUser(@RequestBodyUseruser){returnuserService.save(user);}// 根据ID查询用户@GetMapping("/{id}")publicUsergetUserById(@PathVariableLongid){returnuserService.findById(id);}// 查询所有用户@GetMappingpublicList<User>getAllUsers(){returnuserService.findAllWithCache();}// 根据用户名查询@GetMapping("/search")publicList<User>getUserByUsername(@RequestParamStringusername){returnuserService.findByUsername(username);}// 更新用户@PutMapping("/{id}")publicUserupdateUser(@PathVariableLongid,@RequestBodyUseruser){user.setId(id);returnuserService.update(user);}// 删除用户@DeleteMapping("/{id}")publicStringdeleteUser(@PathVariableLongid){userService.deleteById(id);return"删除成功";}}六、主启动类
零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目
packagecom.example;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cache.annotation.EnableCaching;@SpringBootApplication@EnableCaching// 开启缓存支持publicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}}七、测试示例
7.1 使用 Postman 测试
1. 新增用户
POST http://localhost:8080/api/users Content-Type: application/json { "username": "张三", "email": "zhangsan@example.com", "age": 25 }2. 查询用户
GET http://localhost:8080/api/users/13. 查询所有用户
GET http://localhost:8080/api/users4. 更新用户
PUT http://localhost:8080/api/users/1 Content-Type: application/json { "username": "张三", "email": "zhangsan_new@example.com", "age": 26 }5. 删除用户
DELETE http://localhost:8080/api/users/1八、注解总结
| 注解 | 作用 | 示例 |
|---|---|---|
@RedisHash | 实体类映射到Redis Hash | @RedisHash("user") |
@Id | 标记主键字段 | @Id private Long id; |
@Indexed | 创建二级索引,支持字段查询 | @Indexed private String username; |
@EnableCaching | 启用缓存(主类上) | @EnableCaching |
@Cacheable | 方法结果缓存 | @Cacheable(key = "#id") |
@CachePut | 更新缓存 | @CachePut(key = "#user.id") |
@CacheEvict | 删除缓存 | @CacheEvict(key = "#id") |
@Caching | 组合多个缓存操作 | 见上面Service中的update方法 |
九、运行效果
零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目
- 第一次查询用户(1):控制台打印日志"查询数据库"
- 第二次查询用户(1):直接从缓存返回,不打印日志
- 更新用户(1):更新数据库,并更新缓存
- 删除用户(1):删除数据库记录,并清除缓存
十、注意事项
- 实体类必须实现
Serializable接口 - 缓存注解的方法必须是
public的 - 同一个类内部调用缓存注解的方法不会生效
- Redis 需要提前安装并启动
这个示例包含了最基本的 Redis 注解 CRUD 操作,可以直接运行使用。