最近工作需要使用springboot操作redis。
一脸懵逼,都不会啊。
从网上这块看看那块看看。有的不能运行。想了下,自己弄下吧
从头开始,上图。
1、选择Spring Initializr, JDK选择1.8. 接着NEXT
2、没什么说的,默认即可。如果有需要改名的可以改名。接着next
3、选择web、Redis ,接下来next。(如果找不到,到搜索框中搜索即可)
4、接着next。然后稍稍等下,在下载相应的依赖jar包。
5、看下项目目录。主要是这几个。
6、接下来看下 pom.xml文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
7、接下来在application.properties文件中,配置redis的链接信息。
# REDIS Cluster (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=1
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=600
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=1000
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=300
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=100
# 连接超时时间(毫秒)
spring.redis.timeout=2500
time=5
8、接下来创建demo文件。文件中通过RedisTemplate类操作Redis。包括插入和查询String/map/list/set/zset。
上代码
package com.example.demo;
import java.util.*;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Demo {
@Resource
private RedisTemplate9、接下来验证下。下面的类中有main函数。只需要右键-》RUN就ok
10.输入地址。
11.控制台打印。
12、睡觉~~