spring boot 整合redis
<!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.2.RELEASE</version> </dependency>
添加上述依赖
添加properties中Redis的配置连接
# REDIS 的配置信息(RedisProperties) # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=localhost # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接池最大连接数(使用负值表示没有限制) spring.redis.jedis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1 # 连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=8 # 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0 # 连接超时时间(毫秒) spring.redis.timeout=0
下面示例演示利用StringRedisTemplate添加到redis中
package com.ssm.springboot; 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.*; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.bind.annotation.RequestMapping; import java.util.HashMap; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootApplicationTests { @Autowired private StringRedisTemplate stringRedisTemplate; @Test public void testRedis(){ ValueOperations<String, String> svo = stringRedisTemplate.opsForValue(); svo.set("1","bbb"); String s = svo.get("1"); System.out.println(s); //添加一个set集合的 SetOperations<String, String> set = stringRedisTemplate.opsForSet(); set.add("2222","222","2222","3333"); //获取set的集合 set.members("2222"); System.out.println(set.members("2222")); //添加一个hash集合 HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash(); HashMap<Object, Object> map = new HashMap<>(); map.put("hs","has"); map.put("name","test"); hash.putAll("lpMap",map); System.out.println(hash.entries("lpMap")); //添加一个list列表 ListOperations<String, String> list = stringRedisTemplate.opsForList(); list.rightPush("list","lp"); list.rightPush("list","66"); System.out.println(list.range("list",0,1)); //添加一个有序的set集合 ZSetOperations<String, String> ssz = stringRedisTemplate.opsForZSet(); ssz.add("lpzset","lp",0); ssz.add("lpzset","26",1); ssz.add("lpzset","177",2); System.out.println(ssz.rangeByScore("lpzset",0,2)); } }