Mybatis generate uses Maven plug-in to automatically generate entity classes

Time:2022-5-13

There are two ways to automatically generate entity classes. One is to use Maven plug-in to generate entity classes, and the other is to use generatoruitl Run generatorconfig. Java class Generate entity classes from XML files. It can also generate xxxexample, xxmapper and other files, and directly generate database use files and mapper, which greatly reduces the development time.

It is mainly divided into three steps,

  • Add Maven plug-in to POM file
  • Create generatorconfig XML file and link to the local database address,
  • Open mybatis generator in Maven on the right side of the idea and click mybatis generator: generate to generate the corresponding entity, Dao and mapper file address

Add plug-ins to POM

This is to add Maven plug-in

<build>
        <!--  Plug ins used in the construction process -- >
        <plugins>
            <!--  For specific plug-ins, the operation of reverse engineering appears in the form of plug-ins in the construction process -- >
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>
                        <!--  Here is the configuration generatorconfig XML path. If it is left blank, it means that generatorconfig. XML is found in the resources directory by default XML file -- >
                    </configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.21</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Add the basic package required for mybatis

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

Create generatorconfig XML file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!--  Context is the main configuration information of reverse engineering -- >
    <!--  ID: configuration name -- >
    <!--  Targetruntime: sets the mybatis version of the generated file -- >

    <context id="default" targetRuntime="MyBatis3">

        <!--  Optional, designed to control comments when creating classes -- >
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!--  Whether to remove automatically generated comments true: Yes: false: no -- >
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- JDBC database connection. The following are JDBC drivers above MySQL version 8 -- >
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/db_1?serverTimezone=UTC"
                        userId="root" password="123456"></jdbcConnection>

        <!-- Not required, type processor, conversion control between database type and Java type -- >
        <javaTypeResolver>
            <!--  By default, the decimal and bigint in the database in Java correspond to the BigDecimal class in SQL -- >
            <!--  Not double and long type -- >
            <!--  Use common basic types instead of reference types under SQL package -- >
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--  Targetpackage: the package of the generated entity class -- >
        <!--  Targetproject: location of generated entity class -- >
        <javaModelGenerator targetPackage="com.felixwang.site.generator.entity" targetProject="src/main/java">
            <!--  Allow sub package -- >
            <property name="enableSubPackages" value="false"/>
            <!--  Add constructor to modal -- >
            <property name="constructorBased" value="true"/>
            <!--  Whether to clear the blank characters on the left and right sides of the string queried from the database -- >
            <property name="trimStrings" value="true"/>
            <!--  Establish whether the modal object cannot be changed, that is, the generated modal object will not have a setter method, but only a construction method -- >
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--  Targetpackage and targetproject: the package and location of the generated mapper file -- >
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <!--  For a configuration of the database, whether to use schema as the package name -- >
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!--  Targetpackage and targetproject: package and location of generated interface file -- >
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.felixwang.site.generator.dao" targetProject="src/main/java">
            <!--  For a configuration of Oracle database, whether to use schema as package name -- >
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!--  The table to be mapped and the corresponding entity class name -- >
        <table tableName="work" domainObjectName="Work"></table>

    </context>
</generatorConfiguration>

The above is my personal configuration. The package name and database address need to be changed to personal

Generate entity class

In the idea, click View – > tool windows – > maven, and the project structure will be displayed on the right side of the project
Mybatis generate uses Maven plug-in to automatically generate entity classes
Then the corresponding entity class and mapper file will be automatically generated. The following is the corresponding entity class I automatically generate,
Mybatis generate uses Maven plug-in to automatically generate entity classes
You can add, delete, change and query directly by writing Java class direct area. The above is shared today. The first part is realized through JPA, but JPA is not suitable for complex joint query. The automatically generated mapper file can write SQL by itself, which has achieved the purpose of dynamic query of complex SQL

Recommended Today

How to scan and repair Linux disk errors

In this article, you will learn how to use fsck and XFS_ The repair command fixes Linux disk errors.What is fsck?Fsck (file system consistency check) is a tool used to check the file system consistency of Linux operating system.List Linux disk partitions and types:First, you need to determine the disk partitions of the Linux system, […]