First, the project architecture
Project content
1.pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springboot-redis</groupId> <artifactId>springboot-redis</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springboot-redis Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 父级项目 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencies> <!-- 测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- springmvc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <-! Jpa (persistence) -> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </dependencies> <!-- 编译 --> <build> <!-- 插件 --> <plugins> <!-- maven插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
2.RedisConnectionTest.java
package com.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class RedisConnectionTest { @Autowired private RedisTemplate<String,String> redisTemplate; @Test public void set(){ redisTemplate.opsForValue().set("name","echo"); System.out.println(redisTemplate.opsForValue().get("name")); } }
3.application.properties
server.port: 8890 # Redis database indexes (the default is 0, which has a total of 16 database, we use the first one is 0) spring.redis.database = 0 # Redis server address spring.redis.host = 127.0.0.1 # Redis server port spring.redis.port = 6379 # Redis server connection password (blank by default) # spring.redis.password = 48835a9c-7a5a-4ec8-af6a- 80af3e87b194 # connection pool maximum number of connections (negative values no limit ) spring.redis.pool.max -active = 200 is # latency connection pool maximum blocking (negative values no limit) spring.redis.pool.max -wait = -1 # connection pool maximum idle connection spring.redis .pool.max -idle = 10 # minimum connection pool idle connections spring.redis.pool.min -idle. 1 = # connection time (ms) spring.redis.timeout = 3000
Right-click directly here RedisConnectionTest-> run as application, in order to form junit test this project.