SpringBoot整合Redis[单机版]
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
添加配置
server: port: 8080 spring: application: name: redis redis: host: 192.168.247.141 port: 6379 password: 123456 database: 0
编写代码
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可以+这个一起
标签:单机版,SpringBoot,redis,Redis,springframework,key,import,org,String From: https://www.cnblogs.com/flower-dance/p/16749914.html