MyBatis Generator:
简称MBG,是专门为MyBatis开发者定制的代码生成器,可以快速的根据数据表生成对应的映射文件、接口、以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储 过程这些复杂的SQL定义需要我们手工编写;
官方文档地址:http://www.mybatis.org/generator/
官方工程地址:https://github.com/mybatis/generator/releases
1. 依赖包:
<dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies>
2. 建立一个mbg.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="MyBatis3Simple":生成简单版的CRUD targetRuntime="MyBatis3":生成豪华版,复杂查询,动态映射等 --> <context id="DB2Tables" targetRuntime="MyBatis3Simple"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除注释时间戳,去除为true --> <property name="suppressAllComments" value="true"/> <!-- 是否去除注释,去除为true --> </commentGenerator> <!-- jdbcConnection:如何连接到目标数据库 --> <jdbcConnection driverClass="org.gjt.mm.mysql.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- javaModelGenerator:指定javaBean的生成策略 targetPackage:目标包名 targetProject:目标工程 --> <javaModelGenerator targetPackage="cn.ll.mybatis.vo" targetProject=".src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- sqlMapGenerator:sql映射生成策略 targetPackage:目标包名 targetProject:目标工程 --> <sqlMapGenerator targetPackage="cn.ll.mybatis.dao" targetProject=".src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- javaClientGenerator:指定Mapper接口所在的位置 targetPackage:目标包名 targetProject:目标工程 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.ll.mybatis.dao" targetProject=".src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- table:指定要逆向分析哪些表:根据表要创建javabean tableName;表名 domainObjectName:要生成的类名 --> <table tableName="employee" domainObjectName="Employee"></table> </context> </generatorConfiguration>
3. 通过加载xml配置,生成对应文件:
public class MbgTest {
@Test
public void testMbg() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/main/resources/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);
}
}