springboot集成redis
下载redis的windows安装包并安装
- 下载地址:https://github.com/MicrosoftArchive/redis/releases
- 安装就一路next,然后找到安装redis的目录,新建start.bat,并写入:redis-server.exe redis.windows.conf 
- 点击start.bat启动(退出redis命令:Ctrl+c)
安装redis客户端Redis Desktop Manager
- 下载地址: https://pan.baidu.com/s/1Jvr9MbgFn4UJh4M1AMo3gA 提取码:3i9b
- 安装就一路next,然后先启动redis,然后,打开客户端
 
代码编写
- 新建StudentController类,并写入: - package com.dj.app.springboot.web; 
 import com.dj.app.springboot.service.StudentService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 @Controller
 public class StudentController {
 @Autowired
 private StudentService studentService;
 // 存
 @RequestMapping(value = "/put")
 public @ResponseBody Object put(String key, String value) {
 studentService.put(key, value);
 return "值已存进redis";
 }
 // 取
 @RequestMapping(value = "/get")
 public @ResponseBody String get() {
 String name = studentService.get("name");
 return "数据为:" + name;
 }
 }
- 新建StudentService接口类,并写入: - package com.dj.app.springboot.service; 
 public interface StudentService {
 /**
 * 将值存进redis
 * @param key
 * @param value
 */
 void put(String key, String value);
 String get(String name);
 }
- 新建StuentServiceImpl实现类,并写入: - package com.dj.app.springboot.service.impl; 
 import com.dj.app.springboot.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 {
 @Autowired
 private RedisTemplate<Object, Object> redisTemplate;
 @Override
 public void put(String key, String value) {
 redisTemplate.opsForValue().set(key, value);
 }
 @Override
 public String get(String name) {
 String res = (String) redisTemplate.opsForValue().get(name);
 return res;
 }
 }
- 在application.yml中,配置redis: - spring: 
 # 设置redis
 redis:
 port: 6379
 host: localhost
- 启动,并输入:localhost:8080/put?key=name&value=zhangsan,localhost:8080/get能看到结果 
springboot集成lombok
安装lombok
- 引入jar包<dependency> 
 <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
 <optional>true</optional>
 </dependency>
- 启用lombokFile => Settings => Build, Execution, Deployment => Compiler => Annotation Processors 
 勾选 Enable annotation processing
- 安装lombok插件File => Settings => Plugins, 搜索lombok下载 代码编写# 新建UserInfo2.class类 
 @Setter @Getter // 自动生成成员变量的get和set方法
 public class UserInfo2 {
 private Long id;
 private String name;
 private String phone;
 private String address;
 }
 # 新建使用方法App.class
 public class App {
 public static void main(String[] args) {
 UserInfo2 userInfo2 = new UserInfo2();
 }
 }详细教程点击这里:lombok教程






