Generate Code Step Automation Mybatis Generator-

Disclaimer: This article is a blogger original article, welcome to reprint. https://blog.csdn.net/guo_xl/article/details/86004068

step

  1. Write an XML configuration file generatorConfig.xml
<?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 id="MysqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <commentGenerator>
            <property name="suppressDate" value="true"/>
        </commentGenerator>
      <!--配置数据库信息-->
        <jdbcConnection driverClass="org.mariadb.jdbc.Driver"
                        connectionURL="jdbc:mariadb://localhost:3306/mydb"
                        userId="root"
                        password="devuser">
        </jdbcConnection>
        <!--根据table生成的实体类-->
        <javaModelGenerator targetPackage="rechard.learn.mybatis.entities" targetProject="src/main/java">
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
       <!--mybatis的xml-->
        <sqlMapGenerator targetPackage="rechard.learn.mybatis.xml"  targetProject="src/main/resources"/>
       <!--mybatis的xml对应的接口-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="rechard.learn.mybatis.mappers"  targetProject="src/main/java"/>
      <!--配置的表-->
        <table tableName="TEST">
            <generatedKey column="id" sqlStatement="Mysql"/>
        </table>
    </context>
</generatorConfiguration>

More information can refer to the official configuration xml
http://www.mybatis.org/generator/configreference/xmlconfig.html

  1. TEST build a SQL table
drop table  if exists `TEST`;
CREATE TABLE `TEST` (
  `TESTKY` int(11) NOT NUlL PRIMARY KEY,
  `NAME` varchar(200) not null default '',
  `UPDATEDTTM` timestamp NOT NULL DEFAULT current_timestamp(), 
  `UPDATEUSER` varchar(32) COLLATE utf8_bin NOT NULL,
  `VERSIONSTAMP` smallint(6) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=DYNAMIC;
  1. How to generate
    the following ways
    • command line
    • ant
    • maven
    • java code

The following command line and focuses maven

  1. way command line
    to open a command terminal, enter the following
cd E:\mvn\repository\org\mybatis\generator\mybatis-generator-core\1.3.5
java -jar mybatis-generator-core-1.3.5.jar -configfile D:\code\xxx-mybatis\src\main\resources\generatorConfig.xml

Report

Caused by: java.lang.ClassNotFoundException: org.mariadb.jdbc.Driver
        at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
        at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244)
        at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230)

That is org.mariadb.jdbc.Drivernot found, you need to configure org.mariadb.jdbc.Driverthe package to the classpath, as follows

java -jar mybatis-generator-core-1.3.5.jar 
-classpath E:\mvn\repository\org\mariadb\jdbc\mariadb-java-client\2.3.0\mariadb-java-client-2.3.0.jar
-configfile D:\code\xxx-mybatis\src\main\resources\generatorConfig.xml
  1. maven way recommend this
    pom.xml
<build>
		<plugins>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.5</version>
				<dependencies>
					<dependency>
						<groupId>org.mariadb.jdbc</groupId>
						<artifactId>mariadb-java-client</artifactId>
						<version>2.3.0</version>
					</dependency>
				</dependencies>
			</plugin>			
		</plugins>
	</build>

maven command

mvn -Dmybatis.generator.overwrite=true  -Dmybatis.generator.configurationFile=.\src\main\resources\generatorConfig.xml mybatis-generator:generate

Description:

  • mybatis.generator.configurationFile specify the configuration file
  • mybatis.generator.overwrite to overwrite the existing source code

These configurations can watch the official reference documentation in the Parameter Reference

  1. Some of the more important configuration profiles of generatorConfig.xml
The generated class name
  • The default name of the class students database table name, domainObjectNamemay be renamed to the class name of the desired, for example RPTDEFthe same name asRptDef.java
 <table tableName="RPTDEF" domainObjectName="RptDef">
</table>
Generated class field type
  • The development model in the field type, such as table if the field is boolean type, generated by default
    private byte[] isdelete, use <columnOverride column="ISDELETE" javaType="boolean" />the generatedprivate boolean isdelete
 <table tableName="RPTDEF" domainObjectName="RptDef">        
            <columnOverride column="ISDELETE" javaType="boolean" />
</table>
Generated primary key

Generating a primary key in the generatedKeyinside,

  • sqlStatementSpecify what kind of database use
  • identity = "true", if set to true, as
<table tableName="RPTDEF" domainObjectName="RptDef">   
    <generatedKey column="RPTDEFKY" sqlStatement="Mysql" identity="true" />
    <columnOverride column="ISDELETE" javaType="boolean" />
</table>

After the return to the primary key generated RptDef.rptdefky. This is useful, for example, want to get the generated key in the code.

RPTInstMapper.insertSelective(inst);
int rptInstKy = inst.getRptinstky();

7. Re-generate code coverage issues
post-build If you do not want to overwrite the existing java, etc., will be mybatis.generator.overwriteset to false. But mapper xml file will not be saved, it will be overwritten, do not know whether it is a pit. So the most mapper xml file you want to save under. Back to do manually merge

Reference Documents

http://www.mybatis.org/generator/index.html
http://www.mybatis.org/generator/configreference/xmlconfig.html#

Guess you like

Origin blog.csdn.net/guo_xl/article/details/86004068
Recommended