MyBatis Generator插件之SerializablePlugin

org.mybatis.generator.plugins.SerializablePlugin

在generatorConfig.xml中加上配置:
  1. <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />  

运行MBG,生成Userinfo类,我们发现和不加SerializablePlugin插件之前生成的类相比较区别如下:
  1. public class Userinfo implements Serializable {  
  2.     ......  
  3.     private static final long serialVersionUID = 1L;      
  4.     ......    
  5. }  

区别1:实现了Serializable接口

区别2:增加了private static final long serialVersionUID = 1L;

下面我们看SerializablePlugin的代码:
1.
  1. public class SerializablePlugin extends PluginAdapter  

继承PluginAdapter;

2.

  1. private FullyQualifiedJavaType serializable;    //对应java.io.Serializable的java类型    
  2. private FullyQualifiedJavaType gwtSerializable; //对应com.google.gwt.user.client.rpc.IsSerializable的java类型      
  3. private boolean addGWTInterface;                //是否实现com.google.gwt.user.client.rpc.IsSerializable接口      
  4. private boolean suppressJavaInterface;          //是否实现java.io.Serializable接口      
  5.   
  6. public SerializablePlugin() {          
  7.     super();          
  8.     serializable = new FullyQualifiedJavaType("java.io.Serializable"); //$NON-NLS-1$   实例化          
  9.     gwtSerializable = new FullyQualifiedJavaType("com.google.gwt.user.client.rpc.IsSerializable"); //$NON-NLS-1$  实例化    
  10. }  

成员变量和构造方法,详细看代码注释。

3.
  1. public boolean validate(List<String> warnings) {  
  2.     // this plugin is always valid  
  3.     return true;  
  4. }  
不需要参数,所以直接返回true


4.
  1. @Override  
  2. public void setProperties(Properties properties) {  
  3.     super.setProperties(properties);  
  4.     addGWTInterface = Boolean.valueOf(properties.getProperty("addGWTInterface")); //$NON-NLS-1$  
  5.     suppressJavaInterface = Boolean.valueOf(properties.getProperty("suppressJavaInterface")); //$NON-NLS-1$  
  6. }  
获取addGWTInterface 和 suppressJavaInterface参数,给成员变量赋值。


5.
  1. @Override  
  2. public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,  
  3.         IntrospectedTable introspectedTable) {  
  4.     makeSerializable(topLevelClass, introspectedTable);  
  5.     return true;  
  6. }  
调用了makeSerializable方法给BaeRecordClass添加序列化接口


6.
  1. @Override  
  2. public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,  
  3.         IntrospectedTable introspectedTable) {  
  4.     makeSerializable(topLevelClass, introspectedTable);  
  5.     return true;  
  6. }  
调用了makeSerializable方法给PrimaryKeyClass添加序列化接口

7.
  1. @Override  
  2. public boolean modelRecordWithBLOBsClassGenerated(  
  3.         TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {  
  4.     makeSerializable(topLevelClass, introspectedTable);  
  5.     return true;  
  6. }  
调用了makeSerializable方法给RecordWithBLOBsClass添加序列化接口

8.接下来看看具体的实现方法

  1. protected void makeSerializable(TopLevelClass topLevelClass,  
  2.         IntrospectedTable introspectedTable) {  
  3.     if (addGWTInterface) {  //是否要实现com.google.gwt.user.client.rpc.IsSerializable接口  
  4.         topLevelClass.addImportedType(gwtSerializable); //import com.google.gwt.user.client.rpc.IsSerializable;  
  5.         topLevelClass.addSuperInterface(gwtSerializable);//实现接口  
  6.     }  
  7.       
  8.     if (!suppressJavaInterface) { //不禁止实现java.io.Serializable  
  9.         topLevelClass.addImportedType(serializable);  //import java.io.Serializable;  
  10.         topLevelClass.addSuperInterface(serializable); //实现java.io.Serializable接口  
  11.   
  12.         //添加serialVersionUID字段  
  13.         //最终生成代码private static final long serialVersionUID = 1L;  
  14.         Field field = new Field();  
  15.         field.setFinal(true);  //添加final修饰  
  16.         field.setInitializationString("1L"); //$NON-NLS-1$  赋值为1L  
  17.         field.setName("serialVersionUID"); //$NON-NLS-1$   设置字段名称为serialVersionUID  
  18.         field.setStatic(true); //添加static关键字  
  19.         field.setType(new FullyQualifiedJavaType("long")); //$NON-NLS-1$  声明类型  
  20.         field.setVisibility(JavaVisibility.PRIVATE);  //声明为私有  
  21.         context.getCommentGenerator().addFieldComment(field, introspectedTable);  //生成注解  
  22. n style="white-space:pre;">     </span>  
  23.         //把拼装好的方法DOM添加到topLevelClass中,完成接口的实现和字段的添加  
  24.         topLevelClass.addField(field);  
  25.     }  
  26. }  





posted @ 2018-02-01 10:46  星朝  阅读(2508)  评论(0)    收藏  举报