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


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

        <!--  Plug ins used in the construction process -- >
            <!--  For specific plug-ins, the operation of reverse engineering appears in the form of plug-ins in the construction process -- >
                        <!--  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 -- >

Add the basic package required for mybatis


Create generatorconfig XML file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-// MyBatis Generator Configuration 1.0//EN"

    <!--  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 -- >
            <property name="suppressDate" value="true"/>
            <!--  Whether to remove automatically generated comments true: Yes: false: no -- >
            <property name="suppressAllComments" value="true"/>

        <!-- JDBC database connection. The following are JDBC drivers above MySQL version 8 -- >
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        userId="root" password="123456"></jdbcConnection>

        <!-- Not required, type processor, conversion control between database type and Java type -- >
            <!--  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"/>

        <!--  Targetpackage: the package of the generated entity class -- >
        <!--  Targetproject: location of generated entity class -- >
        <javaModelGenerator targetPackage="" 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"/>

        <!--  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"/>

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


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, […]