spring boot 整合redis教程

本文介绍了如何在Windows上使用cmd命令行安装和启动Redis服务器,配置Maven项目引入SpringBoot并集成Redis,包括POM文件、启动类、配置文件和控制器的实现,展示了如何进行基本的Redis缓存操作。
摘要由CSDN通过智能技术生成

1、cmd进入redis安装目录先输入

 redis-server.exe redis.windows.conf

启动redis服务端

2、再打开一个命令窗口进入到redis安装目录

输入

redis-cli.exe -h 127.0.0.1 -p 6379

启动redis客户端

3、搭建maven项目项目结构如图所示

4、pom文件代码如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>redis</groupId>
  <artifactId>redis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>redis</name>
  <description>redis</description>
<dependencies>
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.6.RELEASE</version>
            <scope>compile</scope>
        </dependency>
 
    <!-- spring2.X集成redis所需common-pool2-->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.6.0</version>
    </dependency>
</dependencies>
</project>

 5、创建启动类SpringboorRedisApplication

package com.test.redis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class SpringboorRedisApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringboorRedisApplication.class,args);
    }


}

6、配置文件application.yml

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: 123456
    timeout: 10000
    database: 0
    jedis:
      pool:
        max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
        max-wait: -1  # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 8  # 连接池中的最大空闲连接
        min-idle: 1  # 连接池中的最小空闲连接

7、创建RedisConfig

package com.test.redis.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<String,Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        //暂时先设置key和value的序列化都是字符串
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }


}

8、创建RedisController

package com.test.redis.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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/redis")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping(value = "/put")
    public String put(@RequestParam String key,@RequestParam String value) {
        redisTemplate.opsForValue().set(key,value);
        return "SUCCESS";
    }
//redis的数据 key相同 value会覆盖 (也就是说键是唯一的,才能保证数据的完整性)
    @GetMapping(value = "/get")
    public String get(@RequestParam String key) {
        String s = (String) redisTemplate.opsForValue().get(key);
        return s;
    }

}

10 、启动项目

打开浏览器地址栏输入给test赋值zh缓存成功

http://localhost:8080/redis/put?key=test&value=zh

11、地址栏输入test获取缓存成功

http://localhost:8080/redis/get?key=test

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值