第一章:简介

  • Mybatis Generator:简称 MBG ,是一个专门为 Mybatis 框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件、接口以及 Bean 类。支持基本的增删改查以及 QBC 风格的条件查询。但是表连接、存储过程等这些复杂的 SQL 的定义还是需要我们手动编写的。
  • 官方文档地址
  • 官方工程地址

第二章:MBG 逆向工程

  • sql 脚本:
  1. SET NAMES utf8mb4;
  2. SET FOREIGN_KEY_CHECKS = 0;
  3. -- ----------------------------
  4. -- Table structure for employee
  5. -- ----------------------------
  6. DROP TABLE IF EXISTS `employee`;
  7. CREATE TABLE `employee` (
  8. `id` int(11) NOT NULL AUTO_INCREMENT,
  9. `last_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  10. `gender` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  11. `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  12. PRIMARY KEY (`id`) USING BTREE
  13. ) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  14. -- ----------------------------
  15. -- Records of employee
  16. -- ----------------------------
  17. INSERT INTO `employee` VALUES (1, 'jerry', '男', 'jerry@qq.com');
  18. INSERT INTO `employee` VALUES (2, 'aa', '男', 'aa@11.com');
  19. INSERT INTO `employee` VALUES (3, 'bb', '男', 'bb@11.com');
  20. INSERT INTO `employee` VALUES (4, 'aa', '男', 'aa@11.com');
  21. INSERT INTO `employee` VALUES (5, 'bb', '男', 'bb@11.com');
  22. SET FOREIGN_KEY_CHECKS = 1;
  • 导入相关 jar 包的 Maven 坐标:
  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <version>4.11</version>
  5. <scope>test</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.mybatis.generator</groupId>
  9. <artifactId>mybatis-generator-core</artifactId>
  10. <version>1.4.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>mysql</groupId>
  14. <artifactId>mysql-connector-java</artifactId>
  15. <version>8.0.21</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.mybatis</groupId>
  19. <artifactId>mybatis</artifactId>
  20. <version>3.5.5</version>
  21. </dependency>
  • mbg.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!-- 指定代码生成的上下文环境 -->
  7. <context id="mysql" targetRuntime="MyBatis3">
  8. <!-- 注释生成 -->
  9. <commentGenerator>
  10. <property name="suppressDate" value="true"/>
  11. <property name="suppressAllComments" value="true" />
  12. </commentGenerator>
  13. <!-- jdbcConnection:指定如何连接到目标数据库 -->
  14. <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
  15. connectionURL="jdbc:mysql://192.168.134.100:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;useSSL=false&amp;serverTimezone=GMT%2B8&amp;allowPublicKeyRetrieval=true&amp;allowMultiQueries=true"
  16. userId="root"
  17. password="123456">
  18. </jdbcConnection>
  19. <!-- Java类型解析器 -->
  20. <javaTypeResolver>
  21. <property name="forceBigDecimals" value="false"/>
  22. </javaTypeResolver>
  23. <!--
  24. javaModelGenerator:指定JavaBean的生成策略
  25. targetPackage:目标包名,指定JavaBean生成的包名
  26. targetProject:目标工程
  27. -->
  28. <javaModelGenerator targetPackage="com.sunxiaping.mbg.domain" targetProject=".\src\main\java">
  29. <property name="enableSubPackages" value="true"/>
  30. <property name="trimStrings" value="true"/>
  31. </javaModelGenerator>
  32. <!--
  33. sqlMapGenerator:SQL映射生成策略
  34. targetPackage:目标包名,指定生成Mapper接口的对应的包名
  35. -->
  36. <sqlMapGenerator targetPackage="com.sunxiaping.mbg.mapper" targetProject=".\src\main\resources">
  37. <property name="enableSubPackages" value="true"/>
  38. </sqlMapGenerator>
  39. <!--
  40. javaClientGenerator:Mapper接口所在的位置
  41. -->
  42. <javaClientGenerator type="XMLMAPPER" targetPackage="com.sunxiaping.mbg.mapper" targetProject=".\src\main\java">
  43. <property name="enableSubPackages" value="true"/>
  44. </javaClientGenerator>
  45. <!-- 指定要逆向分析哪些表:根据表创建JavaBean -->
  46. <table schema="mysql" tableName="employee" domainObjectName="Employee"></table>
  47. </context>
  48. </generatorConfiguration>
  • MBGTest.java
  1. package com.sunxiaping.mbg;
  2. import org.apache.ibatis.io.Resources;
  3. import org.junit.Test;
  4. import org.mybatis.generator.api.MyBatisGenerator;
  5. import org.mybatis.generator.config.Configuration;
  6. import org.mybatis.generator.config.xml.ConfigurationParser;
  7. import org.mybatis.generator.exception.InvalidConfigurationException;
  8. import org.mybatis.generator.exception.XMLParserException;
  9. import org.mybatis.generator.internal.DefaultShellCallback;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.sql.SQLException;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. public class MBGTest {
  16. @Test
  17. public void testGenerator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
  18. List<String> warnings = new ArrayList<>();
  19. boolean overwrite = true;
  20. InputStream inputStream = Resources.getResourceAsStream("mbg.xml");
  21. ConfigurationParser cp = new ConfigurationParser(warnings);
  22. Configuration config = cp.parseConfiguration(inputStream);
  23. DefaultShellCallback callback = new DefaultShellCallback(overwrite);
  24. MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
  25. myBatisGenerator.generate(null);
  26. }
  27. }