1
springboot默认连接本地redis,默认6379端口号,不需要配置,注入redisTemplate后直接使用即可
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.5.3</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.haifei</groupId> 12 <artifactId>springboot6-redis</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>springboot6-redis</name> 15 <description>Demo project for Spring Boot</description> 16 <properties> 17 <java.version>1.8</java.version> 18 </properties> 19 <dependencies> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-data-redis</artifactId> 23 </dependency> 24 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-test</artifactId> 28 <scope>test</scope> 29 </dependency> 30 </dependencies> 31 32 <build> 33 <plugins> 34 <plugin> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-maven-plugin</artifactId> 37 </plugin> 38 </plugins> 39 </build> 40 41 </project>
1 package com.haifei.springboot6redis; 2 3 import org.junit.jupiter.api.Test; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.boot.test.context.SpringBootTest; 6 import org.springframework.data.redis.core.RedisTemplate; 7 8 /** 9 * 运行测试方法前,将本地redis服务器端打开 10 * D:\Program Files\redis-2.8.9\redis-server.exe 11 */ 12 @SpringBootTest 13 class Springboot6RedisApplicationTests { 14 15 @Autowired 16 private RedisTemplate redisTemplate; 17 18 @Test 19 void setTest() { 20 //存入数据 21 redisTemplate.boundValueOps("name").set("zhangsan"); 22 System.out.println("数据存入完成"); 23 } 24 25 @Test 26 void getTest() { 27 //获取数据 28 Object name = redisTemplate.boundValueOps("name").get(); 29 System.out.println(name); 30 } 31 32 }
2
实际项目中,多连接远程redis,可在resources下的配置文件中进行配置