1. 程式人生 > >springboot系列十、springboot整合redis

springboot系列十、springboot整合redis

一、簡介

Redis 的資料庫的整合在 java 裡面提供的官方工具包:jedis,所以即便你現在使用的是 SpringBoot,那麼也繼續使用此開發包。

二、redidTemplate操作

在 Spring 支援的 Redis 操作之中提供有一個 RedisTemplate 處理程式類,利用這個類可以非常方便的實現 Redis 的各種基本數 據操作。

1、引入依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</
artifactId> </dependency>

2、配置yml

spring:  
  redis:
    host: 47.52.199.52
    port: 6379
    password: 123456
    timeout: 1000
    database: 0
    jedis:
      pool:
        max-active: 4
        max-idle: 8
        min-idle: 2
        max-wait: 100

3、使用示例

@RunWith(SpringRunner.class
) @SpringBootTest @WebAppConfiguration public class DemoApplicationTests {

@Resource
private RedisTemplate<String, String> redisTemplate; @Test public void testRedis(){ redisTemplate.opsForValue().set("xing","12345678"); System.out.println(redisTemplate.opsForValue().get(
"xing")); } }