因为要建立测试类测试是否整合成功所以
1.导入测试类的依赖和redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.建实体类,需要序列化
package com.example.demo.Pojo;
import java.io.Serializable;
public class User implements Serializable {
private Long id;
private String name;
private Integer age;
public User() {
}
public User(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.建立测试类
package com.example;
import com.example.demo.DemoApplication;
import com.example.demo.Pojo.User;
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.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class AppTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedisTemplate(){
// 存d到redis数据库
redisTemplate.opsForValue().set("hello","0708java");
// 取
String str =(String) redisTemplate.opsForValue().get("hello");
System.out.println(str);
User user = new User();
user.setId(1L);
user.setName("admin");
user.setAge(12);
redisTemplate.opsForValue().set("user",user);
User user1 = (User)redisTemplate.opsForValue().get("user");
System.out.println(user1);
}
}
4.启动