Mybatis automatically generates entity classes

Keywords: Maven

Mybatis automatically generates entity classes

    today, when I was working, I encountered the need to establish entity. Then I almost fainted when I looked at the database table. There were 78 fields. At this time, if I write one by one, the efficiency would be too low.

  I searched on the Internet. Can I leave this simple but cumbersome process to the program (lazy first productivity), and I really found mybatis generator.

   there are many online searches, which are particularly formal methods, such as github up and down files, executing commands under the console and so on. I follow the tutorial and make sure that either the environment is wrong or the configuration is wrong. Upset.

  however, looking back, Mybatis is a lightweight framework. Its most important thing is an xml file. It's not difficult, so I'm ready to explore by myself. Although it may be a wild way, it really meets the requirements in the end, so I share this fool's tutorial.

1) Create a maven project

   new - > Project - > select mavne project. Don't choose any template. The purpose is to create an independent pom.xml.

  the reason for the new project here is to not affect the original project. The function of plug-in is like this, plug and play.

  why do you want to do it in the new maven project? The supervisor told me not to introduce anything unimportant to the project, such as plug-ins, which will make the project complicated and bloated. The project can focus on the business.

2) Introducing plug-ins

  add the following plug-in information to pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    at the beginning, I directly introduced plugins. I found it impossible. It must be wrapped in the build and plugins tags. mybatis tags are in order. Monkeys can't move corn. They learn and forget.

3) Write generatorConfig.xml

  note: this file must be placed in the src/main/resources directory.

<?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>
    <!-- (1)Database driven:Select the database driver package on your local hard disk-->
    <classPathEntry  location="D:\Environment\apache-maven-3.8.2\maven-repo\org\postgresql\postgresql\9.4.1212\postgresql-9.4.1212.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- Remove automatically generated comments true: Yes: false:no -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--(2)Database link URL,User name and password -->
        <jdbcConnection driverClass="org.postgresql.Driver"
                        connectionURL="jdbc:postgresql://10.9.42.203:5432/ckgldb?db_compatibility=oracle"
                        userId="root"
                        password="root@test">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- Package name and location of the build model-->
        <javaModelGenerator targetPackage="com.server.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- Package name and location of the generated mapping file-->
        <sqlMapGenerator targetPackage="mybatis" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- generate mapper position-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.server.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- (3)Table to generate tableName Is the table name in the database domainObjectName Is the name of the entity class (remember to capitalize)-->
        <table tableName="wms_core_label" domainObjectName="WmsCoreLabelEntity"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

    </context>
</generatorConfiguration>

  the three places to be changed are marked with notes.

   annotation (1): database driven. It is the jar package of relevant database drivers placed in your maven warehouse. Different databases use different drivers. Our project uses pgsql, so it is postgresql-9.4.1212.jar; MySQL can find mysql-connector-java-8.0.16.jar in the warehouse.

   annotation (2): Database link. The familiar old three. The Database connection pool, the Database provided by IDEA, and the third-party Database software (DBeaver, Navicat) all need to be configured. You really can't ask the project director.

   annotation (3): target table. To generate the domainObjectName entity class for the tableName table, note that the class name is capitalized.

4) Maven run

  click Maven in the sidebar of IDEA and run "mybatis generator: generate".

    this time I groped and tried by myself, which made me more firm. The key to mybatis is xml files.

   the key problem of mybatis is the xml problem of the core configuration file, that is, it is very simple to use mybatis, that is, to write and understand the xml file.

Posted by kanetan on Mon, 22 Nov 2021 05:40:04 -0800