1. 新建SpringBoot项目

第一步
基本信息
选择依赖
完成

1.1 打开 setting ,配置Maven

1.2 打开pom文件,修改版本为 2.3.4.RELEASE ,刷新maven

修改版本,刷新maven

1.3 新增Swagger3.0依赖,刷新maven

<!-- Swagger3.0 -->
<dependency>
	<groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

1.4 修改application.properties为application.yml,添加redis配置

server:
  port: 7749

# 单节点
spring:
  redis:
    host: 192.168.109.160
    port: 6379
    timeout: 10000
    password: root

2. 搭建好redis单节点

第 4 篇 : 安装Redis-单节点1主2从

3. 开发

3.1 新建com.hahashou.test.redis.config.RedisConfig

package com.hahashou.test.redis.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @description: Redis配置
 * @author: 哼唧兽
 * @date: 9999/9/21
 **/
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        //使用StringRedisSerializer来序列化和反序列化redis的key
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        //开启事务:redisTemplate.setEnableTransactionSupport(true);我觉得一般用不到(该操作是为了执行一组命令而设置的)
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}

3.2 新建com.hahashou.test.redis.request.TestRequest

package com.hahashou.test.redis.request;

import lombok.Data;

/**
 * @description: 入参
 * @author: 哼唧兽
 * @date: 9999/9/21
 **/
@Data
public class TestRequest {

    private String key;

    private String value;
}

3.2 新建com.hahashou.test.redis.controller.TestController

package com.hahashou.test.redis.controller;

import com.hahashou.test.redis.request.TestRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @description: 测试
 * @author: 哼唧兽
 * @date: 9999/9/21
 **/
@RestController
@RequestMapping("/test")
@Api(tags = "测试")
public class TestController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/keyValue")
    @ApiOperation(value = "存储key和value")
    public String keyValue(@RequestBody TestRequest testRequest){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = testRequest.getKey();
        if (StringUtils.hasText(key)) {
            valueOperations.set(key, testRequest.getValue());
        }
        return "success";
    }

    @PostMapping("/value")
    @ApiOperation(value = "根据key获得value")
    public String value(@RequestBody TestRequest testRequest){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = testRequest.getKey();
        if (StringUtils.hasText(key)) {
            Object object = valueOperations.get(key);
            if (object != null) {
                return object.toString();
            }
        }
        return "success";
    }
}

4. 启动项目测试

Swagger3.0

测试keyValue接口

测试keyValue
测试keyValue成功

测试value接口

测试value
测试value成功

连接Redis工具测试

连接redis工具测试结果

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐