MyBatisGenerator java 文件合并问题解决

为了解决MyBatisGenerator的java文件合并问题,可以继承DefaultShellCallback并覆盖isMergeSupported和mergeJavaFile方法。在mergeJavaFile中,利用javaparser库解析并比较Java文件,当新生成的代码与旧代码有重复时,如果旧代码已存在相同的成员变量,则不再进行合并,避免重复代码。具体实现可参照相关博客文章。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总体思路:

继承DefaultShellCallback.java,Overide 这个类里面isMergeSupported和mergeJavaFile方法,具体实现mergeJavaFile。

其中实现mergeJavaFile,需要引入javaparser相关包。

private static class MyCallBack extends DefaultShellCallback {
    public MyCallBack() {
        super(true);
    }

    @Override
    public boolean isMergeSupported() {
        return true;
    }

    @Override
    public String mergeJavaFile(String newFileSource, File existingFile, String[] javadocTags, String fileEncoding)
        throws ShellException{
        try {
            String ret = new JavaFileMerger().getNewJavaFile(newFileSource, existingFile.getPath());
            return ret;
        } catch (FileNotFoundException e) {
        }
        return null;
    }
}
public static class JavaFileMerger{
    public String getNewJavaFile(String newFileSource, String existingFileFullPath) throws FileNotFoundException {
        CompilationUnit newCompilationUnit = JavaParser.parse(newFileSource);
        CompilationUnit existingCompilationUnit = JavaParser.parse(new File(existingFileFullPath));
        return mergerFile(newCompilationUnit,existingCompilationUnit);
    }

    public String mergerFile(CompilationUnit newCompilationUnit,CompilationUnit existingCompilationUnit){

        StringBuilder sb = new StringBuilder(newCompilationUnit.getPackageDeclaration().get().toString());
        newCompilationUnit.removePackageDeclaration();

        //合并imports
        NodeList<ImportDeclaration> imports = newCompilationUnit.getImports();
        imports.addAll(existingCompilationUnit.getImports());
        Set importSet = new HashSet<ImportDeclaration>();
        importSet.addAll(imports);

        NodeList<ImportDeclaration> newImports = new NodeList<>();
        newImports.addAll(importSet);
        newCompilationUnit.setImports(newImports);
        for (ImportDeclaration i:newCompilationUnit.getImports()) {
            sb.append(i.toString());
        }
        newLine(sb);
        NodeList<TypeDeclaration<?>> types = newCompilationUnit.getTypes();
        NodeList<TypeDeclaration<?>> oldTypes = existingCompilationUnit.getTypes();

        for (int i = 0;i<types.size();i++) {
            //截取Class
            String classNameInfo = types.get(i).toString().substring(0, types.get(i).toString().indexOf("{")+1);
            sb.append(classNameInfo);
            newLine(sb);
            newLine(sb);
            //合并fields
            List<FieldDeclaration> fields = types.get(i).getFields();
            List<FieldDeclaration> oldFields = oldTypes.get(i).getFields();
            for (FieldDeclaration f: fields){
                sb.append(f.toString());
                newLine(sb);
                newLine(sb);
            }

            for (FieldDeclaration f: oldFields){
                boolean flag = true;
                for (String tag : MergeConstants.OLD_ELEMENT_TAGS) {
                    if (f.toString().contains(tag)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    sb.append(f.toString());
                    newLine(sb);
                    newLine(sb);
                }
            }

            //合并methods
            List<MethodDeclaration> methods = types.get(i).getMethods();
            List<MethodDeclaration> existingMethods = oldTypes.get(i).getMethods();
            for (MethodDeclaration f: methods){
                sb.append(f.toString());
                newLine(sb);
                newLine(sb);
            }
            for (MethodDeclaration m:existingMethods){
                boolean flag = true;
                for (String tag : MergeConstants.OLD_ELEMENT_TAGS) {
                    if (m.toString().contains(tag)) {
                        flag = false;
                        break;
                    }
                }
                if (flag){
                    sb.append(m.toString());
                    newLine(sb);
                    newLine(sb);
                }
            }

            //判断是否有内部类
            types.get(i).getChildNodes();
            for (Node n:types.get(i).getChildNodes()){
                if (n.toString().contains("static class")){
                    sb.append(n.toString());
                }
                if (n.toString().contains("enum")) {
                    sb.append(n.toString());
                }
            }

        }

        return sb.append(System.getProperty("line.separator")+"}").toString();
    }
}

参考链接https://blog.youkuaiyun.com/w980994974/article/details/76904587

代码稍有改动,

1. 继承类相关代码更新

2.自动生成的java model文件成员变量附近merge,参考代码新老全部合并,出现两份同样代码,改为老的java file 有了就不合并了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值