springboot-整合redis

需要的依赖

在这里插入图片描述

application.properties的配置

spring.redis.host=127.0.0.1
spring.redis.database=0
spring.redis.port=6379
spring.redis.password=handhand

Controller

package org.akk.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @GetMapping("/set")
    public void set(){
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        opsForValue.set("name","akk");
    }

    @GetMapping("/get")
    public void get(){
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        System.out.println(opsForValue.get("name"));
    }
}

运行后显示密码

在这里插入图片描述

登录

http://localhost:8080/set 自动跳转登陆界面
默认用户名user,使用以上密码
在这里插入图片描述
运行结果
在这里插入图片描述
使用http://localhost:8080/get
将会打印出信息
在这里插入图片描述

发布了7 篇原创文章 · 获赞 0 · 访问量 79

猜你喜欢

转载自blog.csdn.net/weixin_39232166/article/details/104301357