SerializablePlugin.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.zheng.common.plugin;
  2. import org.mybatis.generator.api.IntrospectedTable;
  3. import org.mybatis.generator.api.PluginAdapter;
  4. import org.mybatis.generator.api.dom.java.*;
  5. import java.util.List;
  6. import java.util.Properties;
  7. /**
  8. * Example类和model类实现序列化插件
  9. * Created by shuzheng on 2017/1/1.
  10. */
  11. public class SerializablePlugin extends PluginAdapter {
  12. private FullyQualifiedJavaType serializable = new FullyQualifiedJavaType("java.io.Serializable");
  13. private FullyQualifiedJavaType gwtSerializable = new FullyQualifiedJavaType("com.google.gwt.user.client.rpc.IsSerializable");
  14. private boolean addGWTInterface;
  15. private boolean suppressJavaInterface;
  16. public SerializablePlugin() {
  17. }
  18. @Override
  19. public boolean validate(List<String> warnings) {
  20. return true;
  21. }
  22. @Override
  23. public void setProperties(Properties properties) {
  24. super.setProperties(properties);
  25. this.addGWTInterface = Boolean.valueOf(properties.getProperty("addGWTInterface")).booleanValue();
  26. this.suppressJavaInterface = Boolean.valueOf(properties.getProperty("suppressJavaInterface")).booleanValue();
  27. }
  28. @Override
  29. public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
  30. this.makeSerializable(topLevelClass, introspectedTable);
  31. return true;
  32. }
  33. @Override
  34. public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
  35. this.makeSerializable(topLevelClass, introspectedTable);
  36. return true;
  37. }
  38. @Override
  39. public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
  40. this.makeSerializable(topLevelClass, introspectedTable);
  41. return true;
  42. }
  43. protected void makeSerializable(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
  44. if(this.addGWTInterface) {
  45. topLevelClass.addImportedType(this.gwtSerializable);
  46. topLevelClass.addSuperInterface(this.gwtSerializable);
  47. }
  48. if(!this.suppressJavaInterface) {
  49. topLevelClass.addImportedType(this.serializable);
  50. topLevelClass.addSuperInterface(this.serializable);
  51. Field field = new Field();
  52. field.setFinal(true);
  53. field.setInitializationString("1L");
  54. field.setName("serialVersionUID");
  55. field.setStatic(true);
  56. field.setType(new FullyQualifiedJavaType("long"));
  57. field.setVisibility(JavaVisibility.PRIVATE);
  58. this.context.getCommentGenerator().addFieldComment(field, introspectedTable);
  59. topLevelClass.addField(field);
  60. }
  61. }
  62. /**
  63. * 添加给Example类序列化的方法
  64. * @param topLevelClass
  65. * @param introspectedTable
  66. * @return
  67. */
  68. @Override
  69. public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,IntrospectedTable introspectedTable){
  70. makeSerializable(topLevelClass, introspectedTable);
  71. for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
  72. if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) {
  73. innerClass.addSuperInterface(serializable);
  74. }
  75. if ("Criteria".equals(innerClass.getType().getShortName())) {
  76. innerClass.addSuperInterface(serializable);
  77. }
  78. if ("Criterion".equals(innerClass.getType().getShortName())) {
  79. innerClass.addSuperInterface(serializable);
  80. }
  81. }
  82. return true;
  83. }
  84. }