插件地址:(https://github.com/suyin58/mybatis-generator-tddl/blob/master/generator-plugin/src/main/java/com/toolplat/generator/plugins/LombokPlugin.java)
使用Mybatis的插件拓展(继承PluginAdapter),增加注解处理并去掉默认的getter和setter方法
public class LombokPlugin extends BasePlugin { @Override public boolean validate(List<String> warnings) { return true; } private final Collection<Annotations> annotations; /** * LombokPlugin constructor */ public LombokPlugin() { annotations = new LinkedHashSet<Annotations>(Annotations.values().length); } /** * Intercepts base record class generation * * @param topLevelClass * the generated base record class * @param introspectedTable * The class containing information about the table as * introspected from the database * @return always true */ @Override public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { addDataAnnotation(topLevelClass); return true; } /** * Intercepts primary key class generation * * @param topLevelClass * the generated primary key class * @param introspectedTable * The class containing information about the table as * introspected from the database * @return always true */ @Override public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { addDataAnnotation(topLevelClass); return true; } /** * Intercepts "record with blob" class generation * * @param topLevelClass * the generated record with BLOBs class * @param introspectedTable * The class containing information about the table as * introspected from the database * @return always true */ @Override public boolean modelRecordWithBLOBsClassGenerated( TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { addDataAnnotation(topLevelClass); return true; } /** * Prevents all getters from being generated. * See SimpleModelGenerator * * @param method * the getter, or accessor, method generated for the specified * column * @param topLevelClass * the partially implemented model class * @param introspectedColumn * The class containing information about the column related * to this field as introspected from the database * @param introspectedTable * The class containing information about the table as * introspected from the database * @param modelClassType * the type of class that the field is generated for */ @Override public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { return false; } /** * Prevents all setters from being generated * See SimpleModelGenerator * * @param method * the setter, or mutator, method generated for the specified * column * @param topLevelClass * the partially implemented model class * @param introspectedColumn * The class containing information about the column related * to this field as introspected from the database * @param introspectedTable * The class containing information about the table as * introspected from the database * @param modelClassType * the type of class that the field is generated for * @return always false */ @Override public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { return false; } /** * Adds the lombok annotations' imports and annotations to the class * * @param topLevelClass * the partially implemented model class */ private void addDataAnnotation(TopLevelClass topLevelClass) { for (Annotations annotation : annotations) { topLevelClass.addImportedType(annotation.javaType); topLevelClass.addAnnotation(annotation.name); } } @Override public void setProperties(Properties properties) { super.setProperties(properties); //@Data is default annotation annotations.add(Annotations.DATA); for (Map.Entry<Object, Object> entry : properties.entrySet()) { boolean isEnable = Boolean.parseBoolean(entry.getValue().toString()); if (isEnable) { String paramName = entry.getKey().toString().trim(); Annotations annotation = Annotations.getValueOf(paramName); if (annotation != null) { annotations.add(annotation); annotations.addAll(Annotations.getDependencies(annotation)); } } } } private enum Annotations { DATA("data", "@Data", "lombok.Data"), BUILDER("builder", "@Builder", "lombok.Builder"), SUPER_BUILDER("superBuilder", "@SuperBuilder", "lombok.experimental.SuperBuilder"), ALL_ARGS_CONSTRUCTOR("allArgsConstructor", "@AllArgsConstructor", "lombok.AllArgsConstructor"), NO_ARGS_CONSTRUCTOR("noArgsConstructor", "@NoArgsConstructor", "lombok.NoArgsConstructor"), TO_STRING("toString", "@ToString", "lombok.ToString"); private final String paramName; private final String name; private final FullyQualifiedJavaType javaType; Annotations(String paramName, String name, String className) { this.paramName = paramName; this.name = name; this.javaType = new FullyQualifiedJavaType(className); } private static Annotations getValueOf(String paramName) { for (Annotations annotation : Annotations.values()) { if (String.CASE_INSENSITIVE_ORDER.compare(paramName, annotation.paramName) == 0) { return annotation; } } return null; } private static Collection<Annotations> getDependencies(Annotations annotation) { if (annotation == ALL_ARGS_CONSTRUCTOR) { return Collections.singleton(NO_ARGS_CONSTRUCTOR); } else { return Collections.emptyList(); } } } }
配置文件中使用插件的时候,指定需要的Lombok注解(https://github.com/suyin58/mybatis-generator-tddl/blob/master/generator-test/src/test/resources/generatorConfigMyBatis3.xml)
<!-- lombok 插件 --> <plugin type="com.toolplat.generator.plugins.LombokPlugin"> <property name="data" value="true"/> <property name="builder" value="true"/> <property name="allArgsConstructor" value="true"/> <property name="noArgsConstructor" value="true"/> <property name="toString" value="true"/> </plugin>
生成的文件样例(https://github.com/suyin58/mybatis-generator-tddl/blob/master/generator-test/src/main/java/com/toolplat/demo/domain/TableOneUniqueKeyPO.java):
/** * 多个uk测试 */ @Data @Builder @NoArgsConstructor @AllArgsConstructor @ToString public class TableTwoUniqueKeyPO implements Serializable { private static final long serialVersionUID = 8946653058164L; /** * 多群主键 */ private Long orgId; /** * 多码 */ private String code; /** * id */ private Long id; /** * 创建时间 */ private Date gmtCreate; /** * 修改时间 */ private Date gmtModified; /** * 会话ID */ private String cid; }