当前位置: 首页 > article >正文

SpringBoot+Redis极简整合

1 前言

Redis是现在最受欢迎的NoSQL数据库之一,下面将以最简洁的代码演示,在SpringBoot中使用redis。

2 下载安装Redis

2.1 下载

Redis3.x windows安装版下载地址

2.2 安装到任意位置

一直Next到完即可。
在这里插入图片描述

2.3 启动

打开安装目录,点击redis-server.exe即可启动。
在这里插入图片描述启动成功会弹出命令行,可以看到端口默认是6379。
在这里插入图片描述

3 代码

创建一个名为spring-boot-redis-demo的项目,使用Maven+JDK17。不会的请看 IntelliJ IDEA快速创建Spring Boot项目,最终项目结构,如下图。
在这里插入图片描述

3.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring-boot-redis-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!--  为Spring Boot项目提供一系列默认的配置和依赖管理-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.2</version>
        <relativePath/>
    </parent>

    <dependencies>
        <!--  Spring Boot核心依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- Spring Boot单元测试和集成测试的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot构建Web应用程序的依赖-->
        <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>

    </dependencies>

</project>

3.2 application.properties

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.username=
spring.redis.password=

3.3 org/example/Main.java

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Main extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

3.4 org/example/controller/UserController.java

这个是测试类。

package org.example.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    protected RedisTemplate<String, String> redisTemplate;


    @RequestMapping(value = "/set")
    public Object test() {
        String value = "张1";
        redisTemplate.opsForValue().set("key1", value);
        return "set:" + value;
    }


    @RequestMapping(value = "/get")
    public Object getSMSCode() {
        String key1 = redisTemplate.opsForValue().get("key1");
        return "get:" + key1;
    }


}

3.5 org/example/conf/RedisConf.java

这个是redis配置类。

package org.example.conf;

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 RedisConf {

	@Bean
	public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
		RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(factory);
		// redisTemplate 的默认序列化方式为 jdkSerializeable,
		// StringRedisTemplate的默认序列化方式为StringRedisSerializer
		StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
		// key序列化方式
		redisTemplate.setKeySerializer(stringRedisSerializer);
		// hash key序列化方式
		redisTemplate.setHashKeySerializer(stringRedisSerializer);
		// value序列化方式
		redisTemplate.setValueSerializer(stringRedisSerializer);
		// hash value序列化方式
		redisTemplate.setHashValueSerializer(stringRedisSerializer);
		// 支持事物
		redisTemplate.setEnableTransactionSupport(false);
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}



}

4 测试

  1. 启动项目,打开浏览器访问http://localhost:8080/user/set,set数据。

在这里插入图片描述
2. 启动项目,打开浏览器访问http://localhost:8080/user/get,get数据。
在这里插入图片描述


http://www.kler.cn/news/290656.html

相关文章:

  • springboot集成七牛云上传文件
  • Python画笔案例-030 实现打点之斜正方
  • MATLAB 中的对数计算
  • torch、torchvision、torchtext版本兼容问题
  • ubuntu 22.04安装NVIDIA驱动和CUDA
  • 传统CV算法——基于 SIFT 特征点检测与匹配实现全景图像拼接
  • Java实现根据某个字段对集合进行去重并手动选择被保留的对象
  • vuex 基础使用
  • 网页版修改本地数据器:重新布局,引入 highlight.js高亮显示代码
  • 鸿蒙Next-拉起支付宝的三种方式——教程
  • 【Linux】理解Linux中的软链接与硬链接
  • 传统CV算法——背景建模算法介绍
  • 架构基础 -- 打点系统之FastAPI、python、grafana、prometheus实现
  • Unity数据持久化 之 文件操作(增删查改)
  • 【赵渝强老师】大数据生态圈中的组件
  • 8 自研rgbd相机基于rk3566之sensor图像解析与AWB算法原理
  • SQL语言的规则和规范
  • 盘点成都产业园前十,寻找你的理想创业地!
  • Pencils Protocol生态新进展,即将上线 Vault 产品
  • Python的list和numpy的array有什么区别?
  • 2024自动化测试面试真题(附答案)!
  • 基于FPGA的OV5640摄像头图像采集
  • centOS如何查看并放行防火墙3306端口
  • 无人机喊话器详解!!!
  • flannel,etcd,docker
  • Jmeter(十四)Jmeter分布式部署测试
  • LeetCode - 10 正则表达式匹配
  • C#文件的输入和输出
  • MATLAB生成COE文件
  • Java类和对象之构造方法与对象创建之计算数学中的分数值