pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.redis</groupId>
<artifactId>redisdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>redisdemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
#redis
spring.redis.host=localhost
spring.redis.port=6379
component:
package com.redis.component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
/**
* Created by lpw'ASUS on 2018/6/1.
*/
@Component
public class ZjRedisComponent {
@Autowired
private StringRedisTemplate template;
/**
* 存储值,key,value格式
* @param key
* @param value
*/
public void set (String key,String value){
ValueOperations<String,String> ops = this.template.opsForValue();
if(!this.template.hasKey(key)){
ops.set(key,value);
}else {
System.out.println("this key = "+ops.get(key));
}
}
/**
* 获取key的值
* @param key
* @return
*/
public String get(String key){
return template.opsForValue().get(key);
}
/**
* 删除key这条数据
* @param key
*/
public void delete(String key){
this.template.delete(key);
}
}
测试方法:
package com.redis;
import com.redis.component.ZjRedisComponent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisdemoApplicationTests {
@Autowired
private ZjRedisComponent zjRedisComponent;
@Test
public void contextLoads() {
}
@Test
public void set(){
zjRedisComponent.set("myname","李四");
}
@Test
public void get(){
System.out.println("取到的值myname = "+zjRedisComponent.get("myname"));
}
}