码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis的逆向工程

时间:2020-09-17 23:34:19      阅读:35      评论:0      收藏:0      [点我收藏+]

标签:pack   word   structure   group   defaults   employee   href   需要   sql脚本   

1 简介

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

2 MBG逆向工程

  • sql脚本:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `last_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `gender` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of employee
-- ----------------------------
INSERT INTO `employee` VALUES (1, ‘jerry‘, ‘男‘, ‘jerry@qq.com‘);
INSERT INTO `employee` VALUES (2, ‘aa‘, ‘男‘, ‘aa@11.com‘);
INSERT INTO `employee` VALUES (3, ‘bb‘, ‘男‘, ‘bb@11.com‘);
INSERT INTO `employee` VALUES (4, ‘aa‘, ‘男‘, ‘aa@11.com‘);
INSERT INTO `employee` VALUES (5, ‘bb‘, ‘男‘, ‘bb@11.com‘);

SET FOREIGN_KEY_CHECKS = 1;
  • 导入相关jar包的Maven坐标:
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.4.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.21</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.5</version>
</dependency>
  • 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>

    <!--  指定代码生成的上下文环境  -->
    <context id="mysql" targetRuntime="MyBatis3">
        <!-- 注释生成 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- jdbcConnection:指定如何连接到目标数据库 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        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"
                        userId="root"
                        password="123456">
        </jdbcConnection>

        <!-- Java类型解析器 -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--
            javaModelGenerator:指定JavaBean的生成策略
                targetPackage:目标包名,指定JavaBean生成的包名
                targetProject:目标工程
        -->
        <javaModelGenerator targetPackage="com.sunxiaping.mbg.domain" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--
            sqlMapGenerator:SQL映射生成策略
                targetPackage:目标包名,指定生成Mapper接口的对应的包名
        -->
        <sqlMapGenerator targetPackage="com.sunxiaping.mbg.mapper" targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--
            javaClientGenerator:Mapper接口所在的位置
        -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.sunxiaping.mbg.mapper" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 指定要逆向分析哪些表:根据表创建JavaBean -->
        <table schema="mysql" tableName="employee" domainObjectName="Employee"></table>

    </context>
</generatorConfiguration>
  • MBGTest.java
package com.sunxiaping.mbg;

import org.apache.ibatis.io.Resources;
import org.junit.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.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MBGTest {

    @Test
    public void testGenerator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<>();
        boolean overwrite = true;
        InputStream inputStream = Resources.getResourceAsStream("mbg.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(inputStream);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
   
}

Mybatis的逆向工程

标签:pack   word   structure   group   defaults   employee   href   需要   sql脚本   

原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/13660283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!