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 测试
- 启动项目,打开浏览器访问http://localhost:8080/user/set,set数据。
2. 启动项目,打开浏览器访问http://localhost:8080/user/get,get数据。