inotgo.com

Step 1 : pojo My troubles   
Step 2 : lombok   
Step 3 : Screenshot   
Step 4 : So how to use it   

During the development of the project , There will be a lot of pojo. pojo Also called javabean,bean,entity wait , It's all him .
pojo There will be a lot of setter and getter , toString, hashcode, equals wait

Everyone pojo Write , Added attribute to write , Reduce the number of attributes to write , still ... Very troublesome .
public class Hero { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Hero [id=" + id + ", name=" + name + "]"; } public Hero(int id, String name) { super(); this.id = id; this.name = name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Hero other = (Hero) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
To be lazy , We can use lombok. After you use it, you will see the following code , Just add notes
import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.ToString; @Data public class Hero { private int id; private String name; }
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
public class Hero {
	private int id;
	private String name;
}
To prove that I didn't write setter ,getter Can also access , Made a screenshot , You can see IDE The existing methods will pop up automatically , Promise , setter, getter, toString Something , It's all there -_-
 Screenshot
In order to use , You need to install plug-ins to ... Next, we will explain how to eclipse and idea Use in .


The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .


Q & A area    
2021-01-12 Actual development cannot use Lombok
Clouds and sky

1.JDK Version problem When I want to put the existing project JDK From Java 8 Upgrade to Java 11 Time , I find Lombok Can't work properly . So I had to put all Lombok Annotations are removed from the project source code , And use IDE The built-in function generates getter/setter,equals,hashCode,toString And constructors , You can also use Delombok Tools complete this process . But it will eventually consume a lot of your time . 2. Use... Under duress When you use... In your source code Lombok, It happens that your code is used by others , So people who depend on your code , Must also be installed Lombok plug-in unit ( Whether they like it or not ), It also takes time to understand Lombok Use of annotations , If you don't do that , The code will not work properly . Used Lombok Then , I found it a very rogue behavior . 3. Poor readability Lombok Hidden JavaBean The details of the package , If you use @AllArgsConstructor annotation , It will provide a giant constructor , Let the outside world have the opportunity to modify all the properties in the class when initializing the object . first , This is extremely unsafe , Because we don't want to modify a family attribute in the class ; in addition , If there are dozens of attributes in a class , There will be a constructor with dozens of parameters Lombok Inject into the class , This is irrational behavior ; secondly , The order of constructor parameters is completely determined by Lombok Controlled by , We can't control , Only when you need debugging do you find a strange “ cockroach ” Waiting for you ; last , Before running the code , All JavaBean You can only imagine what they look like , You can't see . 4. Code coupling increases When you use Lombok After writing the code of a module , Other codes that depend on this module need to be introduced Lombok Rely on , At the same time, it also needs to be in IDE Install Lombok Plug in for . Although Lombok The dependency package is not large , But just because one place used Lombok, All other dependent parties must be forced to join Lombok of Jar Bag , This is an intrusive coupling , If you meet again JDK Version problem , This will be a disaster . 5. the loss outweighs the gain use Lombok, I feel very happy for a moment , But it contaminates your code , Destroyed Java Code integrity , Readability and security , At the same time, it also increases the technical debt of the team , This is a way of doing more harm than good , An operation that is not worth the loss . If you really want to make your code more refined , At the same time, it takes into account readability and coding efficiency , You might as well use mainstream Scala or Kotlin This is based on JVM Your language .




1 One answer

iweb_learn
Answer time :2021-11-08
javabean You don't need to subdivide DTO VO Really The construction method can also be manually overridden And if you want to use mybatisplus Must use lombok You can't deny Or combine with the company's development specifications



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2020-11-04 eclipse Can automatically complete get and set How
Fire Lord

After the attribute is written, press shift+alt+s, You can choose generate getters and setters







The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2020-10-02 The needle doesn't poke , Ease




Please... Before asking questions land
The question has been submitted successfully , Auditing . Please My question Check the question record at , thank you
about JAVA application -lombok- Introduction Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot