SpringBoot 2.7教程:SpringBoot 整合 Redis 项目搭建-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot + Web 项目搭建及实践应用-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot + Web 项目搭建,异常捕获处理-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot + Mysql 项目应用-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 集成 jsp 页面开发 -2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 实现文件上传,图片上传并显示功能-2022年最新图文版本

SpringBoot 2.7教程:springboot 设置全局字符编码,解决乱码问题-2022年最新图文版

SpringBoot 2.7教程:SpringBoot mybatis 多数据源的整合方法-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 整合 RocketMQ 项目搭建-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 整合 RabbitMQ 项目搭建-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 整合 MongoDB 项目搭建-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 整合 Redisson 项目搭建-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 整合 Redis 项目搭建-2022年最新图文版本

本章主要目标:

1.学习SpringBoot Mysql 项目搭建

2.学习使用Redis

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

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

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

一、新建项目springboot-redis,项目结构如下

二、修改pom.xml文件

1 修改springboot版本为2.7.0

<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.7.0</spring-boot.version>
</properties>

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>

三、书写代码

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;
    @GetMapping("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;
    }
}

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

spring.redis.host=host地址
spring.redis.port=端口号
spring.redis.password=密码
#连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.pool.max-idle=8
#连接池中的最小空闲连接
spring.redis.pool.min-idle=0
#连接超时时间(毫秒)
spring.redis.timeout=30000

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

GET  http://localhost:8080/redis/set

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

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot 2.7中,可以通过以下步骤来整合Redis: 1. 添加Redis依赖:在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置Redis连接信息:在application.properties或application.yml文件中配置Redis的连接信息,例如: ```yaml spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= ``` 3. 创建Redis配置类:创建一个Redis配置类,用于配置Redis连接工厂和Redis模板。可以使用以下示例代码: ```java @Configuration public class RedisConfig { @Bean public RedisConnectionFactory redisConnectionFactory() { LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(); connectionFactory.setHostName("127.0.0.1"); connectionFactory.setPort(6379); return connectionFactory; } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } } ``` 4. 使用Redis:在需要使用Redis的地方,可以通过注入RedisTemplate来操作Redis。例如,以下代码演示了如何向Redis中存储和获取数据: ```java @Autowired private RedisTemplate<String, Object> redisTemplate; public void setValue(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object getValue(String key) { return redisTemplate.opsForValue().get(key); } ``` 以上是SpringBoot 2.7整合Redis的基本步骤。你可以根据自己的需求进行进一步的配置和使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值