【xxx-mall学习系列-基础框架使用-002】-基于ssm整合Redis

279 阅读1分钟

在001节中的ssm整合的基础之上整合Redis

1. 依赖引入

        <!-- redis -->
        <dependency>
            <groupId>com.xxx.basic</groupId>
            <artifactId>xxx-starter-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>com.xxx.basic</groupId>
            <artifactId>xxx-starter-test</artifactId>
        </dependency>

在ssm整合的基础上,引入了基础组件的Redis以及单元测试的依赖

2. 单元测试类

1. 单元测试基类

package com.xxx.learn.redis;

import com.xxx.learn.App;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author amyzhang
 * @history 2020/7/31 新建
 * @since JDK1.7
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class BaseTestCase {
}

2. Redis单元测试类

package com.xxx.learn.redis;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author amyzhang
 * @history 2020-09-17 新建
 * @since JDK1.7
 */
@Slf4j
public class RedisTest extends BaseTestCase {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testRedis(){
        redisTemplate.opsForValue().set("test-key","aaa");
        Object result = redisTemplate.opsForValue().get("test-key");
        log.info("------------->{}", JSONObject.toJSONString(result));
    }
}

3. 单元测试

运行单元测试方法,出现以下结果即可。