Lombok,热部署
作者:互联网
一.Lombok
1.概述
简化了Model层的代码的编写
以前pojo类/实体类,需要自己提供set get toString equals hashCode
Lombok通过各种注解,简化了以上操作
@Data会生成set get toString equals hashCode
@NoArgsConstructor自动生成无参构造
@AllArgsConstructor自动生成全参构造
@Accessors(chain = true)//开启链式编程
2.使用步骤
-1.在IDEA里安装插件
Settings-选Plugins-搜插件名-install
-2.修改pom.xml文件,添加lombok的jar包依赖
<!--添加jar包依赖-->
<dependencies>
<!--添加lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
-3.修改pojo类
用lombok前
package cn.tedu.pojo;
public class Car {
private Integer id;
private String name;
private String color;
private Double price;
//get set tostring
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"id=" + id +
", name='" + name + '\'' +
", color='" + color + '\'' +
", price=" + price +
'}';
}
}
用lombok后
package cn.tedu.pojo;
//使用lombok
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data//自动生成get set tostring hashcode equals
@NoArgsConstructor//自动生成无参构造
@AllArgsConstructor//自动生成全参构造
@Accessors(chain = true)//开启链式编程
public class Student {
private Integer id;
private String name;
private String sex;
}
-4.测试
package cn.tedu.test;
import cn.tedu.pojo.Car;
import cn.tedu.pojo.Student;
import org.junit.jupiter.api.Test;
public class Test1 {
//测试lombok
@Test
public void get2(){
Student s = new Student();
Student s2 = new Student(200,"rose","女");
// s.setId(100);
// s.setName("jack");
// s.setSex("男");
//对set()可以使用lombok的链式编程
s.setId(100).setName("jack").setSex("男");
System.out.println(s);
System.out.println(s.getId()+s.getName()+s.getSex());
}
//测试Car
@Test
public void get(){
Car c = new Car();
c.setName("BMW");
String s = c.getName();
System.out.println(s);
c.setId(100);//自动装箱
Integer i = c.getId() ;
System.out.println(i);
System.out.println( c.getId() );
}
}
二.热部署
1.概述
以前服务器改完代码,每次都需要重启服务器
现在只需要改完了重新build就行
2.步骤
-1添加jar包
<!--添加热部署的jar-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.5.3</version>
</dependency>
-2.开启IDEA的热部署的功能
按下组合键: ctrl + shift + alt + / 或者 ctrl + alt + a, 选中Registry, 选中自动编译, ok
-3.测试
创建RunApp启动类
package cn.tedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RunApp {
public static void main(String[] args) {
SpringApplication.run(RunApp.class);
}
}
创建StuController,接受请求处理响应
package cn.tedu.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("stu")
public class StuController {
@RequestMapping("get")
public String get(){
//热部署:改变成新的数据,也不必重启服务器了,只是build就可以啦
return "xyz";
}
}
-4测试:只要刷新,就可以获取新数据
http://localhost:8080/stu/get
三两框架整合
0.项目结构
1.概述
2.创建网页
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>测试 两个框架整合</title>
</head>
<body>
<a href="http://localhost:8080/car/get">请求服务器的数据</a>
</body>
</html>
3.创建RunApp类启动服务器
package cn.tedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//SpringBoot自动配置了包扫描:默认基于启动类所在的包
public class RunApp {
public static void main(String[] args) {
SpringApplication.run(RunApp.class);
}
}
4.创建Car类
修改pom.xml,添加lombok包的jar包
<?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">
<parent>
<artifactId>cgb2106boot03</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>day1602</artifactId>
<dependencies>
<!--导入lombok简化pojo-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
</project>
Car类
package cn.tedu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data //自动生成get set tostring...
@NoArgsConstructor //自动生成无参构造
@AllArgsConstructor//自动生成全参构造
@Accessors(chain = true)//开启链式编程
public class Car {
private String name;
private String color;
private Double price;
}
5.创建CarController类
package cn.tedu.controller;
import cn.tedu.pojo.Car;
import cn.tedu.service.CarServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("car")
public class CarController {
@Autowired //想要调用service层的代码--di
private CarServiceImpl carService;
@RequestMapping("get")
public Object get(){
return carService.get(); //把结果给浏览器返回
}
}
6.创建CarService接口
package cn.tedu.service;
import cn.tedu.pojo.Car;
//定义接口
public interface CarService {
//接口里的方法都是抽象方法,而且都是public的
Car get(); //获取汽车数据
// void add(Car c) ;//新增汽车数据
}
7.创建CarServiceImpl实现类
package cn.tedu.service;
import cn.tedu.pojo.Car;
import org.springframework.stereotype.Component;
@Component
//实现了接口后,要重写抽象方法
public class CarServiceImpl implements CarService{
public Car get(){
Car c = new Car();
//lombok的链式编程
c.setName("保时捷").setColor("红色").setPrice(641000.0);
return c;
}
}
8.测试
1.启动服务器
2.打开浏览器执行前端HTML网页,发起请求,请求服务器的数据
3.服务器收到请求后响应了准备好的数据
9.总结
标签:cn,部署,Car,public,import,Lombok,tedu,lombok 来源: https://blog.csdn.net/mashenk/article/details/120736858