Idea automatically generates code artifact EasyCode, let you even more powerful

In Mybatis-based projects, we can generate business code from the database table structure through the Mybatis plug-in. Today I will introduce to you an Idea plug-in, which not only can achieve the effect of Mybatis reverse generation, but also can be used more flexibly.

Plug-in installation

There are usually two forms of Idea plug-in installation.

Method 1: Find the Plugins configuration directly in Idea, then search for "Easy Code", click "install" to install, restart Idea to complete the installation. As shown below: 

Method 2: Visit the official website of Idea plug-in: https://plugins.jetbrains.com/, then search for "Easy Code", the following results will be displayed:

Click "Install to Idea" to download and install. At this time, it will check the Idea of ​​the machine and install the plug-in. If it is already installed, the following will be displayed:

This method is suitable for scenarios where Easy Code is not found in Plugins.

Database configuration

Create a table in the database, here is an example of tb_order:

CREATE TABLE `tb_order` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `amount` int(11) NOT NULL DEFAULT '1',
  `order_no` varchar(64) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

Then add the configuration data source in Idea: 

Here choose to use Mysql database and configure.

If you have not configured the corresponding database before, you also need to download the corresponding driver.

The connection is successful, the effect is as follows: 

Reverse Generate Code

Right-click the table to generate code, select easycode, Generate Code.

Select the package path and the class to be generated on the pop-up page:

I have created a Spring Boot project here. After clicking "yes" all the way, the following packages and classes are generated:

Of course, there are also mybatis xml files in the resources directory. Some default methods of adding, deleting, modifying and checking are provided in these generated classes.

In order not to report errors, the dependency of mybatis needs to be added. The following is the overall dependency of the entire project:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <!--mybatis-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>


    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

Through the above series of operations, we can easily generate code through the plug-in, and no longer have to manually create each layer of classes every time.

As of now, the above project does not work properly because there is no place to scan the Mapper interface. At this time, you can add the @Mapper annotation to the TbOrderDao class, or you can add the @MapperScan("com.secbro2.easycode.biz.dao") annotation to the startup class for scanning.

Finally, configure the corresponding database connection in application.properties or application.yml.

Plug-in extension

If you dislike the trouble of handwriting, or want to generate other types of classes or files, you can extend or modify it in the configuration. For example, you can automatically add @Mapper to the automatically generated dao.java template class, or change entity.java to Lombok to generate it. It can be modified.

Through a series of operations of the plug-in, is writing code a very easy task? At least 90% of the workload can be saved in basic class generation and other tasks. Use it now.

Friends who like this article, please click on the picture to follow the subscription account and watch more exciting content!

Recommended reading:

Guess you like

Origin blog.csdn.net/qq_39507327/article/details/106394287