Mybitas自动生成代码

导入pom.xml依赖

<dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.7</version>
</dependency>

 <plugin>
        <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.6</version>
          <configuration>
              <!-- 配置文件的位置 -->
              <configurationFile>generatorConfig.xml</configurationFile>
              <verbose>true</verbose>
              <overwrite>true</overwrite>
          </configuration>
</plugin>

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>

    <!-- 指定连接数据库的JDBC驱动,指定到本机的完整路径(可以指定到maven仓库中的jar路径) -->
    <classPathEntry
            location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar"/>

    <!-- 配置table表信息内容体,targetRuntime指定采用mybatis3的版本 -->
    <context id="aiot_drone_cloud" targetRuntime="MyBatis3">

        <!-- 抑制生成注释,由于生成的注释都是英文版的,可以不让它生成 -->
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://192.168.0.14:43306/aiot_drone_cloud?useUnicode=true&amp;characterEncoding=UTF8&amp;useSSL=false"
                        userId="root"
                        password="123123"
        />

        <!-- 生成model类,targetPackage指定model类的包名,targetProject指定生成的model放在哪个工程中 -->
        <javaModelGenerator targetPackage="com.dj.demo.domain"
                            targetProject="C:\Users\Administrator\Desktop\zhanhui\MybitasGet\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成mybatis的mapper.xml文件,targetPackage指定mapper.xml的包名,targetProject指定mapper.xml在哪个工程目录下 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="C:\Users\Administrator\Desktop\zhanhui\MybitasGet\src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成mybatis的Mapper接口类文件,targetPackage指定Mapper接口类文件的包名,targetProject指定Mapper接口类在哪个工程目录下 -->
        <javaClientGenerator targetPackage="com.dj.demo.mapper"
                             targetProject="C:\Users\Administrator\Desktop\zhanhui\MybitasGet\src\main\java"
                             type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>-->
        <!-- 数据库表以及对应的java模型类名(如果是多个表的话,可以复制多份然后修改成对应的表以及需要生成的模型名) -->
        <table tableName="sys_user" domainObjectName="SysUser"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"
                catalog="aiot_drone_cloud"
        >
            <!--<columnRenamingRule searchString="^D_"
                                replaceString=""/>-->
            <property name="ignoreQualifiersAtRuntime" value="true"></property>
        </table>

    </context>
</generatorConfiguration>

MybatisGen.java

package com.dj.demo;


import java.awt.geom.GeneralPath;
import java.awt.im.InputContext;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.log4j.chainsaw.Main;
import org.apache.log4j.lf5.util.Resource;
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;

public class MybatisGen {
    public static void generator() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //项目根路径不要有中文,我的有中文,所以使用绝对路径
        File configFile = new File("C:\\Users\\Administrator\\Desktop\\zhanhui\\MybitasGet\\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);
    }
    public static void main(String[] args) {
        try {
            generator();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


表重复问题 参考解决方案 https://blog.csdn.net/weixin_49610478/article/details/112694508

posted @ 2022-07-17 13:56  离人怎挽_wdj  阅读(65)  评论(0编辑  收藏  举报