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")); } }

猜你喜欢

转载自www.cnblogs.com/wangzhuxing/p/10195198.html