一起學習Springboot(五):Springboot整合redis
阿新 • • 發佈:2018-12-18
引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
redis配置
#redis伺服器地址 spring.redis.host=172.20.13.144 #redis伺服器埠 spring.redis.port=6415 #redis伺服器連線密碼 spring.redis.password= #連線超時時間 spring.redis.timeout=10000 #連線池維護最大連線數(負值表示沒有限制) spring.redis.jedis.pool.max-active=10 #連線池最大空閒連線 spring.redis.jedis.pool.max-idle=10 #連線池最小空閒連線 spring.redis.jedis.pool.min-idle= 0 #最大阻塞等待時間(負值表示沒有限制) spring.redis.jedis.pool.max-wait=-1
完成上面兩步就可以直接使用redis了,是不是極其簡單
測試
@RunWith(SpringRunner.class) @SpringBootTest public class SpringbootRedisApplicationTests { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; @Test public void contextLoads() { redisTemplate.opsForValue().set("name","張三"); String name = (String) redisTemplate.opsForValue().get("name"); stringRedisTemplate.boundValueOps("age").set("26"); String age = stringRedisTemplate.boundValueOps("age").get(); System.out.println(name); System.out.println(age); } }