- SpringBoot集成Redis
- 1. 添加依赖(操作Redis数据类型的依赖)
- 2. 在SpringBoot的核心配置文件中添加Redis的配置
- 3. 往Redis中读取、写入数据
- 4. 程序跑起来——看结果
2. 在SpringBoot的核心配置文件中添加Redis的配置org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-redis
#配置Redis配置信息
spring:
redis:
host: 192.168.0.128
port: 6379
password: 123456 #如果没有设置密码可以直接去掉这个属性
3. 往Redis中读取、写入数据
1.StudentController.java
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/put")
public String put(String key,String value){
studentService.put(key,value);
return "值已经成功放入Redis";
}
@GetMapping("/get")
public String get(String key){
Object value = studentService.get(key);
return key+"对应的Value值为:"+value;
}
}
2.StudentService.java
package com.guo.service;
public interface StudentService {
void put(String key, String value);
Object get(String key);
}
3.StudentServiceImpl.java
package com.guo.service.impl;
import com.guo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentService {
//引入操作Redis的模板象
@Autowired
private RedisTemplate
4. 程序跑起来——看结果
- 通过网址进行访问:
http://localhost:8080/put?key=name&value=zhangsan
- 通过Windows上的可视化界面查看值是否成功存入
- 通过网址读取Redis中的数据



