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