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>Redis_maven</groupId>
<artifactId>Reids_maven</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<!-- Spring Boot Cache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
</dependencies>
</project>
main函数
package test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.Iterator;
import java.util.Set;
/*
spring boot 整合 redis
*/
@SpringBootApplication
public class App {
public static void main(String args[])
{
SpringApplication.run(App.class,args);
System.out.println("项目开始启动");
// 连接池相关配置
JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(20);
jedisPoolConfig.setTestOnBorrow(true);//设置成True ,获取的redis 对象均可以使用
jedisPoolConfig.setMaxWaitMillis(10000);
//连接池
JedisPool jedisPool=new JedisPool();
// GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password
JedisPool jedisPool1=new JedisPool(jedisPoolConfig,"localhost",6379,10000,"");
System.out.println(jedisPool.getResource());
//关于字符串的操作 写入和获取
jedisPool.getResource().set("2021","字符串写入 ");
System.out.println(jedisPool.getResource().get("2021"));
//关于集合的操作 写入和获取
jedisPool.getResource().lpush("list1","张三");
jedisPool.getResource().lpush("list1","李四");
jedisPool.getResource().lpush("list1","王武");
System.out.println("集合输出:"+jedisPool.getResource().lrange("list1",0,2));
//keys 实例的获取 即String 、list 的key 值 等
Set<String> set=jedisPool.getResource().keys("*");
System.out.println("set.size:"+set.size());
Iterator iterator=set.iterator();//获取关于该集合的 iterator
while(iterator.hasNext())
{
System.out.println(iterator.next().toString());
}
System.out.println(jedisPool.getResource().get("2021"));
}
}
关于redis的数据类型具体的操作APi介绍 借鉴了
https://blog.csdn.net/asd051377305/article/details/90234620?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-11