• Lombok代码生成插件使用


    步骤

    1、下载Project Lombok Installer

    版本:v1.18.6

    2、执行jar进行编辑器安装

    Eclipse、Spring Tool Suite

    $ java -jar lombok-1.18.6.jar
    






    安装成功后,会在自动拷入lombok.jar。

    3、配置文件

    配置eclipse.ini或SpringToolSuite4.ini,在*.ini文件中加入:

    -javaagent:lombok.jar
    

    4、创建lombok-demo项目

    pom.xml

    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>com.codedok.lombok</groupId>
    	<artifactId>lombok-demo</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    	
    	<properties>
    		<compiler.version>1.8</compiler.version>
    		<java.source.version>1.8</java.source.version>
    		<java.target.version>1.8</java.target.version>
    		<file.encoding>UTF-8</file.encoding>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.projectlombok</groupId>
    			<artifactId>lombok</artifactId>
    			<version>1.18.6</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.projectlombok</groupId>
    			<artifactId>lombok-maven-plugin</artifactId>
    			<version>1.18.20.0</version>
    			<scope>provided</scope>
    		</dependency>
    	</dependencies>
    	
    	<build>
            <plugins>
                <!-- Java Compiler -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <compilerVersion>${compiler.version}</compilerVersion>
                        <source>${java.source.version}</source>
                        <target>${java.target.version}</target>
                        <encoding>${file.encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <configuration>
                        <charset>${file.encoding}</charset>
                        <encoding>${file.encoding}</encoding>
                    </configuration>
                    <executions>
                        <execution>
                            <id>attach-javadocs</id>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin> 
                <plugin>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok-maven-plugin</artifactId>
                    <version>1.18.20.0</version>
                    <configuration>
                        <encoding>${file.encoding}</encoding>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>delombok</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <profiles>
            <profile>
                <id>lombok-needs-tools-jar</id>
                <activation>
                    <file>
                        <exists>${java.home}/../lib/tools.jar</exists>
                    </file>
                </activation>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-maven-plugin</artifactId>
                            <version>1.18.20.0</version>
                            <dependencies>
                                <dependency>
                                    <groupId>sun.jdk</groupId>
                                    <artifactId>tools</artifactId>
                                    <version>1.6</version>
                                    <scope>system</scope>
                                    <systemPath>${java.home}/../lib/tools.jar</systemPath>
                                </dependency>
                            </dependencies>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    	
    </project>
    

    User.java

    package com.codedot.lombok.model;
    
    import java.io.Serializable;
    import java.util.Date;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User implements Serializable, Cloneable {
    
      private static final long serialVersionUID = 1L;
      
      private Long id;
      
      private String username;
      
      private String email;
      
      private Date birthday;
      
      @Override
      protected Object clone() {
        User user = null;
        try {
          user = (User) super.clone();
        } catch (CloneNotSupportedException e) {
          return null;
        }
        user.setBirthday(this.getBirthday());
        return user;
      }
      
      
    }
    
    

    加入Lombok注解

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    
    @Data(自动生成Get、Set方法,
    以及equals()、canEqual()、hashCode(),toString()方法)
    
    
    

    LombokDemoTest.java

    package com.codedot.lombok.test;
    
    import java.util.Date;
    
    import com.codedot.lombok.model.User;
    
    public class LombokDemoTest {
      
      public static void main(String[] args) {
        User user = new User();
        user.setId(9982313891523L);
        user.setEmail("jason@gmail.com");
        user.setUsername("jason");
        user.setBirthday(new Date(2018, 11, 25));
        
        System.out.println(user);
        
      }
    
    }
    
    

    运行结果:

    User(id=9982313891523, username=jason, email=jason@gmail.com, birthday=Wed Dec 25 00:00:00 CST 3918)
    
    
    每天都要有所进步!
  • 相关阅读:
    转:SQL Server 2005 Express附加数据库为“只读”的解决方法!
    通过WPF模拟交通红绿灯(图文教程)
    手把手教你怎样把文件保存到Oracle数据库
    已删除
    JavaScript精炼类(class)、构造函数(constructor)、原型(prototype)
    Ext:RowLayout和ColumnLayout连用必须加panel的问题
    Ext:前台js往gridpanel动态添加记录
    "int i=1" "int i=new int() "和“String str = "a";” “String str = new String("a")”区别以及c#值类型和引用类型
    未能加载文件或程序集“Model Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。
    hibernate:inverse、cascade,一对多、多对多详解
  • 原文地址:https://www.cnblogs.com/weijunqiang/p/15659267.html
Copyright © 2020-2023  润新知