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.

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

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

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

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

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

#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.

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

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.

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

#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.

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

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.

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

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.

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

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

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

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

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

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

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

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.

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

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

3.8 ★ | 46 Vote