Use of SpringBoot Lombok

1 Lombok

Steps for usage

  1. The first is to configure the following in the pom.xml file
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>	
    
  2. Download the lombok plug-in in idea, the download steps are as follows
    • Click file—>settings
    image.png
  • Click plugins---->Marketplace
    image.png
    • Enter lombok in the input box
    image.png
    as shown in the figure below • Click Enter to regret the content in the red box in the figure below is displayed below
    image.png
    • Finally click Install to install

3. When the above operation is completed, the following is the specific use

  1. Use in entity classes
    • @Data is equivalent to the getter and setter methods in the previous entity classes, that is to say, when the annotation is configured in the entity class, it can be used directly without writing the getter and setter methods
    public Pet(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
  • @ToString is equivalent to the toString method in the entity class
    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                '}';
    }
  • @AllArgsConstructor is equivalent to the full-parameter constructor in the entity class
    public Car(String brand, Integer price) {
        this.brand = brand;
        this.price = price;
    }
  • @NoArgsConstructor is equivalent to the parameterless constructor in the entity class
    public Car() {
        
    }
  1. Simplify log development
    . Annotate configuration at the controller layer to achieve log printing.
    Configuration method:
    • First configure the @Slf4j annotation in the controller layer class
    • Then write log.info in the method equipped with @RestController ("write specific input content")
    • The execution code is as follows
import org.springframework.web.bind.annotation.RestController;

/**
 * @RestController专门用来配置控制器
 * 该注解是@Controller和@ResponseBody的结合体
 */
@Slf4j
@RestController
public class HelloController {


    @RequestMapping("/hello")//该注解直接将方法中的一段话返回给浏览器
    public String handle01() {
        log.info("你好");
        return "Hello, Spring Boot 2! 你好";
    }

}

Guess you like

Origin blog.csdn.net/weixin_45566730/article/details/113839298