由表到类生成
准备
1、jar包
mybatis-generator-core.jar
2、配置文件 generator.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>
<!--
suppressAllComments属性值:
true:自动生成实体类、SQL映射文件时没有注释
true:自动生成实体类、SQL映射文件,并附有注释
-->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test"
userId="root" password="mysql">
</jdbcConnection>
<!--
forceBigDecimals属性值:
true:把数据表中的DECIMAL和NUMERIC类型,
解析为JAVA代码中的java.math.BigDecimal类型
false(默认):把数据表中的DECIMAL和NUMERIC类型,
解析为解析为JAVA代码中的Integer类型
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--
targetProject属性值:实体类的生成位置
targetPackage属性值:实体类所在包的路径
-->
<javaModelGenerator targetPackage="org.zq.entity"
targetProject=".\src">
<!-- trimStrings属性值:
true:对数据库的查询结果进行trim操作
false(默认):不进行trim操作
-->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--
targetProject属性值:SQL映射文件的生成位置
targetPackage属性值:SQL映射文件所在包的路径
-->
<sqlMapGenerator targetPackage="org.zq.mapper"
targetProject=".\src">
</sqlMapGenerator>
<!-- 生成动态代理的接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="org.zq.mapper" targetProject=".\src">
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="person"> </table>
<table tableName="personjob"> </table>
<table tableName="personbook"> </table>
</context>
</generatorConfiguration>
生成main方法
private static void generator() throws InterruptedException, SQLException, IOException, XMLParserException, InvalidConfigurationException {
File file = new File("src/generator.xml");
List<String> warings = new ArrayList<>();
ConfigurationParser cp = new ConfigurationParser(warings);
Configuration configuration = cp.parseConfiguration(file);
DefaultShellCallback callback = new DefaultShellCallback(true);
//核心方法
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, callback, warings);
myBatisGenerator.generate(null);
}