SpringBoot——SpringBoot集成Redis

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: SpringBoot——SpringBoot集成Redis

1.开始


SpringBoot集成Redis了话,这里我不再集成MyBatis了,就单纯的做一个简单的模拟,我们知道这个过程是怎么样的就可以了。

2.步骤


2.1 pom.xml文件中添加依赖

<!-- SpringBoot集成Redis的起步依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.2 配置SpringBoot核心配置文件

  #设置redis的配置信息
  spring.redis.host=localhost
  spring.redis.port=6379

这里没有配置内嵌Tomcat端口号,以及项目的上下文根,所以默认端口号就是8080,上下文根就是 /


2.3 创建一个StudentService和它的实现类

package com.songzihao.springboot.service;
/**
 *
 */
public interface StudentService {
    void put(String key, String value);
    String get(String key);
}

配置了上面的步骤,Spring Boot 将自动配置 RedisTemplate,在需要操作 redis 的类中注入 redisTemplate 即可。

注意:Spring Boot 帮我们注入 RedisTemplate 类,泛型里面只能写 <String, String> <Object, Object> 、或者什么都不写。

package com.songzihao.springboot.service.impl;
import com.songzihao.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
/**
 *
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public void put(String key, String value) {
        redisTemplate.opsForValue().set(key,value);
    }
    @Override
    public String get(String key) {
        String count= (String) redisTemplate.opsForValue().get(key);
        return count;
    }
}

2.4 创建一个StudentController

其中 /put 对应的操作是将值存入redis/get 对应的操作是redis中取值。

package com.songzihao.springboot.controller;
import com.songzihao.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
 *
 */
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;
    @RequestMapping(value = "/put")
    public Object put(String key,String value) {
        studentService.put(key,value);
        return "值已成功放入redis";
    }
    @RequestMapping(value = "/get")
    public String get() {
        String count=studentService.get("count");
        return "数据count为: " + count;
    }
}

2.5 启动Redis服务

2.6 启动入口类测试

 

 

相关实践学习
基于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
相关文章
|
18天前
|
消息中间件 Java Kafka
Springboot集成高低版本kafka
Springboot集成高低版本kafka
|
24天前
|
NoSQL Java Redis
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
275 0
|
1天前
|
Java Docker 容器
SpringBoot项目集成XXL-job
SpringBoot项目集成XXL-job
|
3天前
|
Java 关系型数据库 数据库
【SpringBoot系列】微服务集成Flyway
【4月更文挑战第7天】SpringBoot微服务集成Flyway
【SpringBoot系列】微服务集成Flyway
|
8天前
|
NoSQL 数据可视化 Java
Springboot整合redis
Springboot整合redis
|
8天前
|
人工智能 前端开发 Java
Java语言开发的AI智慧导诊系统源码springboot+redis 3D互联网智导诊系统源码
智慧导诊解决盲目就诊问题,减轻分诊工作压力。降低挂错号比例,优化就诊流程,有效提高线上线下医疗机构接诊效率。可通过人体画像选择症状部位,了解对应病症信息和推荐就医科室。
148 10
|
18天前
|
NoSQL Java Redis
Springboot整合redis
Springboot整合redis
|
18天前
|
SQL Java 调度
SpringBoot集成quartz定时任务trigger_state状态ERROR解决办法
SpringBoot集成quartz定时任务trigger_state状态ERROR解决办法
|
25天前
|
NoSQL Java Redis
SpringBoot集成Redis
SpringBoot集成Redis
54 1
|
16天前
|
NoSQL Linux Redis
06- 你们使用Redis是单点还是集群 ? 哪种集群 ?
**Redis配置:** 使用哨兵集群,结构为1主2从,加上3个哨兵节点,总计分布在3台Linux服务器上,提供高可用性。
230 0