美文网首页
Spring Data Redis 之 Redis Reposi

Spring Data Redis 之 Redis Reposi

作者: louieSun | 来源:发表于2020-02-02 22:20 被阅读0次

    Working with Redis Repositories lets you seamlessly convert and store domain objects in Redis Hashes, apply custom mapping strategies, and use secondary indexes.

    使用 Redis Repository 库可以很方便的以 Redis Hash 的方式存储对象,以类似 Spring Data JPA 的方式进行数据的存取。

    添加 redis 依赖(Gradle为例):

    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    

    application.yml 配置 redis服务:

    spring:
      redis:
        host: 127.0.0.1
        database: 0
        password:
        port: 6379
    

    创建 User 实体类:

    @RedisHash(value = "user")  // redis hash name
    public class User {
        @Id // 指定id属性
        private String id;
        private String name;
        private String mobile;
        private String ip;
        private long loginTime;
    }
    

    UserRepository 类,继承CrudRepository接口即可:

    @Repository
    public interface UserRepository extends CrudRepository<User, String> {
    }
    

    User Service 实现类:

    @Service
    public class UserServiceImpl implements UserService {
        private final UserRepository userRepository;
    
        public UserServiceImpl (UserRepository userRepository) {
            this.userRepository = userRepository;
        }
    
        @Override
        public User save(User user) {
            return userRepository.save(user);
        }
    
        @Override
        public User findUser(String userId) {
            return userRepository.findById(userId).orElse(null);
        }
    
        @Override
        public void delete(String userId) {
            userRepository.deleteById(userId);
        }
    
        @Override
        public void update(User user) {
            userRepository.save(user);
        }
    }
    

    User Controller 类:

    @RestController
    @RequestMapping(value = "/user")
    public class UserController {
        private final UserService userService;
    
        public UserController (UserService userService) {
            this.userService = userService;
        }
    
        @PostMapping
        public ResponseEntity<String> createUser (@RequestBody User user) {
            userService.save(user);
            return new ResponseEntity<>("user created ...", HttpStatus.OK);
        }
    
        @GetMapping(value = "/{userId}")
        public User getUser (@PathVariable String userId) {
            return userService.findUser(userId);
        }
    
        @PutMapping
        public ResponseEntity<String> updateUser (@RequestBody User user) {
            userService.update(user);
            return new ResponseEntity<>("user updated ...", HttpStatus.OK);
        }
    
        @DeleteMapping(value = "/{userId}")
        public ResponseEntity<String> deleteUser (@PathVariable String userId) {
            userService.delete(userId);
            return new ResponseEntity<>("user deleted ...", HttpStatus.OK);
        }
    }
    

    以上,Redis Repository 的使用介绍完毕。

    接下来,对接口做下测试,下面是测试用例:

    user-post.http:

    POST http://127.0.0.1:8080/user
    Content-Type: application/json
    
    {
      "id": "deea85ee-9b4a-43e2-b4a7-696252f08969",
      "name": "TestUser",
      "mobile": "13312341234",
      "ip": "127.0.0.1",
      "loginTime": 1580564162945
    }
    
    ###
    

    user-get.http:

    GET http://127.0.0.1:8080/user/deea85ee-9b4a-43e2-b4a7-696252f08969
    
    ###
    

    user-put.http:

    PUT http://127.0.0.1:8080/user
    Content-Type: application/json
    
    {
      "id": "deea85ee-9b4a-43e2-b4a7-696252f08969",
      "name": "TestUser",
      "mobile": "13312341234",
      "ip": "127.0.0.1",
      "loginTime": 11580646475542
    }
    
    ###
    

    user-delete.http:

    DELETE http://127.0.0.1:8080/user/deea85ee-9b4a-43e2-b4a7-696252f08969
    
    ###
    

    Demo 工程git 仓库:

    https://gitee.com/louie-sun/redis-repository.git

    相关文章

      网友评论

          本文标题:Spring Data Redis 之 Redis Reposi

          本文链接:https://www.haomeiwen.com/subject/ixrcxhtx.html