第七篇:SpringBoot整合Redis
redis主要是做缓存,比如用户登录,未付款的订单等等。这一篇简单介绍下基本用法
工程建设
1. pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<!--jedis连接池-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
2. application.properties

3.RedisService
@Service public class RedisService { @Autowired RedisTemplate redisTemplate; public void add(String key, String val){ ValueOperations ops = redisTemplate.opsForValue(); ops.set(key, val); } public String select(String key){ ValueOperations ops = redisTemplate.opsForValue(); return (String) ops.get(key); } }
测试
@RunWith(SpringRunner.class) @SpringBootTest public class RedisTest { @Autowired private RedisService redisService; @Test public void add(){ redisService.add("name","紫霞"); } @Test public void get(){ String v = redisService.select("name"); System.out.println(v); } }
测试通过即可
Redis还有很多,缓冲池等等,自行深入研究

浙公网安备 33010602011771号