1、引入依赖
<!-- Mybatis Generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
2、添加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>
<!-- 数据库驱动包位置 -->
<properties resource="mybatis-generator\generatorConfig.properties"/>
<classPathEntry location="C:\tools\maven-repository\mysql\mysql-connector-java\5.1.45\mysql-connector-java-5.1.45.jar" />
<!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
<!-- 一个数据库一个content -->
<context id="MybatisTables" targetRuntime="MyBatis3">
<property name="autoDelimitKeyWords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="${jdbc_driver}"
connectionURL="${jdbc_url}"
userId="${jdbc_username}"
password="${jdbc_password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成实体类的包名和位置,这里配置将生成的实体类放在cn.chejinbao.ecommerce.entity这个包下 -->
<javaModelGenerator targetPackage="com.ithuang.pojo" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的SQL映射文件包名和位置,这里配置将生成的SQL映射文件放在cn.chejinbao.ecommerce.mapping这个包下 -->
<sqlMapGenerator targetPackage="mappers" targetProject="src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置,这里配置将生成的dao类放在cn.chejinbao.ecommerce.dao这个包下 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.ithuang.dao" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
<table tableName="tb_patrol_record" domainObjectName="PatrolRecord"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
3、配置文件
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url = jdbc:mysql://localhost:3306/patrol?autoReconnect=true&useSSL=false&characterEncoding=utf-8
jdbc_username = root
jdbc_password = root
4、添加主函数
public static void main(String[] args)
throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// 读取配置文件generatorConfig.xml
File configFile = new File("src/main/resources/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);
}
运行结果