package com.ttstd.videotablet.controller; import com.ttstd.videotablet.result.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { //引入 redis @Autowired private StringRedisTemplate stringRedisTemplate; @GetMapping("/public/hello") public Result getMethodName() { return Result.ok().message("Welcome to Yijiantong"); } /** * 存储 * * @return */ @PostMapping("/public/set") public Result setRedis(@RequestParam(value = "username") String username) { //存储 key-value 键值对: "username"-"jaychou" stringRedisTemplate.opsForValue().set("username", username); return Result.ok().message("redis 存储成功!"); } /** * 读取 * * @return */ @GetMapping("/public/get") public Result getRedis(@RequestParam(value = "username") String username) { //通过 key 值读取 value String result = stringRedisTemplate.opsForValue().get(username); return Result.ok().data("username", result); } }