springboot二集成redis

3次阅读

共计 2542 个字符,预计需要花费 7 分钟才能阅读完成。

相隔很久,我又回来了,献上一篇整合 redis 的教程给新手看一下吧,技术点不怎么有,最简单的配置,入手即用,那下面开始

本章在我的上一篇文章为基础继续整合的,不知道的可以见我的整合 tkmapper
springboot 整合 tkmapper
1、下面开始引入 pom 依赖

        <!--  springboot 整合 redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2、yml 文件根据自己配置添加 redis 信息

 spring: 
    redis:
        host: 127.0.0.1
        database: 0
        password: redis
        port: 6379
        timeout: 60000

3、HelloController 代码,用户访问接口测试 redis


    package com.cxt.demo.controller;

import com.cxt.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liBai
 * @Classname HelloController
 * @Description TODO
 * @Date 2019-06-02 10:49
 */
@RestController
@RequestMapping("/test")
public class HelloController {
    @Autowired
    private HelloService helloService;
    @RequestMapping("/hello")
    public String hello(){return helloService.sayHello();
    }
    @RequestMapping("/get")
    public String getRedisInfo(){return helloService.getRedisInfo();
    }
}

4、HelloService 代码

package com.cxt.demo.service;

/**
 * @author liBai
 * @Classname HelloService
 * @Description TODO
 * @Date 2019-06-02 10:49
 */
public interface HelloService {String sayHello();

    String getRedisInfo();}

impl

package com.cxt.demo.service.impl;

import com.cxt.demo.bean.TestSys;
import com.cxt.demo.dao.TestSysMapper;
import com.cxt.demo.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author liBai
 * @Classname HelloServiceImpl
 * @Description TODO
 * @Date 2019-06-02 10:50
 */
@Service
@Slf4j
public class HelloServiceImpl implements HelloService {
    @Autowired
    private TestSysMapper testSysMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public String sayHello() {TestSys testSys = testSysMapper.selectByPrimaryKey("1");
        redisTemplate.opsForValue().set("testSys",testSys.toString(),10,TimeUnit.MINUTES);
        log.info("redis set value =" +testSys.toString());
        return "redis set value success,userName ="+testSys.getName();}

    @Override
    public String getRedisInfo() {log.debug("redis get info {}",redisTemplate.opsForValue().get("testSys"));
        return redisTemplate.opsForValue().get("testSys");
    }
}

5、到这初级版本整合就可以使用了,正常项目中不可能这么简单使用 redis,毕竟 redis 的用处还是很强大的,例如单机,集群,哨兵等,具体的配置就自己挖掘吧,或者继续关注我后续的文章,下面就来演示一下这节的成果吧
浏览器访问
http://localhost:8081/test/hello

上面已经看到 redis 放入字符串已经放进去了,下面就拿出来试试吧
访问
http://localhost:8081/test/get

到了这基本也就结束了,有问题欢迎留言讨论,如有错误请指出一起交流,谢谢!

正文完
 0