一 概述
二 XML配置文件generatorConfig.xml
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>
<!--数据库驱动-->
<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
connectionURL="jdbc:db2:TEST"
userId="db2admin"
password="db2admin">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="test.model" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="test.xml" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--生成对应表及类名-->
<table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
</context>
</generatorConfiguration>
三 生成代码的不同方式
A. From the Command Line
java -jar mybatis-generator-core-x.x.x.jar -configfile \temp\generatorConfig.xml -overwrite
B. Running MyBatis Generator With Maven
<project ...>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</plugin>
...
</plugins>
...
</build>
...
</project>
Maven Goal and Execution
The MBG Maven plugin includes one goal:
mybatis-generator:generate
goal
不是由Maven自动执行。它可以通过两种方式来执行. The goal can be executed from the command line with the command:
mvn mybatis-generator:generate
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
C. Running MyBatis Generator With Java
from Java with an XML Configuration File
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
from Java with a Java Based Configuration
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
Configuration config = new Configuration();
// ... fill out the config object as appropriate...
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
D. Running MyBatis Generator with Eclipse
Eclipse Plugin
org.mybatis.generator.eclipse.site-1.3.7.201807042148.zip
Using the Eclipse Feature
To get up and running quickly with MyBatis Generator in the eclipse environment, follow these steps:
- Create a configuration file:
- File>New>Other...
- Select "MyBatis Generator Configuration File" from the "MyBatis" category, then press "Next"
- Specify the location of the file, and the name of the file (defaults to generatorConfig.xml)
- Finish the wizard
- Fill out the configuration file appropriately. At a minimum, you must specify:
- jdbcConnection information to specify how to connect to the target database
- A target package, and target project for the javaModelGenerator
- A target package, and target project for the sqlMapGenerator
- A target package, target project, and type for the javaClientGenerator (or you can remove the javaClientGenerator element if you don't wish to generate Java Clients)
- At least one database table
- Save the file
- Right click on the configuration file in Eclipse's navigator, or package explorer, view and take the menu option "Run As>Run MyBatis Generator"
<?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>
<classPathEntry location="D:\apache-maven-3.5.4\repo\com\oracle\ojdbc\6\ojdbc-6.jar"/>
<context id="context1">
<jdbcConnection connectionURL="jdbc:oracle:thin:@localhost:1521:orcl"
driverClass="oracle.jdbc.OracleDriver" password="dev" userId="dev" />
<javaModelGenerator targetPackage="com.neusoft.demo1.sys.model" targetProject="sys\src\main\java" />
<sqlMapGenerator targetPackage="com.neusoft.demo1.sys.mapper" targetProject="sys\src\main\resources" />
<javaClientGenerator targetPackage="com.neusoft.demo1.sys.mapper" targetProject="sys\src\main\java" type="XMLMAPPER" />
<!-- enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" -->
<table schema="dev" tableName="T_USER">
<property name="useActualColumnNames" value="false"/>
<domainObjectRenamingRule searchString="^T" replaceString=""/>
<columnOverride column="USER_ID" property="id" />
</table>
</context>
</generatorConfiguration>
参考文档:| Eclipse Plugins, Bundles and Products - Eclipse Marketplace
MyBatis Generator Core – MyBatis Generator XML Configuration File Reference