Before, many friends asked songko micro personnel project (github.com/lenve/vhr) used MyBatis reverse engineering where? In fact, the old version of micro personnel at that time did not use reverse engineering, is my own manual knock out, of course, manual knock is very large workload.
In the new version of micro, which was released this year, I reverse-engineered the code to automatically generate the entity class and DAO layer. MyBatis reverse engineering either needs to add dependencies, or need to download jar online, it is still a bit troublesome to use, so today, I introduce a free open source plug-in on IntelliJ IDEA, called EasyCode, which can easily generate corresponding codes according to the database.
EasyCode is a code generation plug-in based on IntelliJ IDEA development, support to customize arbitrary templates (Java, HTML, JS, XML); Any database-related code can be generated from custom templates; Support database type and Java type mapping configuration; Support simultaneous generation of multiple table code, a table has independent configuration information, complete personalized definition, rules set by you.
I’ve had a little taste of it myself, and it’s pretty handy.
1. Install
First go to File->Settings->Plugins and then search EasyCode as follows:
After the installation is successful, restart IDEA.
2. Create projects
I’m going to show you how to use EasyCode in Spring Boot. First of all, we create a SpringBoot project in IDEA. I won’t talk about the creation process. If you don’t know SpringBoot, you can reply to SpringBoot in my public account background, with detailed tutorials.
3. Add a data source
Since the project was developed based on Database Tools on IDEA, we first connected to the data source through Database on IDEA.
Next, configure your database connection information. If no suitable database Driver is detected, click on the lower left corner of the automatic download:
Once connected, click OK. By default, the connected database is not displayed, so we need to check it manually. For example, I want to connect to the VHR database, as follows:
4. Generate code
After completing the first three steps, the preparation is OK, and it’s time to generate the code. For example, I want to generate an entity class, a DAO layer, and a Service layer for all the code in VHR as follows:
First select all tables, then right-click and select Easy Code->Generate Code as follows:
The first time you add a database type that does not correspond to a Java type, you will be prompted to add the mapping.
Click Yes and go add your own type mapping:
After the addition is successful, we can do the code generation, which is ready:
Here, if the project is a multi-module project, you can select the Module to generate the code, then set the package to generate the code, and finally check the code template to generate from below and click OK.
In the process of code generation, if service, DAO and Entity packages are not detected under the package, the tool will prompt automatic creation. Click Yes.
The resulting code looks like this:
At this point, the code for each layer is basically generated. Most of you are probably OK with this step.
Of course, if you feel that the templates automatically generated by EasyCode are not enough or not easy to use, we can also customize the templates.
5. Customize templates
For example, I would like to add an additional method to the generated DAO layer code that fetters all data without any conditions (this method is not included in the default build template).
Go to File->Settings->Easy Code->Template Setting:
Following the tool’s own definition, we now add the following methods in dao.java:
/** * query all data **@param$! tool.firstLowerCase($! {tableInfo.name}) instance object *@returnObject list */List<$! {tableInfo.name}> getAll();Copy the code
Then add a method from mapper. XML to it. Open mapper. XML and add the following method:
<select id="getAll" resultMap="$!!! {tableInfo.name}Map">select #allSqlColumn() from $! {tableInfo.obj.parent.name}.$! tableInfo.obj.name</select>
Copy the code
When the configuration is complete, click OK.
If you then regenerate the code, you will see that there is an extra method in the generated method. Other code templates have similar definitions, which I won’t go over.
All right, guys, let’s try it.