POJO Generator: Support creating Pojos automatically in Intellij Idea
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.
Fill in the connection information (you can refer to the article above for more details) => then click Apply to connect to the database.
This is the database I connected to. You can click on the Database tab as shown below to open it.
#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.
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.
#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.
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.
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.
To generate a DTO class, you also do the same as with Entity class.
You can create a separate folder called dto to store DTO (Data Transfer Object) classes.
And this is the content of the generated DTO file corresponding to the customers table in the database.
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.
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
You should read it
- How to install Python plugin to be able to execute Python programming on IntelliJ
- Advice on choosing suitable generators for families
- 4 best QR code generator tools for PDF
- Generator in Python
- Should you use a password generator to protect your online accounts?
- Instructions on how to connect to MySQL Server in IntelliJ
- How to generate QR codes with Me QR Generator on phones and computers
- How to use AI Background Generator to create images based on keywords
- self-maintenance steps, generator maintenance
- Instructions for using Chrome's built-in password generator
- Instructions on how to create QR codes on Android with QR Code Generator
- Instructions to turn off the creation of the Facebook security code on the phone