Spring boot集成redis教程
项目结构
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.linyuan</groupId>
<artifactId>redis_0101</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<!-- spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring-boot-starter-data-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
application.properties
########################################################
###Redis (RedisConfiguration)
########################################################
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
#spring.redis.password=123456
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=5000
RedisTest测试类
package com.linyuan.redis_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.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import com.linyuan.redis.App;
import com.linyuan.redis.service.RedisService;
@RunWith(SpringRunner.class)
@SpringBootTest(classes =App.class)
public class RedisTest {
@Autowired
private RedisService redisService;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@org.junit.Test
public void addString() {
redisService.setStr("name","linyuan");
}
@org.junit.Test
public void addList() {
stringRedisTemplate.opsForList().leftPush("stuList", "林");
stringRedisTemplate.opsForList().rightPush("stuList", "wangwu");
}
@org.junit.Test
public void popList() {
stringRedisTemplate.opsForList().leftPop("stuList");
}
@org.junit.Test
public void addSet() {
stringRedisTemplate.opsForSet().add("goodsSet", "苹果");
stringRedisTemplate.opsForSet().add("goodsSet", "苹果2");
stringRedisTemplate.opsForSet().add("goodsSet", "苹果1");
stringRedisTemplate.opsForSet().add("goodsSet", "苹果10");
Boolean isMember = stringRedisTemplate.opsForSet().isMember("goodsSet", "苹果");
System.out.println(isMember);
}
@org.junit.Test
public void removeSet() {
stringRedisTemplate.opsForSet().remove("goodsSet", "苹果");
}
@org.junit.Test
public void addSortedSet() {
stringRedisTemplate.opsForZSet().add("goodsSortedSet", "苹果",10);
stringRedisTemplate.opsForZSet().add("goodsSortedSet", "梨",100);
stringRedisTemplate.opsForZSet().add("goodsSortedSet", "脐橙",8);
}
@org.junit.Test
public void removeSortedSet() {
stringRedisTemplate.opsForZSet().remove("goodsSortedSet", "苹果");
}
@org.junit.Test
public void addHMSet() {
stringRedisTemplate.opsForHash().put("person", "name", "linyuan");
stringRedisTemplate.opsForHash().put("person", "pass", "123456");
}
@org.junit.Test
public void removeHMSet() {
stringRedisTemplate.opsForHash().delete("person", "pass");
}
}