绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
微服式务分布云构架-SpringBoot整合RedisTemplate 模版操作
2020-02-26 18:46:33

在 Spring 支持的 Redis 操作之中提供有一个 RedisTemplate 处理程序类,利用这个类可以非常方便的实现 Redis 的各种基本数 据操作。

1、 修改项目中的 pom.xml 配置文件,追加 redis 的依赖引用:了解springcloud架构可以加求求:三五三六二四七二五九

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、 如果要想使用 Redis 操作,则一定要修改 application.yml 配置文件,在这个配置文件之中要进行 Redis 的各种连接配置处理; 

spring:
redis:
host: 192.168.68.166
port: 6379
password: studyjava
timeout: 1000
database:
pool:
max-active: 10
max-idle: 8
min-idle: 2
max-wait: 100

3、 下面就可以通过程序来利用 RedisTemplate 模版进行数据处理了,因为以上的配置一旦完成之后会在 Spring 内部帮助用户直接 获得一个 RedisTemplate 模版处理对象,为了简单,直接建立一个测试类完成: 

package cn.study.microboot;
import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestRedis {
@Resource
private RedisTemplate<String, String> redisTemplate;
@Test
public void testSet() {
this.redisTemplate.opsForValue().set("study", "java");
System.out.println(this.redisTemplate.opsForValue().get("study"));
}
}

则此时就可以利用 Redis 实现在 SpringBoot 中的数据存储操作了。


分享好友

分享这个小栈给你的朋友们,一起进步吧。

数据架构
创建时间:2020-05-20 11:23:41
有关数据架构的小栈里面全都有
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • 小雨滴
    专家
戳我,来吐槽~