ITPub博客

首页 > 架构设计 > > 微服式务分布云构架-SpringBoot整合RedisTemplate 模版操作

微服式务分布云构架-SpringBoot整合RedisTemplate 模版操作

作者:gung123 时间:2020-02-26 18:46:33 0 删除 编辑

在 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 中的数据存储操作了。


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69952307/viewspace-3146503/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-11-01

  • 博文量
    200
  • 访问量
    79622