架构师成长记_第六周_01_SpringBoot 整合 Redis

文章目录

  • SpringBoot 整合 Redis
    • 1. 在pom 中添加 Redis 依赖
    • 2. 在 yml 文件进行配置 Redis
    • 3. 编写 Redis Controller (操作String类型)
    • 浏览器测试
      • 1. 测试set方法
      • 2. 测试gat方法
      • 3. 测试delete方法

SpringBoot 整合 Redis

1. 在pom 中添加 Redis 依赖

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

架构师成长记_第六周_01_SpringBoot 整合 Redis_第1张图片

2. 在 yml 文件进行配置 Redis

spring:
  redis:
    database: 1
    host: 192.168.92.132
    port: 6379
    password: xxxxxxx

架构师成长记_第六周_01_SpringBoot 整合 Redis_第2张图片

3. 编写 Redis Controller (操作String类型)

package com.beyond.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.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 *  使用 Redis 操作 String 类型: opsForValue()
 */


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

    @Autowired
    private RedisTemplate redisTemplate;

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

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


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

浏览器测试

1. 测试set方法

架构师成长记_第六周_01_SpringBoot 整合 Redis_第3张图片
架构师成长记_第六周_01_SpringBoot 整合 Redis_第4张图片

2. 测试gat方法

架构师成长记_第六周_01_SpringBoot 整合 Redis_第5张图片

3. 测试delete方法

架构师成长记_第六周_01_SpringBoot 整合 Redis_第6张图片
架构师成长记_第六周_01_SpringBoot 整合 Redis_第7张图片
架构师成长记_第六周_01_SpringBoot 整合 Redis_第8张图片

你可能感兴趣的:(You,Are,the,Architect,redis,spring,boot)