123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package com.conpany.project;
- import org.mybatis.generator.api.IntrospectedTable;
- import org.mybatis.generator.api.PluginAdapter;
- import org.mybatis.generator.api.dom.java.*;
- import java.util.List;
- import java.util.Properties;
- /**
- * 分布式开发的话,Example对象也必须要序列化
- * 扩展一个 mybatis generator 插件,用于不仅仅在生成的实体类 还有 *Example 类都序列化
- * @author alexgaoyh
- *
- */
- public class SerializablePlugin extends PluginAdapter {
-
- private FullyQualifiedJavaType serializable;
- private FullyQualifiedJavaType gwtSerializable;
- private boolean addGWTInterface;
- private boolean suppressJavaInterface;
-
- public SerializablePlugin() {
- super();
- serializable = new FullyQualifiedJavaType("java.io.Serializable"); //$NON-NLS-1$
- gwtSerializable = new FullyQualifiedJavaType("com.google.gwt.user.client.rpc.IsSerializable"); //$NON-NLS-1$
- }
-
- public boolean validate(List<String> warnings) {
- // this plugin is always valid
- return true;
- }
-
- @Override
- public void setProperties(Properties properties) {
- super.setProperties(properties);
- addGWTInterface = Boolean.valueOf(properties.getProperty("addGWTInterface")); //$NON-NLS-1$
- suppressJavaInterface = Boolean.valueOf(properties.getProperty("suppressJavaInterface")); //$NON-NLS-1$
- }
-
- @Override
- public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
- IntrospectedTable introspectedTable) {
- makeSerializable(topLevelClass, introspectedTable);
- return true;
- }
-
- @Override
- public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
- IntrospectedTable introspectedTable) {
- makeSerializable(topLevelClass, introspectedTable);
- return true;
- }
-
- @Override
- public boolean modelRecordWithBLOBsClassGenerated(
- TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
- makeSerializable(topLevelClass, introspectedTable);
- return true;
- }
-
- /**
- * 添加给Example类序列化的方法
- * @param topLevelClass
- * @param introspectedTable
- * @return
- */
- @Override
- public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,IntrospectedTable introspectedTable){
- makeSerializable(topLevelClass, introspectedTable);
-
- for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
- if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
- innerClass.addSuperInterface(serializable);
- }
- if ("Criteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
- innerClass.addSuperInterface(serializable);
- }
- if ("Criterion".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
- innerClass.addSuperInterface(serializable);
- }
- }
-
- return true;
- }
-
- protected void makeSerializable(TopLevelClass topLevelClass,
- IntrospectedTable introspectedTable) {
- if (addGWTInterface) {
- topLevelClass.addImportedType(gwtSerializable);
- topLevelClass.addSuperInterface(gwtSerializable);
- }
-
- if (!suppressJavaInterface) {
- topLevelClass.addImportedType(serializable);
- topLevelClass.addSuperInterface(serializable);
-
- Field field = new Field();
- field.setFinal(true);
- field.setInitializationString("1L"); //$NON-NLS-1$
- field.setName("serialVersionUID"); //$NON-NLS-1$
- field.setStatic(true);
- field.setType(new FullyQualifiedJavaType("long")); //$NON-NLS-1$
- field.setVisibility(JavaVisibility.PRIVATE);
- context.getCommentGenerator().addFieldComment(field, introspectedTable);
-
- topLevelClass.addField(field);
- }
- }
- }
|