SpringBoot整合Redis步骤:
1.在pom.xml添加依赖
<!--springboot整合redis相关依赖引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.在application.yml配置文件中配置redis的ip和端口
#Redis配置
spring:
redis:
host: 192.168.175.15
port: 6379
3.在测试代码代码中,使用RedisTemplate对象实现redis操作
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class TestDemo {
//SpringBoot整合Redis
//1.获取RedisTemplate对象
@Autowired
private RedisTemplate redisTemplate;
//测试方法
@Test
public void getRedis(){
redisTemplate.opsForValue().set("Hello","ni hao");
String hello = (String)redisTemplate.opsForValue().get("Hello");
System.out.println(hello);
MyUser myUser = new MyUser();
myUser.setId(3);
myUser.setName("哈哈");
myUser.setPassword("123456");
myUser.setUsername("haha");
redisTemplate.opsForValue().set("myUser",myUser);
MyUser myUser1 = (MyUser)redisTemplate.opsForValue().get("myUser");
System.out.println(myUser1);
}
}
4.查看运行结果