MyBatis Generator:
代码生成器
可以根据指定的表快速生成对应的映射文件,接口,以及Bean类
支持基本的增删改查,以及QBC风格的条件查询
但是一些复杂的表连接还是需要我们自己来去编写
使用步骤:
1、下载:
- https://github.com/mybatis/generator/releases
2、把相关jar导入到工程当中
3、创建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>
<!--
targetRuntime:设置自动生成的版本
MyBatis3:
MyBatis3Simple:简单增删改查
-->
<!--建议读者将<context id="DB2Tables" targetRuntime="MyBatis3">改为
<context id="mybatis" targetRuntime="MyBatis3Simple">这样读者更好理解与应用 -->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--
不要生成日期和备注
-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"
userId="root"
password="1234">
</jdbcConnection>
<!--
配置domain生成策略
targetProject:把自动生成的domian放在哪个工程里面
targetPackage:哪个包下
-->
<javaModelGenerator targetPackage="com.itlike.domain" targetProject=".\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--
配置mapper的生成策略
targetPackage:把自动生成的mapper放在哪个工程里面
targetProject:哪个包下
-->
<sqlMapGenerator targetPackage="com.itlike.mapper" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--
mapper接口生成策略
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.itlike.mapper" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="customer" domainObjectName="Customer" ></table>
<table tableName="teacher" domainObjectName="Teacher" ></table>
<table tableName="student" domainObjectName="Student" ></table>
</context>
</generatorConfiguration>
4、编写生成代码
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("./src/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);