0.导入核心依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
1.创建mbg.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="DB2Tables" targetRuntime="MyBatis3">
<!-- 去掉反向生成的注释-->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- mybatis连接数据库-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/moxing3?serverTimezone=GMT%2B8"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成的bean路径-->
<javaModelGenerator targetPackage="com.mark.bean" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- xxxmapper.xml生成的路径-->
<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- xxxmapper接口生成的路径-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.mark.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 让数据库中的表反向生成类-->
<table schema="DB2ADMIN" tableName="ceshi_2_mrcg" domainObjectName="Mrcg"/>
<table schema="DB2ADMIN" tableName="ceshi_2_mrgpc" domainObjectName="Mrgpc"/>
<table schema="DB2ADMIN" tableName="ceshi_2_mrzzc" domainObjectName="Mrzzc"/>
<table schema="DB2ADMIN" tableName="gpdm" domainObjectName="Gpdm"/>
<table schema="DB2ADMIN" tableName="gprkb" domainObjectName="Gprkb"/>
</context>
</generatorConfiguration>
2.编写一个启动该xml文件的类,并运行
public class test {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.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);
}
}