Simple use of Lombok in Mybatis basic learning

preface: Hello, guys, I'm running snail rz. Of course, yo...
10.1 Lombok basic concept
10.2 use steps
10.3 important notes in Lombok
10.4 use of annotations in Lombok
10.5 Lombok's advantages and disadvantages
10.6 Usage Summary

preface:

Hello, guys, I'm running snail rz. Of course, you can call me snail Jun. I'm a rookie who has studied Java for more than half a year. At the same time, I also have a great dream, that is, to become an excellent Java Architect one day.

This Mybatis basic learning series is used to record the whole process of learning the basic knowledge of Mybatis framework (this series is written with reference to the latest Mybatis tutorial of crazy God in station B. because it was sorted out before, but it was not released at that time, there may be errors in some places. I hope you can correct them in time!)

After that, I will try my best to update this series at the rate of two days and one hour. For those who haven't learned the Mybatis3 framework, please refer to my blog; Of course, the little friends who have studied can also review the basics with me. Finally, I hope I can make progress with you! Come on! Boys!

Special note: if you are interested in Mybatis basic learning series, you can read the previous blogs of this series:
Part I: First knowledge of Mybatis basic learning
Part II: The first Mybatis program for basic learning of Mybatis
Part III: Addition, deletion and modification of CRUD in Mybatis basic learning
Part IV: Mybatis basic learning omnipotent Map and fuzzy query
Part V: Configuration analysis of Mybatis basic learning (Part I)
Part VI: Configuration analysis of Mybatis basic learning (Part 2)
Part VII: Mybatis basic learning uses ResultMap to solve the inconsistency of field names
Part VIII: Simple use of Mybatis basic learning log factory
Part IX: Simple use of data paging in Mybatis basic learning
Part 10: Development of usage annotation for Mybatis basic learning

Today we come to the tenth stop of Mybatis basic learning: the simple use of Lombok. No more nonsense. Let's start today's study.

10. Simple use of Lombok

10.1 Lombok basic concept

10.1.1 what is Lombok?

Lombok is a java development plug-in, which enables Java developers to eliminate tedious and cumbersome code in business engineering through some annotations defined by Lombok, especially for simple java model objects (POJO s).

After using Lombok plug-in in the development environment, Java developers can save a lot of time for repeated construction of methods such as hasCode and equals and accessor and toString of various business object models.

For these methods, it can automatically help us generate these methods during compiling the source code without reducing the program performance as reflection

Official website: https://projectlombok.org/

10.1.2 core content

  • Java library: Java library
  • Plug ins: plug-ins
  • Build tools: build tools
  • with one annotation your class: you only need to add annotations in front of the class

10.2 use steps

10.2.1 installing Lombok plug-in in IDEA

  • Find the Plugins option in the settings under the File directory, search for the Lombok plug-in and download it

10.2.2 import the jar package of Lombok in the project

  • Import the resource dependency in the pom.xml file of the corresponding project
<dependencies> <!-- lombok Resource dependency --> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency> </dependencies>

10.3 important notes in Lombok

Key notes:

  • @Getter and @Setter: getter and setter methods in Java entity classes
  • @FieldNameConstants: fields, properties, and constants
  • @ToString: convert to string method
  • @EqualsAndHashCode: equality and hash code
  • @AllArgsConstructor: parametric construction method
  • @RequiredArgsConstructor and @NoArgsConstructor: parameterless construction method
  • @Data: data (most used)

Other notes can be understood:

  • @Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog, @Flogger,
  • @CustomLog
  • @Builder
  • @SuperBuilder
  • @Singular
  • @Delegate
  • @Value
  • @Accessors
  • @Wither
  • @With
  • @SneakyThrows

10.4 use of annotations in Lombok

10.4.1 use @ Data annotation

1. Write Java code of User entity class
package com.kuang.pojo; import lombok.Data; // Use @ Data annotation @Data // user entity class public class User { private int id; // number private String name; // user name private String password; // password }
2. After using @ Data annotation

Results: parameterless construction methods, get and set methods, toString methods, hashcode methods and equals methods were automatically generated

10.4.2 use @ AllArgsConstructor annotation

1. Write Java code of User entity class
package com.kuang.pojo; import lombok.AllArgsConstructor; import lombok.Data; // Use @ Data annotation to introduce parameterless construction, get, set, toString and other methods @Data // Use @ AllArgsConstructor annotation @AllArgsConstructor // User entity class public class User { private int id; // number private String name; // user name private String password; // password }
2. After using @ AllArgsConstructor annotation

Result: after adding @ AllArgsConstructor annotation, the parameterless structure disappears!

3. After deleting the @ AllArgsConstructor annotation

Result: after deleting the @ AllArgsConstructor annotation, the parameter structure disappeared again

4. Usage Summary

Summary: therefore, if the parameter construction method is defined in the displayed, the parameter free construction method needs to be assigned manually

10.4.3 use @ NoArgsConstructor annotation

1. Write Java code of User entity class
package com.kuang.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; // User entity class @Data // Introduce parameterless construction, get, set, toString and other methods @AllArgsConstructor // The parametric construction method is introduced @NoArgsConstructor // Introduction of parameterless construction method public class User { private int id; // number private String name; // user name private String password; // password }
2. After annotation with and without parameters

Results: after using @ AllArgsConstructor (parametric construction) annotation and @ NoArgsConstructor (nonparametric construction) annotation at the same time, both parametric construction methods and nonparametric construction methods appear!

###10.4.4 use @ ToString annotation

1. Write Java code of User entity class
package com.kuang.pojo; import lombok.ToString; // Use @ ToString annotation @ToString // User entity class public class User { private int id; // number private String name; // user name private String password; // password }
2. After using @ ToString annotation

Result: toString method is introduced!

10.4.5 use @ EqualsAndHashCode annotation

1. Write Java code of User entity class
package com.kuang.pojo; import lombok.EqualsAndHashCode; // Use @ EqualsAndHashCode annotation @EqualsAndHashCode public class User { private int id; // number private String name; // user name private String password; // password }
2. After using @ EqualsAndHashCode annotation

Results: equals method and hashCode method are introduced!

10.4.6 use @ Getter annotation

1. Analyze @ Getter source code
package lombok; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; // Use @ Target annotation, and the scope is fields and types (there are multiple values, surrounded by {} and separated by ",") @Target() // Use the @ Retention annotation to set the Retention policy to the source file @Retention(RetentionPolicy.SOURCE) // @Getter annotation public @interface Getter { AccessLevel value() default AccessLevel.PUBLIC; Getter.AnyAnnotation[] onMethod() default {}; boolean lazy() default false; /** * Use @ deprecated to indicate that the method is deprecated */ @Deprecated @Retention(RetentionPolicy.SOURCE) @Target({}) public @interface AnyAnnotation { } }
2. Write Java code of User entity class 2-1 direct action on class
package com.kuang.pojo; import lombok.Getter;] // Use @ Getter annotation: act directly on class @Getter // User entity class public class User { private int id; // number private String name; // user name private String password; // password }
2-2 action on field
package com.kuang.pojo; import lombok.Getter; // User entity class public class User { // Using @ Getter annotation: acting on fields @Getter private int id; // number @Getter private String name; // user name @Getter private String password; // password }
3. After using @ Getter annotation

Result: the corresponding get method is generated!

10.5 Lombok's advantages and disadvantages

advantage:

  • The constructor, getter/setter, equals, hashCode, toString and other methods are automatically generated in the form of annotations, which improves the development efficiency
  • Make the code concise and don't pay too much attention to the corresponding methods
  • When modifying properties, it also simplifies the getter/setter methods generated by maintaining these properties

Disadvantages:

  • Overloading of multiple parameter constructors is not supported
  • Although it saves the trouble of manually creating getter/setter methods, it also greatly reduces the readability and integrity of the source code and reduces the comfort of reading the source code

10.6 Usage Summary

Lombok has many advantages, but Lombok is more similar to an IDE plug-in, and the project also needs to rely on the corresponding jar package.

Lombok relies on the jar package because its annotations are used during compilation. Why is it similar to a plug-in?

When in use, eclipse or IntelliJ IDEA need to install corresponding plug-ins. During compiler compilation, bytecode generation is changed through AST (abstract syntax tree). In a disguised way, it is changing Java syntax.

Unlike Spring's dependency injection or mybatis's ORM (object relational mapping), it is a runtime feature, but a compile time feature. The most unpleasant thing is the dependence on plug-ins. Although Lombok only saves some trouble of manually generating code, the IDE has shortcut keys to help generate getter/setter and other methods, which is also very convenient.

Well, that's the end of today's simple use of Lombok. You are welcome to study and discuss actively. If you like, you can pay attention to Mr. snail. By the way, you can click three times. I'll see you next time. Bye!

Reference video link: [crazy God says Java] the latest complete tutorial of Mybatis, the IDEA version is easy to understand

5 November 2021, 15:01 | Views: 5864

Add new comment

For adding a comment, please log in
or create account

0 comments