Mybatis逆向工程
Mybatis框架需要:实体类,自定义Mapper接口,Mapper.xml
传统的开发中上述的三个组件都需要开发者手动创建,逆向工程可以帮助开发者自动创建三个组件,提高效率
如何使用?
Mybatis Generator,简称MBG,是一个专门为Mybatis框架开发者定制的代码生成器,可自动生成Mybatis框架所需的实体类,Mapper接口,Mapper.xml,支持基本的CRUD操作,但是一些相对复杂的SQL需要开发者自己来完成。
-
新建Maven工程,pom.xml
-
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.southwind</groupId> <artifactId>aimbg</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency> <!--添加mbg依赖--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
-
创建MBG配置文件genneratorConfig.xml
1.jdbcConnection配置数据库连接信息。
2.javaModelGenerator配置javabean(实体类)的生成策略。
3.sqlMapGenerator配置Sql映射文件(Mapper.xml文件)生成策略。
4.javaClientGenerator配置自定义Mapper接口的生成策略。
5. table配置目标数据表(tableName:表名,domainObjectName:JavaBean类名)。
-
resources包下的配置文件
<?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/mybatis?userUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"
userId="root"
password="123456" >
</jdbcConnection>
<!--配置实体类。targetPackage=属性设定实体类存放的位置-->
<!--targetProject属性,从项目根目录到包的路径-->
<javaModelGenerator
targetPackage="com.southwind.entity" targetProject="./src/main/java"></javaModelGenerator>
<!--sqlMapGenerator配置Sql映射文件(Mapper.xml文件)生成策略。-->
<sqlMapGenerator targetPackage="com.southwind.respository" targetProject="./src/main/java"></sqlMapGenerator>
<!-- javaClientGenerator配置自定义Mapper接口的生成策略。-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.southwind.respository" targetProject="./src/main/java"></javaClientGenerator>
<!--table映射数据库的表,tableName=设置映射的表名。domainObjectName定义生成的实体类名字-->
<table tableName="user" domainObjectName="User"></table>
</context>
</generatorConfiguration>
-
创建Generator执行类
package com.southwind.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<String>(); 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(); } } }