SpringBoot 2.6教程:3.SpringBoot + Redis 项目入门搭建-2022年最新图文版本

 SpringBoot 2.6 系列2022年最新图文版本:1.SpringBoot Web项目入门搭建

SpringBoot 2.6 系列2022年最新图文版本:2.SpringBoot + Mysql 项目搭建

SpringBoot 2.6 系列2022年最新图文版本:3.SpringBoot + Redis 项目搭建

SpringBoot 2.6 系列2022年最新图文版本:4.SpringBoot + validation参数校验

本章主要目标:

1.学习SpringBoot Mysql 项目搭建

2.学习使用Redis

当前最常用的版本2.2.x、2.3.x将在2022年停止维护了。

我们以最新的版本2.6.6进行系统搭建。

下面我们开始进行搭建系统。

一、右键新建Module

二、修改模板下载地址为阿里云

三、配置Group、Artifact

四、搜索添加依赖

五、确定项目存放目录

六、修改pom.xml文件

6.1 修改springboot版本为2.6.6

<properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.6</spring-boot.version>
</properties>

6.2 确定web、redis依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

七、书写代码

7.1 书写Controller   

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/***
 * @date: 2022/5/10 
 * @author: fenghaikuan
 * @description: TODO
 */
@RestController
public class RedisController {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @PostMapping("redis/set")
    public String setValue(){
        String key = "username";
        String value = "zhangsan";
        stringRedisTemplate.opsForValue().set(key,value);
        return "set succ";
    }
    @GetMapping("redis/get")
    public String getValue(){
        String key = "username";
        String result = stringRedisTemplate.opsForValue().get(key);
        return result;
    }
}

7.2 配置redis连接信息,application.properties

spring.redis.host=host地址
spring.redis.port=端口号
spring.redis.password=密码

八、启动Application测试使用Postman进行测试

POST  http://localhost:8080/redis/set

GET  http://localhost:8080/redis/get

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值