0

我为 mybatis 生成器编写了一个插件,但是在扩展插件方法的 clientGenerated 中不起作用。请帮帮我~^_^
代码在下面:


public class MapperAnnotationPlugin extends PluginAdapter {
    private final static Map<String, String> ANNOTATION_IMPORTS;
    static {
        ANNOTATION_IMPORTS = new HashMap<>();
        ANNOTATION_IMPORTS.put("@Mapper", "org.apache.ibatis.annotations.Mapper");
        ANNOTATION_IMPORTS.put("@Repository", "org.springframework.stereotype.Repository");
    }
    private List<String> annotationList;
    @Override
    public void initialized(IntrospectedTable introspectedTable) {
        super.initialized(introspectedTable);
        this.annotationList = new ArrayList<>();
        Properties properties = this.getProperties();
        boolean findMapper = false;
        for (Object key : properties.keySet()) {
            String keyStr = key.toString().trim();
            if (keyStr.startsWith("@Mapper")) {
                findMapper = true;
            }

            if (StringUtility.isTrue(properties.getProperty(key.toString()))) {
                annotationList.add(keyStr);
            }
        }
        if (!findMapper) {
            annotationList.add(0, "@Mapper");
        }
    }

    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
        super.clientGenerated(interfaze, introspectedTable);
        for (String annotation : annotationList) {
            if ("@Mapper".equals(annotation)) {
                if (introspectedTable.getTargetRuntime() == IntrospectedTable.TargetRuntime.MYBATIS3) {
                    interfaze.addImportedType(new FullyQualifiedJavaType(ANNOTATION_IMPORTS.get(annotation)));
                    interfaze.addAnnotation(annotation);
                }
            } else if (Objects.nonNull(ANNOTATION_IMPORTS.get(annotation))) {
                logger.info(PluginConst.TEACHEE_PLUGIN + "添加" + annotation);
                interfaze.addImportedType(new FullyQualifiedJavaType(ANNOTATION_IMPORTS.get(annotation)));
                interfaze.addAnnotation(annotation);
            }
        }
        return true;
    }
}

在第二种方法中,在调试中,它没有采用这种方法,所以下一步我该怎么办

4

1 回答 1

0

当我为 mybatis-generator-plugin 制作插件时同样的问题。clientGenerated 的方法没有回调。这是我的方式,从另一个包含接口对象的方法调用。

@Override
    public boolean clientCountByExampleMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        this.clientGenerated(interfaze, null, introspectedTable);
        return false;
    }

generator-core/generator-plugin 版本都是 1.4.0,现在可以正常工作了。

https://github.com/mybatis/generator/releases/tag/mybatis-generator-1.4.0

于 2021-01-07T08:34:50.957 回答