前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot整合Redis[单机版]

SpringBoot整合Redis[单机版]

作者头像
彼岸舞
发布2022-10-04 08:21:15
2710
发布2022-10-04 08:21:15
举报

SpringBoot整合Redis[单机版]

添加依赖

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

添加配置

代码语言:javascript
复制
server:
  port: 8080
spring:
  application:
    name: redis
  redis:
    host: 192.168.247.141
    port: 6379
    password: 123456
    database: 0

编写代码

代码语言:javascript
复制
package com.dance.redis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/string/set")
    public String stringSet(String key,String value){
        redisTemplate.opsForValue().set(key,value);
        return "set ok";
    }
    @GetMapping("/string/get")
    public String stringGet(String key){
        String s = redisTemplate.opsForValue().get(key).toString();
        return "get : " + s;
    }
    @GetMapping("/string/del")
    public String stringSet(String key){
        Boolean delete = redisTemplate.delete(key);
        return "del : "+delete;
    }

}

写了一般最简单的整合, 因为我看别的又是工具类什么的,一大堆, 比如这个精简, 没有任何多余的东西, 大家可以直接根据这个整合, 然后再自己根据业务扩展, 我一般不是很喜欢用别人的工具类啥的, 因为不贴合自己的业务, 反而粘贴了一大堆

我之前写的Redis序列化+自定义RedisTemplate可以+这个一起

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot整合Redis[单机版]
    • 添加依赖
      • 添加配置
        • 编写代码
        相关产品与服务
        云数据库 Redis
        腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档