逆向工程
MyBatis 框架需要:实体类、自定义 Mapper 接口、Mapper.xml
传统的开发中,上述的三个组件,需要开发者手动来创建,逆向工程可以帮助开发者来自动创建这三个组件,减轻开发者的工作量,提高工作效率。
如何使用
MyBatis Generator,简称 MBG,是一个专门为 MyBatis 框架开发者定义的代码生成器,可自动生成 MyBatis 框架所需的实体类、Mapper 接口、Mapper.xml,支持基本的 CRUD 操作。但是一些相对复杂的 SQL 需要开发者自己来完成。
-
新建 Maven 工程 pom.xml
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies>
-
创建 MBG 配置文件 generatorConfig.xml
1、jdbcConnection 配置数据库连接信息
2、javaModelGenerator 配置 JavaBean 的生成策略
3、sqlMapGenerator 配置 SQL 映射文件生成策略
4、javaClientGenerator 配置 Mapper 接口的生成策略
5、table 配置目标数据表(tableName:表名,domainObjectName:JavaBean 类名)
<!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="testTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8" userId="root" password="123456"> </jdbcConnection> <javaModelGenerator targetPackage="com.xiaoxin.entity" targetProject="./src/main/java"></javaModelGenerator> <sqlMapGenerator targetPackage="com.xiaoxin.repository" targetProject="./src/main/java"></sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.xiaoxin.repository" targetProject="./src/main/java"></javaClientGenerator> <table tableName="t_user" domainObjectName="User"></table> </context> </generatorConfiguration>
-
创建 Generator 执行类
package com.xiaoxin.test; import org.apache.ibatis.io.Resources; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.IOException; import java.sql.SQLException; import java.util.List; import java.util.ArrayList; import java.io.File; public class Test { public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { List<String> warings = new ArrayList<String>(); boolean overwrite = true; String genCig = "/generatorConfig.xml"; File configFile = new File(Test.class.getResource(genCig).getFile()); ConfigurationParser configurationParser = new ConfigurationParser(warings); Configuration configuration = null; configuration = configurationParser.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = null; myBatisGenerator = new MyBatisGenerator(configuration, callback, warings); myBatisGenerator.generate(null); } }