Spring Boot集成Spring Data Redis
生成项目模板
浏览器打开https://start.spring.io/
添加依赖Spring Data Redis (Access+Driver)
引入依赖
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 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.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置连接信息
application.yml
spring:
redis:
host: 192.168.56.101
port: 6379
password:
database: 0
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: -1
min-idle: 0
简单操作Redis
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean implements CommandLineRunner {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Override
public void run(String... args) throws Exception {
redisTemplate.opsForValue().set("s-string", "spring-data-redis");
redisTemplate.opsForList().rightPush("s-list", "github.com");
redisTemplate.opsForList().rightPush("s-list", "google.com");
redisTemplate.boundSetOps("s-set").add("java", "c", "c++", "go");
redisTemplate.opsForZSet().add("s-zset", "google.com", 1);
redisTemplate.opsForZSet().add("s-zset", "baidu.com", 2);
redisTemplate.boundZSetOps("s-zset").add("bing.com", 3);
redisTemplate.opsForHash().put("s-hash", "name", "tom");
redisTemplate.opsForHash().put("s-hash", "age", "20");
redisTemplate.opsForHash().put("s-hash", "email", "tom@example.com");
redisTemplate.opsForHyperLogLog().add("s-hyperloglog", "aa");
redisTemplate.opsForHyperLogLog().add("s-hyperloglog", "bb");
redisTemplate.opsForHyperLogLog().add("s-hyperloglog", "cc");
redisTemplate.opsForHyperLogLog().add("s-hyperloglog", "dd");
redisTemplate.opsForHyperLogLog().add("s-hyperloglog", "dd");
redisTemplate.opsForHyperLogLog().add("s-hyperloglog", "cc");
System.out.println("s-hyperloglog count:" + redisTemplate.opsForHyperLogLog().size("s-hyperloglog"));
System.out.println(redisTemplate.opsForValue().get("s-string"));
}
}
启动类:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
启动测试
启动类DemoApplication
运行后,到redis数据库中即可查看到操作结果