springboot搭建后台框架 (二)整合Redis

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: springboot搭建后台框架 (二)整合Redis

相隔很久,我又回来了,献上一篇整合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

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

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
7天前
|
Web App开发 编解码 Java
B/S基层卫生健康云HIS医院管理系统源码 SaaS模式 、Springboot框架
基层卫生健康云HIS系统采用云端SaaS服务的方式提供,使用用户通过浏览器即能访问,无需关注系统的部署、维护、升级等问题,系统充分考虑了模板化、配置化、智能化、扩展化等设计方法,覆盖了基层医疗机构的主要工作流程,能够与监管系统有序对接,并能满足未来系统扩展的需要。
33 4
|
26天前
|
NoSQL Java Redis
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
300 0
|
1月前
|
消息中间件 NoSQL Java
springboot redis 实现消息队列
springboot redis 实现消息队列
42 1
|
1月前
|
NoSQL Java Redis
SpringBoot集成Redis
SpringBoot集成Redis
456 0
|
4天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
10天前
|
NoSQL 数据可视化 Java
Springboot整合redis
Springboot整合redis
|
10天前
|
人工智能 前端开发 Java
Java语言开发的AI智慧导诊系统源码springboot+redis 3D互联网智导诊系统源码
智慧导诊解决盲目就诊问题,减轻分诊工作压力。降低挂错号比例,优化就诊流程,有效提高线上线下医疗机构接诊效率。可通过人体画像选择症状部位,了解对应病症信息和推荐就医科室。
151 10
|
19天前
|
NoSQL Java Redis
Springboot整合redis
Springboot整合redis
|
27天前
|
NoSQL Java Redis
SpringBoot集成Redis
SpringBoot集成Redis
54 1
|
30天前
|
存储 监控 NoSQL
SpringBoot 后台管理系统
SpringBoot 后台管理系统
12 0

热门文章

最新文章