SpringBoot整合Redis

it2026-09-23  18

1.引入jar包

<!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> //会自动注入

2.yml文件配置

spring: redis: host: 192.168.188.146 port: 6379

3.编写测试类

package com.offcn; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.junit4.SpringRunner; /** * @Auther: lm * @Date: 2020/10/22 10:56 * @Description: */ @RunWith(SpringRunner.class) @SpringBootTest(classes = {UserStartApplication.class}) // 主启动类.class public class Test { @Autowired private StringRedisTemplate stringRedisTemplate; //数据类型单一 少了存入数据没有序列化操作 与redisTemplate不互通 @org.junit.Test public void saveValue(){ stringRedisTemplate.opsForValue().set("message","欢迎来到优就业学习"); } }
最新回复(0)