POJO Generator: Support creating Pojos automatically in Intellij Idea

How to automatically create Pojos in Intellij Idea? Instructions for installing POJO Generator on IntelliJ to automatically generate Entity and DTO classes based on tables in the database

Hello everyone, in previous posts, I have introduced to you a library called Lombok. This library helps us to simplify the creation of POJOs (Plain Old Java Objects) in Java.

But in fact, in many projects, we want to be able to generate Entity, DTO classes from the tables in the database because the number of tables and fields in is a lot.

If you create it manually, it will be very time consuming and also very error prone. At this point you should install POJO Generator (a great plugin for IntelliJ Idea users).

#first. Connect to the database

First, you have to connect to the database you use through IntelliJ first. Specifically, how to connect data, I already have a separate tutorial article.

You can refer here: How to connect MySQL Server in IntelliJ, it's time to quit MySQL Workbench

In this article, I will connect to the MySQL database management system as shown below. You can choose any database management system you like.

Picture 1 of POJO Generator: Support creating Pojos automatically in Intellij Idea

Fill in the connection information (you can refer to the article above for more details) => then click Apply to connect to the database.

Picture 2 of POJO Generator: Support creating Pojos automatically in Intellij Idea

This is the database I connected to. You can click on the Database tab as shown below to open it.

Picture 3 of POJO Generator: Support creating Pojos automatically in Intellij Idea

#2. Install POJO Generator Plugin

The next step is that we will install the POJO Generator utility to be able to automatically generate Entities and DTO classes during Java code.

You go to File => Settings as shown below.

Picture 4 of POJO Generator: Support creating Pojos automatically in Intellij Idea

In the Plugins tab, select Marketplace as shown below => Then enter POJO Generator in the search box and click search.

Search results, you select the plugin as shown below => then click Install to install. Finally click OK to confirm and if possible Restart IntelliJ to activate this utility.

Picture 5 of POJO Generator: Support creating Pojos automatically in Intellij Idea

#3. Generate Entity classes, DTOs based on the database table

After installing the plugin, you reopen the Database tab that you just connected in Step 1. Here you want to generate Entity class or DTO class from any table, right-click on that table.

At the bottom there will be an option to use POJO Generator. You can click and choose to generate Entities as well as DTOs corresponding to that table.

Picture 6 of POJO Generator: Support creating Pojos automatically in Intellij Idea

After choosing to generate Entity or DTO, you must choose the folder you will contain the class after it is generated. Here I usually put it in the model folder of the project. You can put it in any folder in the project.

Picture 7 of POJO Generator: Support creating Pojos automatically in Intellij Idea

After clicking OK, this is the Entity class that the utility has automatically generated for you. The fields in the database will be mapped corresponding to the fields in the generated class.

Picture 8 of POJO Generator: Support creating Pojos automatically in Intellij Idea

To generate a DTO class, you also do the same as with Entity class.

Picture 9 of POJO Generator: Support creating Pojos automatically in Intellij Idea

You can create a separate folder called dto to store DTO (Data Transfer Object) classes.

Picture 10 of POJO Generator: Support creating Pojos automatically in Intellij Idea

And this is the content of the generated DTO file corresponding to the customers table in the database.

Picture 11 of POJO Generator: Support creating Pojos automatically in Intellij Idea

As you can see, in fact, this plugin still generates setter and getter functions (making Entity and DTO classes untidy and time consuming to compile into .class files)

To overcome this drawback, you can delete all the Setter, Getter functions and with the Lombok library as follows.

Picture 12 of POJO Generator: Support creating Pojos automatically in Intellij Idea

Okay, so I've finished showing you how to install POJO Generator on IntelliJ to automatically generate Entity and DTO classes based on tables in the database.

With real projects, it is very important for programmers to have more time to focus on solving business-related problems, with the time saved, the programmer will have the opportunity improve the quality of the source code for the project to be better.

Yes, and extensions/plugins like POJO Generator, Lombok library… were born with just that purpose. Helps to minimize the programmer's time spent on procedural operations.

Hopefully this article will help Java programmers have more tools to optimize their programming time. See you in the next posts

Update 09 August 2021
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile