IDEA多项目的情况下Mybatis生成逆向工程老是找不到xml文件的解决

在使用idea生成Mybatis逆向工程时,老是加载不到generator.xml文件

在这里插入图片描述

我写的路径没有问题啊,这是我的项目结构:

在这里插入图片描述

  • 这是我的主类:
public class TestGenerator {

    public static void main(String[] args) throws Exception{


            File file = new File("generator.xml");
            List<String> warnings = new ArrayList<>();
            ConfigurationParser cp = new ConfigurationParser(warnings);

            Configuration config = cp.parseConfiguration(file);

            DefaultShellCallback callback = new DefaultShellCallback(true);

            //逆向工程的核心类
            MyBatisGenerator generator = new MyBatisGenerator(config, callback, warnings);
            generator.generate(null);
        }
}

原来使用Mybatis生成逆向工程时,配置文件必须是绝对路径,才能识别xml
下面是我生成逆向工程的完整过程:

  • generator.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_3.2.dtd">
<generatorConfiguration>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!--
                         suppressAllComments属性值:
                             true:自动生成实体类、SQL映射文件时没有注释
                             true:自动生成实体类、SQL映射文件,并附有注释
                       -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test"
                        userId="root"  password="">
        </jdbcConnection>
        <!--
                  forceBigDecimals属性值:
                      true:把数据表中的DECIMAL和NUMERIC类型,
      解析为JAVA代码中的java.math.BigDecimal类型
                      false(默认):把数据表中的DECIMAL和NUMERIC类型,
      解析为解析为JAVA代码中的Integer类型
              -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!--
                  targetProject属性值:实体类的生成位置
                  targetPackage属性值:实体类所在包的路径
              -->
        <javaModelGenerator targetPackage="com.itt.entity"
                            targetProject="I:\MyBatisIdea\MyBatisGenerator\src\main\java">
            <!-- trimStrings属性值:
                         true:对数据库的查询结果进行trim操作
                         false(默认):不进行trim操作
                       -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--
                  targetProject属性值:SQL映射文件的生成位置
                  targetPackage属性值:SQL映射文件所在包的路径
              -->
        <sqlMapGenerator targetPackage="com.itt.mapper"
                         targetProject="I:\MyBatisIdea\MyBatisGenerator\src\main\java">
        </sqlMapGenerator>
        <!-- 生成动态代理的接口  -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.itt.mapper"
                             targetProject="I:\MyBatisIdea\MyBatisGenerator\src\main\java">
        </javaClientGenerator>

        <!-- 指定数据库表  -->
        <table tableName="student"> </table>
        <table tableName="studentCard"> </table>
        <table tableName="studentClass"> </table>
    </context>
</generatorConfiguration>

注意:

在这里插入图片描述

  • 主类
public static void main(String[] args) throws Exception{

            File file = new File("generator.xml");
            List<String> warnings = new ArrayList<>();
            ConfigurationParser cp = new ConfigurationParser(warnings);

            Configuration config = cp.parseConfiguration(file);

            DefaultShellCallback callback = new DefaultShellCallback(true);

            //逆向工程的核心类
            MyBatisGenerator generator = new MyBatisGenerator(config, callback, warnings);
            generator.generate(null);
    }
  • 生成的项目结构图
    在这里插入图片描述

注意:在多个项目的过程中使用的是绝对路径!

发布了27 篇原创文章 · 获赞 23 · 访问量 6582

猜你喜欢

转载自blog.csdn.net/weixin_42893085/article/details/105110672