MyBatis code generator - based on Maven plug-in

Keywords: Mybatis MySQL xml Java

MBG is the official code generator of MyBatis. MBG can automatically generate entity classes, Mapper interfaces and corresponding XML files, which can reduce the workload of developers to a certain extent. This paper introduces how to use MBG Maven plug-in.


1, In pom files, dependency packages and plug-ins are introduced.

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<scope>runtime</scope>
	<version>${mysql.connector.version}</version>
</dependency>		
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
    <version>${spring.admin.version}</version>
</dependency>
<plugin>
	<!--  MBG plug-in unit  -->
	<groupId>org.mybatis.generator</groupId>
	<artifactId>mybatis-generator-maven-plugin</artifactId>
	<version>1.3.5</version>
	<configuration>
		<!-- Define profile -->
		<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
		<!-- Output details -->
		<overwrite>true</overwrite>
		<!-- Overwrite build file -->
		<verbose>true</verbose>
	</configuration>
	<dependencies>
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.5</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.16</version>
		</dependency>
	</dependencies>
	<executions>
		<execution>
			<id>MyBatis Generate</id>
			<goals>
				<goal>generate</goal>
			</goals>
		</execution>
	</executions>
</plugin>

 

2, Edit mybatis-generator.xml Code generator configuration

The file configuration here is 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>

    <!-- MyBatis Code generator  -->
    <context id="DBdemo" targetRuntime="MyBatis3">

        <!-- Remove automatically generated comments -->
        <commentGenerator>
            <property name="suppressAllComments" value="false" />
        </commentGenerator>

        <!-- Database connection address, account number, password -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/demoDB?characterEncoding=utf8"
                        userId="root" password="root">
        </jdbcConnection>

        <!-- generate model Class storage location -->
        <javaModelGenerator targetPackage="com.zhirong.model" targetProject="src/main/java">
            <!-- Add package name or not scheme name -->
            <property name="enableSubPackages" value="true" />
            <!-- Spaces before and after values returned from the database are cleaned up -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- generate mapper Map file storage location -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- generate dao Class storage location of -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhirong.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- Generate corresponding table and class name -->
        <table tableName="t_goods_info" domainObjectName="Goods"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"
        />
    </context>
</generatorConfiguration>

 

3, maven runs the command.

 

4, Check the generation effect

Finally, we see that the build is successful.

Posted by WormTongue on Tue, 26 May 2020 10:01:20 -0700