其他分享
首页 > 其他分享> > Mybatis逆向工程的MBG

Mybatis逆向工程的MBG

作者:互联网

Mybatis逆向工程的MBG

1.generatorConfig.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>

  <!-- toString MyBatis3 类型可选 详情参考官网MyBatis Generator Core – MyBatis Generator Quick Start Guide  -->

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- toString  -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <!-- 如果不加会在生成的内容中出现很多注释 -->
        <commentGenerator>
            <!-- 是否去除自动生成日期的注释 true:是 : false:否 -->
            <property name="suppressDate" value="true"/>
            <!-- 是否去除所有自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://47.106.185.191:3306/zh_mysql?useSSL=false&useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8"
                        userId="root"
                        password="123456">
        </jdbcConnection>

        <!--生成entity类存放位置 也可使用相对位置-->
        <javaModelGenerator targetPackage="com.zh.demo.bean" targetProject="D:\IdeaProjects\demo1\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--生成映射文件存放位置 也可使用相对位置-->
        <sqlMapGenerator targetPackage="mybatis.mapper" targetProject="D:\IdeaProjects\demo1\src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.zh.demo.mapper" targetProject="D:\IdeaProjects\demo1\src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 有几个表就写几个 表名与要生成的类名 -->
        <table tableName="person" domainObjectName="Person" />

    </context>
</generatorConfiguration>

​

  1. 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>org.zh</groupId>
    <artifactId>mybatis-generatorConfiguration</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
    </dependencies>


</project>
  1. MBG运行类
package com.zh.demo;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MBG {

        public static void main(String[] args) throws Exception {
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            File configFile = new File("D:\\IdeaProjects\\mybatis-generatorConfiguration\\src\\main\\resources\\generatorConfig.xml");
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        }
}

标签:xml,逆向,generator,MBG,new,mybatis,org,Mybatis,import
来源: https://blog.csdn.net/qq_36390561/article/details/120534380