Redisson和Spring Boot 结合的案例
    		       		warning:
    		            这篇文章距离上次修改已过423天,其中的内容可能已经有所变动。
    		        
        		                
                
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class RedisController {
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
 
    @GetMapping("/set")
    public String setKey() {
        redisTemplate.opsForValue().set("testKey", "testValue");
        return "Key set successfully";
    }
 
    @GetMapping("/get")
    public Object getKey() {
        return redisTemplate.opsForValue().get("testKey");
    }
}这段代码展示了如何在Spring Boot应用中使用RedisTemplate操作Redis数据库。setKey方法通过RedisTemplate的opsForValue().set方法设置一个键值对,getKey方法通过opsForValue().get方法获取键对应的值。这个例子简单地展示了如何在Spring Boot中集成Redis,并进行基本的读写操作。
评论已关闭