SpringBoot项目使用idea创建
https://blog.youkuaiyun.com/w8827130/article/details/109934393 最简单SpringBoot项目创建
https://blog.youkuaiyun.com/w8827130/article/details/110458979 Windows环境安装Redis
项目结构
项目目录
pom.xml
springboot的pom整合了redis
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springbootredis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootredis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
springboot中关于redis的配置
spring:
redis:
host: 127.0.0.1
port: 6379
password: 123456
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 500
min-idle: 0
lettuce:
shutdown-timeout: 0
字符串存储
RedisController.java
使用redisTemplate,存入key=myKey,value=myValue
package com.example.springbootredis.controller;
import com.example.springbootredis.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class RedisController {
private static final Logger log = LoggerFactory.getLogger(RedisController.class);
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("setRedisString")
public String setRedisString() {
redisTemplate.opsForValue().set("myKey","myValue");
log.info(redisTemplate.opsForValue().get("myKey").toString());
return redisTemplate.opsForValue().get("myKey").toString();
}
}
请求接口
网页返回了接口存入的value值
redis中存储的myKey数据
实体类的存储
User.java
这里如果User没有实现Serializable接口,会报错 DefaultSerializer requires a Serializable payload but received an object of type [com.example.springbootredis.pojo.User]
package com.example.springbootredis.pojo;
import java.io.Serializable;
public class User implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
RedisObjectController.java
package com.example.springbootredis.controller;
import com.example.springbootredis.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisObjectController {
private static final Logger log = LoggerFactory.getLogger(RedisObjectController.class);
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("setRedisObject")
public String setRedisObject() {
User user = new User();
user.setName("cuidaidai");
redisTemplate.opsForValue().set("user",user);
log.info(((User)(redisTemplate.opsForValue().get("user"))).getName());
return ((User)(redisTemplate.opsForValue().get("user"))).getName();
}
}
请求setRedisObject接口
网页返回User的name