main函数生成:
工程代码demo:
- 添加依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
- 代码生成配置文件 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="F:\maven_local_repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar"/>
<context id="MySqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- beginningDelimiter/endingDelimiter: 指明数据库的用于标记数据库对象名的符号,比如ORACLE是双引号,MYSQL默认是`反引号 -->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 删除自动生成注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/mooc_one"
userId="root"
password="root">
</jdbcConnection>
<!-- 自动生成实体类存放的位置 -->
<javaModelGenerator targetPackage="com.mybatis.generator.entity"
targetProject="src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 自动生成Mapper.xml存放的位置 -->
<sqlMapGenerator targetPackage="mappers"
targetProject="src\main\resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 自动生成Mapper接口存放的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.mybatis.generator.mapper"
targetProject="src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="item_info" domainObjectName="ItemInfo">
<generatedKey column="id" sqlStatement="MySql"/>
</table>
<!--<table tableName="%">-->
<!--<generatedKey column="id" sqlStatement="MySql"/>-->
<!--</table>-->
</context>
</generatorConfiguration>
- main函数
public class GeneratorMain {
public void generator() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// 指定配置文件
String genCfg = "/generatorConfig.xml";
File configFile = new File(GeneratorMain.class.getResource(genCfg).getFile());
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);
}
public static void main(String[] args) {
try {
GeneratorMain generatorSqlmap = new GeneratorMain();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
插件生成
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<!--配置文件所在位置-->
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>