SpringBoot整合Redis

时间:2021-08-30
本文章向大家介绍SpringBoot整合Redis,主要包括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";
    }
}

原文地址:https://www.cnblogs.com/jinshengnianhua/p/15207318.html