1. Spring boot Redis
1.1 环境搭建
- 修改pom.xml文件,添加依赖(redis、测试)
<dependencies> <!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <!--测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
yml配置
spring: redis: database: 0 #确定使用库 host: 127.0.0.1 #redis服务地址 port: 6379 #redis 端口号
启动类
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author 桐叔 * @email liangtong@itcast.cn */ @SpringBootApplication public class TestRedisApplication { public static void main(String[] args) { SpringApplication.run(TestRedisApplication.class, args); } }
测试类
package com.czxy.test; import com.czxy.TestRedisApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; /** * @author 桐叔 * @email liangtong@itcast.cn */ @RunWith(SpringRunner.class) //spring 整合 Junit @SpringBootTest(classes = TestRedisApplication.class) //spring boot 整合 junit public class TestRedis { @Resource //注意:命名必须是 stringRedisTemplate private StringRedisTemplate stringRedisTemplate; @Test public void demo01(){ System.out.println(stringRedisTemplate); } }
1.2 StringRedisTemplate
1.2.1 常用方法
- Redis 一共有5种类型,StringRedisTemplate提供对5种类型操作。
1.2.2 字符串操作
- 具体操作,通过 opsForValue() 获得字符串操作对象,具体的操作
1.2.3 测试
1.package com.czxy.test; import com.czxy.TestRedisApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.util.concurrent.TimeUnit; /** * @author 桐叔 * @email liangtong@itcast.cn */ @RunWith(SpringRunner.class) //spring 整合 Junit @SpringBootTest(classes = TestRedisApplication.class) //spring boot 整合 junit public class TestRedis { @Resource //注意:命名必须是 stringRedisTemplate private StringRedisTemplate stringRedisTemplate; @Test public void demo01(){ //添加字符串数据 stringRedisTemplate.opsForValue().set("test001","我是测试数据"); } /** * TimeUnit 时间单位 * TimeUnit.SECONDS 秒 * TimeUnit.MINUTES 分 * TimeUnit.HOURS 小时 * TimeUnit.DAYS 添加 * TimeUnit.MILLISECONDS 毫秒 * TimeUnit.MICROSECONDS 微秒 * TimeUnit.NANOSECONDS 纳秒 */ @Test public void demo02(){ //添加字符串数据,有效时间5分钟 stringRedisTemplate.opsForValue().set("test002","测试数据",5, TimeUnit.MINUTES); } @Test public void demo03(){ //获得字符串数据 String str = stringRedisTemplate.opsForValue().get("test001"); System.out.println(str); } @Test public void demo04(){ //删除数据 stringRedisTemplate.delete("test001"); } }