本篇文章小编给大家分享一下解决mybatis-generator生成器添加类注释方法无效问题代码,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
解决办法如下
先看效果
import org.springframework.stereotype.Component; /** *我这里需求是自动添加注解,也可以看下方生成注释,导包等操作 */ @Component("tableName") public interface tableNameMapper { ... }
1、继承PluginAdapter
package com.**.***.generate; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; import org.mybatis.generator.api.dom.java.Interface; import org.mybatis.generator.api.dom.java.TopLevelClass; import java.util.List; /** * Created by JGMa on 2019-07-17 **/ public class BaseMapperGeneratorPlugin extends PluginAdapter { @Override public boolean validate(Listwarnings) { return true; } /** * 生成mapper接口 */ @Override public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType("Mapper<" + introspectedTable.getBaseRecordType() + ">"); FullyQualifiedJavaType imp = new FullyQualifiedJavaType( "com.github.abel533.mapper.Mapper"); FullyQualifiedJavaType imp2 = new FullyQualifiedJavaType( "org.springframework.stereotype.Component"); /** * extends MybatisBaseMapper */ interfaze.addSuperInterface(fqjt); /** * 导包 */ interfaze.addImportedType(imp); interfaze.addImportedType(imp2); /** * 清楚原有方法 */ interfaze.getMethods().clear(); //interfaze.getAnnotations().clear(); // 根据业务自己灵活发挥,本次需要spring注入 interfaze.addAnnotation("@Component("" + introspectedTable.getFullyQualifiedTable() + "")"); return true; } }
com.github.abel533.mapper.Mapper是一个通用的mapper有兴趣的也可以去看,下面给出pom
com.github.abel533 mapper 2.3.4
2、xml中配置插件
3、启动(请使用此种方式运行逆向工程)
import org.mybatis.generator.api.ShellRunner; /** * Created by JGMa on 2019-07-15 **/ public class StartUp { public static void main(String[] args) { args = new String[] { "-configfile", "D:mybatis-generator-config.xml", "-overwrite" }; ShellRunner.main(args); } }
如果找不到shellRunner需要导包
org.mybatis.generator mybatis-generator-core 1.3.7