微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringBoot整合Redis

SpringBoot整合Redis

pom引入redis依赖

<!-- 引入 redis 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

yml添加配置

spring:
  redis:
    database: 0 # 第几个库
    host: ip
    port: 6379
    password: 密码

写个Controller测试

package com.jinsh.controller;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("redis")
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private Redistemplate redistemplate;

    @GetMapping("/get")
    public String get(String key){
        String result = (String) redistemplate.opsForValue().get(key);
        return result;
    }

    @GetMapping("/set")
    public String set(String key, String value){
        redistemplate.opsForValue().set(key, value);
        return "OK";
    }

    @GetMapping("/del")
    public String del(String key){
        redistemplate.delete(key);
        return "OK";
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐