mybatis clientGenerated couldn't call back

111 Views Asked by At

i code a plugin for mybatis generator, but in clientGenerated of extends plugin method doesn't work. please helpe me ~ ^_^
code is in under:


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;
    }
}

in second method, in debug, it had not go this method, so what could i do in next step

1

There are 1 best solutions below

1
bwhyman On

Same problem when I maked a plugin for mybatis-generator-plugin. The method of clientGenerated didnot be callback. It is my way, call from another method which include Interface object.

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

Both generator-core/generator-plugin version is 1.4.0, work fine now.

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