如何去除get,set方法。@Data注解和如何使用,lombok

主要的作用提高代码的简洁

@Data

去除一些重复的get,set方法,

1.需要官网下载jar包

https://projectlombok.org/download

2运行jar包,添加到eclipse目录下

点击install/update按钮,即可安装完成

检查eclipse.ini文件最后是否添加了jar包

3然后重启一下eclipse,

4最重要的是需要在maven中引入

org.projectlombok

lombok

1.16.6

比较一下:

原来的代码

public class EnergySource {

	private String energySourceId;
	private String fuel;
	private String industrialWater;
	private String gas;
	private String naturlGas;
	private String steam;
	private String oxygenNitrogen;
	private String compressedAir;
	private String dynamicMedium;
	public String getEnergySourceId() {
		return energySourceId;
	}
	public String getFuel() {
		return fuel;
	}
	public String getIndustrialWater() {
		return industrialWater;
	}
	public String getGas() {
		return gas;
	}
	public String getNaturlGas() {
		return naturlGas;
	}
	public String getSteam() {
		return steam;
	}
	public String getOxygenNitrogen() {
		return oxygenNitrogen;
	}
	public String getCompressedAir() {
		return compressedAir;
	}
	public String getDynamicMedium() {
		return dynamicMedium;
	}
	public void setEnergySourceId(String energySourceId) {
		this.energySourceId = energySourceId;
	}
	public void setFuel(String fuel) {
		this.fuel = fuel;
	}
	public void setIndustrialWater(String industrialWater) {
		this.industrialWater = industrialWater;
	}
	public void setGas(String gas) {
		this.gas = gas;
	}
	public void setNaturlGas(String naturlGas) {
		this.naturlGas = naturlGas;
	}
	public void setSteam(String steam) {
		this.steam = steam;
	}
	public void setOxygenNitrogen(String oxygenNitrogen) {
		this.oxygenNitrogen = oxygenNitrogen;
	}
	public void setCompressedAir(String compressedAir) {
		this.compressedAir = compressedAir;
	}
	public void setDynamicMedium(String dynamicMedium) {
		this.dynamicMedium = dynamicMedium;
	}
	@Override
	public String toString() {
		return "EnergySource [energySourceId=" + energySourceId + ", fuel=" + fuel + ", industrialWater="
				+ industrialWater + ", gas=" + gas + ", naturlGas=" + naturlGas + ", steam=" + steam
				+ ", oxygenNitrogen=" + oxygenNitrogen + ", compressedAir=" + compressedAir + ", dynamicMedium="
				+ dynamicMedium + "]";
	}
	
}

加注解的代码

import lombok.Data;

@Data
public class EnergySource {

	private String energySourceId;
	private String fuel;
	private String industrialWater;
	private String gas;
	private String naturlGas;
	private String steam;
	private String oxygenNitrogen;
	private String compressedAir;
	private String dynamicMedium;
	
}

 

还有一些提供的注解:

 

        @Data   :注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法

        @Setter:注解在属性上;为属性提供 setting 方法

        @Getter:注解在属性上;为属性提供 getting 方法

        @Log4j :注解在类上;为类提供一个 属性名为log 的 log4j 日志对象

        @NoArgsConstructor:注解在类上;为类提供一个无参的构造方法

        @AllArgsConstructor:注解在类上;为类提供一个全参的构造方法

 

 

 

补充:

在idea的springboot项目中使用的话只要在pom.xml文件中加入


    org.projectlombok
    lombok
    1.16.6

你可能感兴趣的:(一些好玩的注解,Data,lombok,注解)