Spring 5.0 Projects
上QQ阅读APP看书,第一时间看更新

Defining the model classes

Now, let's create Java classes to model the data in the database and also the data coming from the World Bank API. Our approach is simple. We will have one Java class for each table in our database and the columns of the database will become the properties of the Java class. 

In the generated application, the java folder is missing under the main directory. We will manually create the java folder and package the  com.nilangpatel.worldgdp, which will be the root package for the application. Let's go ahead and implement the approach we decided on. But before that, let's see an interesting project called Project Lombok

Project Lombok provides annotations for generating your getters, setters, default, and overloaded constructors, and other boilerplate code. More details on how to integrate with your IDE can be found on their project website (https://projectlombok.org/).

We need to update our pom.xml to include a dependency on Project Lombok. The following are the parts of pom.xml you need to copy and add to relevant locations in the XML:

<properties>
<java.version>1.8</java.version>
<lombok.version>1.16.18</lombok.version>
</properties>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>${lombok.version}</version>
</dependency>

All the model classes that we are going to create next belong to the com.nilangpatel.worldgdp.model package. The model class to represent Country data is given in the following code:

@Data
@Setter
@Getter
public class Country {
private String code;
private String name;
private String continent;
private String region;
private Double surfaceArea;
private Short indepYear;
private Long population;
private Double lifeExpectancy;
private Double gnp;
private String localName;
private String governmentForm;
private String headOfState;
private City capital;
private String code2;
}

The City class is not created yet, let's go ahead and create it as follows: 

@Data
@Setter
@Getter
public class City {
private Long id;
private String name;
private Country country;
private String district;
private Long population;
}

Next is to model the CountryLanguage class, which is the language spoken in a country, as follows:

@Data
@Setter
@Getter
public class CountryLanguage {
private Country country;
private String language;
private String isOfficial;
private Double percentage;
}

We also need a model class to map the GDP information obtained from the World Bank API. Let's go ahead and create a CountryGDP class as shown in the following code:

@Data
@Setter
@Getter
public class CountryGDP {
private Short year;
private Double value;
}

At this moment, everything works perfectly fine. But when you start calling getter and setter of these model classes into some other class, you may get a compilation error. This is because we need to do one more step to configure Lombok. After you defined the Maven dependency, you will see the JAR reference from IDE. Just right-click on it and select the Run As | Java Application option. Alternatively, you can execute the following command from terminal at the location where the Lombok JAR file is kept, as follows:

java -jar lombok-1.16.18.jar 

Here, lombok-1.16.18.jar is the name of JAR file. You will see a separate window pop up as follows:

Select the location of your IDE by clicking on the Specify location... button. Once selected, click on the Install / Update button to install it. You will get a success message. Just restart the IDE and rebuild the project and you will see that just by defining @Setter and @Getter, the actual setters and getters are available to other classes. You are no longer required to add them explicitly.