- pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiaohe</groupId>
<artifactId>flea_market-common-reverse</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 依赖 MyBatis 核心包 -->
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- Mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!--数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--逆向工程的核心依赖-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
</project>
- MyGennerator运行
<?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="mysql-connector-java-5.1.48-bin.jar"/>-->
<!--
defaultModelType=flat(推荐)只为每张表生成一个实体类,这个实体类包含表中的所有字段
targetRuntime MyBatis3 ( MyBatis3Simple不会生成与Example相关的方法)-->
<context id="MySqlTables" targetRuntime="MyBatis3" defaultModelType="flat">
<!--前后置分隔符-->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<commentGenerator>
<!--阻止生成注释,默认为false-->
<property name="suppressAllComments" value="true"/>
<!--阻止生成的注释,包含时间戳,默认为false-->
<property name="suppressDate" value="true"/>
<!-- 注释是否添加数据库表的备注信息,默认为false-->
<!-- <property name="addRemarkComments" value="true"/>-->
</commentGenerator>
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/flea_market?serverTimezone=GMT%2B8"
userId="root"
password="123456"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--src\main\java-->
<javaModelGenerator targetPackage="com.xiaohe.entity"
targetProject="flea_market-common-reverse\src\main\java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 映射文件生成的路径 -->
<sqlMapGenerator targetPackage="com.xiaohe.mapper"
targetProject="flea_market-common-reverse\src\main\resources">
<!--是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--type="XmlMapper":所有的方法都在XML中,接口调用依赖XML文件 -->
<!--type= "ANNOTATEDMAPPER",则SQL生成在Mapper接口的注解中 annotatedMapper-->
<!--type= "MixedMapper",则SQL生成在Mapper接口的注解中-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.xiaohe.mapper" targetProject="flea_market-common-reverse\src\main\java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--<table tableName="%customer%"> 如果所有的则%-->
<table tableName="t_admin" domainObjectName="Admin">
</table>
</context>
</generatorConfiguration>
- 启动类
public class MyGennerator {
private String path = "/generatorConfig.xml";//指定配置文件路径
private boolean overwrite = true; //当生成的代码重复时,覆盖原代码
public static void main(String[] args) {
MyGennerator generator = new MyGennerator();
generator.generateMyBatis();
}
// targetRuntime="MyBatis3Simple", 不生成Example
public void generateMyBatis() {
//该集合记录MBG执行过程中的警告信息
List<String> warnings = new ArrayList<String>();
InputStream is = MyGennerator.class.getResourceAsStream(path);//读取MBG配置文件
ConfigurationParser cp = new ConfigurationParser(warnings);
try {
Configuration config = cp.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);//执行生成代码
} catch (Exception e) {
e.printStackTrace();
}
//打印出执行过程中的警告信息,以便于修改
for (String warning : warnings) {
System.out.println(warning);
}
}
}
成功