资讯专栏INFORMATION COLUMN

springboot(二)集成redis

zhangke3016 / 437人阅读

摘要:相隔很久,我又回来了,献上一篇整合的教程给新手看一下吧,技术点不怎么有,最简单的配置,入手即用,那下面开始本章在我的上一篇文章为基础继续整合的,不知道的可以见我的整合整合下面开始引入依赖整合文件根据自己配置添加信息代码,用户

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

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

        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        

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

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77788.html

相关文章

  • 关于springboot集成redis及关于redis的Key 乱码问题

    摘要:一在文件中引入二配置地址等配置数据库索引默认为服务器地址服务器连接端口服务器连接密码默认为空连接池最大连接数使用负值表示没有限制连接池最大阻塞等待时间使用负值表示没有限制连接池中的最大空闲连接连接池中的最小空闲连接连接超时时 一、在pom文件中引入redis org.springframework.boot spring-boot-starter-redis ...

    RancherLabs 评论0 收藏0
  • spring boot - 收藏集 - 掘金

    摘要:引入了新的环境和概要信息,是一种更揭秘与实战六消息队列篇掘金本文,讲解如何集成,实现消息队列。博客地址揭秘与实战二数据缓存篇掘金本文,讲解如何集成,实现缓存。 Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控 - 掘金Health 信息是从 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring...

    rollback 评论0 收藏0

发表评论

0条评论

zhangke3016

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<