资讯专栏INFORMATION COLUMN

SpringBoot+Redis的入门教程

dmlllll / 3185人阅读

摘要:历史文章如何在安装最新版安装安装最新版的入门教程教程内容备注本系列开发工具均为构建项目,选择后面发现其实没有用到三个基本的依赖。

本博客 猫叔的博客,转载请申明出处

本系列教程为HMStrange项目附带。

历史文章

如何在VMware12安装Centos7.6最新版

Centos7.6安装Java8

Centos7.6安装MySQL+Redis(最新版)

SpringBoot+MySQL+MyBatis的入门教程

教程内容
备注:本系列开发工具均为IDEA
1、构建项目,选择Lombok(后面发现其实没有用到)、Web、Redis三个基本的Maven依赖。

pom文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.github.myself
    redisdemo
    0.0.1-SNAPSHOT
    redisdemo
    Demo project for Spring Boot

    
        1.8
    

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

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2、准备Redis,这里可以参考历史文章的安装Redis环节,我使用Redis Desktop Manager桌面工具进行链接

3、构建项目目录,我构建了一个普通的web项目目录,切了dao层,仅仅由service、controller

4、填写application.yml,默认生成不是yml,不过我觉得yml视觉效果好一些,就改了一下,我们需要填写Redis链接信息
spring:
  redis:
    host: 192.168.192.133
5、构建service接口,这里普通实现两个set、get信息的业务
package com.github.myself.service;

/**
 * Created by MySelf on 2019/4/11.
 */
public interface MsgService {

    public String setMsg(String key,String msg);

    public String getMsg(String key);

}
6、接口实现,引入RedisTemplate
package com.github.myself.service.impl;

import com.github.myself.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * Created by MySelf on 2019/4/11.
 */
@Service
public class MsgServiceImpl implements MsgService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String setMsg(String key,String msg) {
        redisTemplate.opsForValue().set(key,msg);
        return "success";
    }

    @Override
    public String getMsg(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }
}
7、controller层的业务实现了
package com.github.myself.controller;

import com.github.myself.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by MySelf on 2019/4/11.
 */
@RestController
@RequestMapping("/msg")
public class MsgController {

    @Autowired
    private MsgService msgService;

    @GetMapping("/set")
    public String setMsg(@RequestParam(value = "key") String key,@RequestParam(value = "msg") String msg){
        return msgService.setMsg(key,msg);
    }

    @GetMapping("/get")
    public String getMsg(@RequestParam(value = "key") String key){
        return msgService.getMsg(key);
    }

}
8、启动项目,使用Postmane测试

9、项目下载地址

欢迎到HMStrange项目进行下载:https://github.com/UncleCatMy...

公众号:Java猫说

学习交流群:728698035

现架构设计(码农)兼创业技术顾问,不羁平庸,热爱开源,杂谈程序人生与不定期干货。

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

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

相关文章

  • SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列

    摘要:环境依赖创建一个新的工程,在其文件加入依赖创建一个消息接收者类,它是一个普通的类,需要注入到中。 这篇文章主要讲述如何在springboot中用reids实现消息队列。 准备阶段 安装redis,可参考我的另一篇文章,5分钟带你入门Redis。 java 1.8 maven 3.0 idea 环境依赖 创建一个新的springboot工程,在其pom文件,加入spring-boot-...

    APICloud 评论0 收藏0
  • SpringBoot非官方教程 | 第九篇: SpringBoot整合Redis

    摘要:经过上述两步的操作,你可以访问数据了。数据访问层通过来访问分钟过期单元测试启动单元测试,你发现控制台打印了单元测试通过源码下载参考资料 这篇文章主要介绍springboot整合redis 引入依赖 在pom文件中添加redis依赖: org.springframework.boot spring-boot-starter-data-redis 配置数据源 spri...

    csRyan 评论0 收藏0
  • 两年了,我写了这些干货!

    摘要:开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章系列处理登录请求前后端分离一使用完美处理权限问题前后端分离二使用完美处理权限问题前后端分离三中密码加盐与中异常统一处理 开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章! Spring Boo...

    huayeluoliuhen 评论0 收藏0

发表评论

0条评论

dmlllll

|高级讲师

TA的文章

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