mybatis-generator插件如何修改配置文件路径

导读:本篇文章讲解 mybatis-generator插件如何修改配置文件路径,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

  • 方法1:pom配置文件中指定配置文件路径

pom.xml中的插件配置,在configurationFile中指定文件路径为${basedir}/src/test/resources/generatorConfig.xml,  其中${basedir}是内置变量,定位到当前maven(子)项目根目录

 <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <!-- 在此处指定代码生成文件 basedir变量定位到当前项目根目录 -->
                    <configurationFile>${basedir}/src/test/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.38</version>
                    </dependency>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-validator</artifactId>
                        <version>6.0.7.Final</version>
                    </dependency>
                    <dependency>
                        <groupId>com.itfsw</groupId>
                        <artifactId>mybatis-generator-plugin</artifactId>
                        <version>1.0.19</version>
                    </dependency>

                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.0.3-beta1</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

运行 mvn clean package -DskipTests mybatis-generator:generate 即可生成

  • 方法2: 创建一个main方法或者单元测试,在其中指定文件

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
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;


@Slf4j
public class GenerateCodeMain {

    @Test
    public void testGenerate(){
        List<String>        warnings    = new ArrayList<>();
        boolean             overwrite  = true;
        String              genCfg     = "/generatorConfig-2.xml";
        File                configFile = new File(GenerateCodeMain.class.getResource(genCfg).getFile());
        ConfigurationParser cp         = new ConfigurationParser(warnings);
        Configuration       config     = null;
        try {
            config = cp.parseConfiguration(configFile);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLParserException e) {
            e.printStackTrace();
        }
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = null;
        try {
            myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
        try {
            myBatisGenerator.generate(null);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("generate finish");
        log.info("do callback \n warnings:{}", warnings);


    }
}

方法2 有一个问题是需要将plugin dependency中依赖迁移到dependencies标签下

  • PS. generationConfig.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="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
        </plugin>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/testdb"
                        userId="root"
                        password="root123456">
        </jdbcConnection>


        <javaModelGenerator targetPackage="cn.test.model1" targetProject="../core/src/main/java">
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mybatis/mapper/model1" targetProject="../core/src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator targetPackage="cn.test.model1" targetProject="../core/src/main/java"
                             type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>



        <table tableName="account" domainObjectName="AccountDO" >
            <generatedKey column="id" sqlStatement="mysql" identity="true"/>
        </table>

       
    </context>
</generatorConfiguration>

参考:Mybatis最入门—代码自动生成(generatorConfig.xml配置)

扩展阅读:mybatis单元测试(无需启动容器)icon-default.png?t=L892https://blog.csdn.net/qq_33217349/article/details/92993772

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/9919.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!