问题描述
我为mybatis 生成器编写了一个插件,但是在clientGenerated 的extends 插件方法中不起作用。
请帮帮我~^_^
代码在:
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;
}
}
在第二种方法中,在调试中,它没有采用这种方法,那么下一步该怎么办
解决方法
当我为 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