Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Maven 如何让MyBatis生成器覆盖已生成的*Mapper.xml?_Maven_Pom.xml_Mybatis_Overwrite_Mybatis Generator - Fatal编程技术网

Maven 如何让MyBatis生成器覆盖已生成的*Mapper.xml?

Maven 如何让MyBatis生成器覆盖已生成的*Mapper.xml?,maven,pom.xml,mybatis,overwrite,mybatis-generator,Maven,Pom.xml,Mybatis,Overwrite,Mybatis Generator,像title一样,当我执行mybatis生成器时,我希望覆盖已经生成的*Mapper.xml全部,而不是合并! 但我尝试了很多配置方式,它并没有实现正确。 而且每次生成xml内容的次数都是一次又一次。 像这样: <resultMap id="BaseResultMap" type="com.test.entity.GoodsEntity"> ... <resultMap id="BaseResultMap" type="com.test.entity.GoodsEntity"&

像title一样,当我执行mybatis生成器时,我希望覆盖已经生成的*Mapper.xml全部,而不是合并! 但我尝试了很多配置方式,它并没有实现正确。 而且每次生成xml内容的次数都是一次又一次。 像这样:

<resultMap id="BaseResultMap" type="com.test.entity.GoodsEntity"> ...
<resultMap id="BaseResultMap" type="com.test.entity.GoodsEntity"> ...
<resultMap id="BaseResultMap" type="com.test.entity.GoodsEntity"> ...
。。。
...
...
在属性中,我添加了以下行:

<mybatis.generator.overwrite>true</mybatis.generator.overwrite>
true
在build>插件中,我添加了以下行:

<plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.5</version>
            <configuration>
                <verbose>true</verbose>
                <overwrite>true</overwrite>
                <configurationFile>${mybatis.generator.configurationFile}</configurationFile>
            </configuration>
            <executions>
                <execution>
                    <id>Generate MyBatis Artifacts</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.test</groupId>
                    <artifactId>ob-maven-plugin-mybatis-generator</artifactId>
                    <version>1.0</version>
                </dependency>
            </dependencies>
        </plugin>

org.mybatis.generator
mybatis生成器maven插件
1.3.5
真的
真的
${mybatis.generator.configurationFile}
生成MyBatis伪影
生成
com.test
ob maven插件mybatis发生器
1
在mybatis-generator.xml中,我尝试覆盖配置。 所有配置都不起作用


如何修改配置?

我今天遇到了相同的问题。要解决此问题,只需更改mybatis generator maven插件的版本即可

<mybatis-generator-maven-plugin.version>1.3.4-SNAPSHOT</mybatis-generator-maven-plugin.version>
1.3.4-SNAPSHOT

MyBatis generator如果找到匹配项,将始终合并XML文件。目前没有关闭的选项。

我可以通过创建一个插件并将其添加到mybatis-generator-config.xml文件来解决这个问题当然,请注意,此解决方案将导致始终覆盖Mapper.xml文件,而不管是否指定了-overwrite标志。

mybatis生成器config.xml:

<generatorConfiguration>
    ...
    <context id="myContextId">
        <plugin type="com.mydomain.DeleteExistingSqlMapsPlugin"></plugin>
        ...
    </context>
</generatorConfiguration>
...
public class DeleteExistingSqlMapsPlugin extends PluginAdapter {

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean sqlMapGenerated(GeneratedXmlFile sqlMap,
            IntrospectedTable introspectedTable)
    {
        String sqlMapPath = sqlMap.getTargetProject() + File.separator
                + sqlMap.getTargetPackage().replaceAll("\\.", File.separator)
                + File.separator + sqlMap.getFileName();
        File sqlMapFile = new File(sqlMapPath);

        sqlMapFile.delete();

        return true;
    }

}

...
...
DeleteExistingSqlMapsPlugin.java:

<generatorConfiguration>
    ...
    <context id="myContextId">
        <plugin type="com.mydomain.DeleteExistingSqlMapsPlugin"></plugin>
        ...
    </context>
</generatorConfiguration>
...
public class DeleteExistingSqlMapsPlugin extends PluginAdapter {

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean sqlMapGenerated(GeneratedXmlFile sqlMap,
            IntrospectedTable introspectedTable)
    {
        String sqlMapPath = sqlMap.getTargetProject() + File.separator
                + sqlMap.getTargetPackage().replaceAll("\\.", File.separator)
                + File.separator + sqlMap.getFileName();
        File sqlMapFile = new File(sqlMapPath);

        sqlMapFile.delete();

        return true;
    }

}
。。。
公共类DeleteExistingSqlMapsPlugin扩展了PluginAdapter{
@凌驾
公共布尔验证(列出警告){
返回true;
}
@凌驾
公共布尔sqlMapGenerated(GeneratedXmlFile sqlMap,
内省的(内省的)
{
字符串sqlMapPath=sqlMap.getTargetProject()+File.separator
+sqlMap.getTargetPackage().replaceAll(“\\.”,File.separator)
+File.separator+sqlMap.getFileName();
文件sqlMapFile=新文件(sqlMapPath);
sqlMapFile.delete();
返回true;
}
}

这是因为sqlMapGenerated()是在内存中创建Mapper.xml文件之后,但在写入磁盘之前调用的。

我编写了一个插件来合并xml Mapper文件

并修改mybatis生成器核心,将java和xml结合起来

这可以使xml和java文件的修改不被覆盖

用法:

<generatorConfiguration>
    ...
    <context id="myContextId">
        <plugin type="com.mydomain.CombineXmlPlugin"></plugin>
        ...
    </context>
</generatorConfiguration>

...
...
插件代码:

    package com.haitian.plugins;

import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.ShellCallback;
import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User:zhangweixiao
 * Description:
 * old nodes is your existing xml file's first level nodes,like <insert><resultMap>
 *  new nodes is mybatis-generator generate for you to combine
 * This compare the first level node's name and "id" attribute of new nodes and old nodes
 * if the two equal,then new node will not generate
 * so this can make you modification in old nodes not override.
 * if you want to regenrate old node,delete it,it will generate new.
 */
public class CombineXmlPlugin extends PluginAdapter {
    //shellCallback use TargetProject and TargetPackage to get targetFile
    ShellCallback shellCallback = new DefaultShellCallback(false);
    //save new nodes
    org.mybatis.generator.api.dom.xml.Document document;

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    /**
     * assing document variable to get new nodes
     * @param document
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean sqlMapDocumentGenerated(org.mybatis.generator.api.dom.xml.Document document,
                                           IntrospectedTable introspectedTable) {
        this.document = document;
        return true;
    }


    //new nodes is generated,but not write on disk,we just need to filter.
    @Override
    public boolean sqlMapGenerated(GeneratedXmlFile sqlMap,
                                   IntrospectedTable introspectedTable) {

        try {
            //get old nodes
            File directory = shellCallback.getDirectory(sqlMap.getTargetProject(), sqlMap.getTargetPackage());
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(false);
            DocumentBuilder db = dbf.newDocumentBuilder();
            File xmlFile = new File(directory, sqlMap.getFileName());
            if (directory.exists() == false || xmlFile.exists() == false)
                return true;
            Document doc = db.parse(new FileInputStream(xmlFile));
            org.w3c.dom.Element rootElement = doc.getDocumentElement();
            NodeList list = rootElement.getChildNodes();
            //get new nodes
            List<Element> elements = document.getRootElement().getElements();

            //get nodeName and the value of id attribute use regex
            Pattern p = Pattern.compile("<(\\w+)\\s+id=\"(\\w+)\"");

            boolean findSameNode = false;
            // traverse new nodes to compare old nodes to filter
            for (Iterator<Element> elementIt = elements.iterator(); elementIt.hasNext(); ) {
                findSameNode = false;
                String newNodeName = "";
                String NewIdValue = "";
                Element element = elementIt.next();
                Matcher m = p.matcher(element.getFormattedContent(0));
                if (m.find()) {
                  //get nodeName and the value of id attribute
                    newNodeName = m.group(1);
                    NewIdValue = m.group(2);
                }
                //if the nodeName of newNode and oldNode are equal
                //and the id attribute of newNode and oldNode are equal
                //then filter newNode
                for (int i = 0; i < list.getLength(); i++) {
                    Node node = list.item(i);
                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        if (newNodeName.equals(node.getNodeName())) {
                            NamedNodeMap attr = node.getAttributes();
                            for (int j = 0; j < attr.getLength(); j++) {
                                Node attrNode = attr.item(j);
                                if (attrNode.getNodeName().equals("id") && attrNode.getNodeValue().equals(NewIdValue)) {
                                    //filter new node,just delete it ,and it will not generate
                                    elementIt.remove();
                                    findSameNode = true;
                                    break;
                                }
                            }
                            if (findSameNode == true)
                                break;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
package com.haitai.plugins;
导入org.mybatis.generator.api.GeneratedXmlFile;
导入org.mybatis.generator.api.introspectable;
导入org.mybatis.generator.api.PluginAdapter;
导入org.mybatis.generator.api.shell回调;
导入org.mybatis.generator.api.dom.xml.Element;
导入org.mybatis.generator.internal.DefaultShellCallback;
导入org.w3c.dom.Document;
导入org.w3c.dom.NamedNodeMap;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入java.io.File;
导入java.io.FileInputStream;
导入java.util.Iterator;
导入java.util.List;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
/**
*用户:张维晓
*说明:
*旧节点是现有xml文件的第一级节点,如
*新节点是mybatis生成器生成的,供您合并
*这将比较新节点和旧节点的第一级节点的名称和“id”属性
*如果两者相等,则不会生成新节点
*因此,这可以使您在旧节点中的修改不被覆盖。
*如果要重新生成旧节点,请将其删除,它将生成新节点。
*/
公共类CombineXmlPlugin扩展了PluginAdapter{
//shellCallback使用TargetProject和TargetPackage获取targetFile
ShellCallback ShellCallback=新的DefaultShellCallback(false);
//保存新节点
org.mybatis.generator.api.dom.xml.Document文件;
@凌驾
公共布尔验证(列出警告){
返回true;
}
/**
*正在分配文档变量以获取新节点
*@param文件
*@param可自省
*@返回
*/
@凌驾
公共布尔sqlMapDocumentGenerated(org.mybatis.generator.api.dom.xml.Document、,
内省的(内省的){
本文件=文件;
返回true;
}
//生成新节点,但不在磁盘上写入,我们只需要进行筛选。
@凌驾
公共布尔sqlMapGenerated(GeneratedXmlFile sqlMap,
内省的(内省的){
试一试{
//获取旧节点
File directory=shellCallback.getDirectory(sqlMap.getTargetProject(),sqlMap.getTargetPackage());
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db=dbf.newDocumentBuilder();
File xmlFile=新文件(目录,sqlMap.getFileName());
if(directory.exists()==false | | xmlFile.exists()==false)
返回true;
Document doc=db.parse(新文件输入流(xmlFile));
org.w3c.dom.Element rootElement=doc.getDocumentElement();
NodeList list=rootElement.getChildNodes();
//获取新节点
列表元素=document.getRootElement().getElements();
//获取节点名和id属性的值使用regex
帕