1、配置文件 mbg.xml(放在src同级目录下)
<?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>
<!--
targetRuntime 可以设置生成的版本
MyBatis3 豪华版本
MyBatis3Simple CRUD操作
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--去掉全部注释-->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--
修改数据库的四个连接属性
-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mbg"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--
javaModelGenerator生成javaBean
targetPackage指定生成的包
targetProject指定生成的项目位置
-->
<javaModelGenerator targetPackage="com.atguigu.pojo"
targetProject=".MyBatis03_mbgsrc">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--
sqlMapGenerator标签是生成mapper.xml配置信息
targetPackage指定生成的包
targetProject指定生成的项目位置
-->
<sqlMapGenerator targetPackage="com.atguigu.mapper" targetProject=".MyBatis03_mbgsrc">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--
javaClientGenerator生成mapper接口
targetPackage指定生成的包
targetProject指定生成的项目位置
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mapper"
targetProject=".MyBatis03_mbgsrc">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="t_user" domainObjectName="User" ></table>
<table tableName="t_book" domainObjectName="Book" ></table>
</context>
</generatorConfiguration>
2、生成器Runner.java
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Runner {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("MyBatis03_mbg/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);
}
}