MyBatis Generator插件之SerializablePlugin

org.mybatis.generator.plugins.SerializablePlugin
 
  
在generatorConfig.xml中加上配置:
 
  

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

区别1:实现了Serializable接口
区别2:增加了private static final long serialVersionUID = 1L;
 
  
下面我们看SerializablePlugin的代码:
1.
 
  
public class SerializablePlugin extends PluginAdapter

继承PluginAdapter;

2.

private FullyQualifiedJavaType serializable;    //对应java.io.Serializable的java类型  
private FullyQualifiedJavaType gwtSerializable; //对应com.google.gwt.user.client.rpc.IsSerializable的java类型    
private boolean addGWTInterface;                //是否实现com.google.gwt.user.client.rpc.IsSerializable接口    
private boolean suppressJavaInterface;          //是否实现java.io.Serializable接口    

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$  实例化  
}
成员变量和构造方法,详细看代码注释。
3.
 
  
    public boolean validate(List warnings) {
        // this plugin is always valid
        return true;
    }
不需要参数,所以直接返回true
 
  
4.
 
  
    @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$
    }
获取addGWTInterface 和 suppressJavaInterface参数,给成员变量赋值。
 
  
5.
 
  
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable) {
        makeSerializable(topLevelClass, introspectedTable);
        return true;
    }
调用了makeSerializable方法给BaeRecordClass添加序列化接口
 
  
6.
 
  
    @Override
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable) {
        makeSerializable(topLevelClass, introspectedTable);
        return true;
    }
调用了makeSerializable方法给PrimaryKeyClass添加序列化接口

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

8.接下来看看具体的实现方法
 
  
    protected void makeSerializable(TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable) {
        if (addGWTInterface) {  //是否要实现com.google.gwt.user.client.rpc.IsSerializable接口
            topLevelClass.addImportedType(gwtSerializable); //import com.google.gwt.user.client.rpc.IsSerializable;
            topLevelClass.addSuperInterface(gwtSerializable);//实现接口
        }
        
        if (!suppressJavaInterface) { //不禁止实现java.io.Serializable
            topLevelClass.addImportedType(serializable);  //import java.io.Serializable;
            topLevelClass.addSuperInterface(serializable); //实现java.io.Serializable接口

            //添加serialVersionUID字段
            //最终生成代码private static final long serialVersionUID = 1L;
            Field field = new Field();
            field.setFinal(true);  //添加final修饰
            field.setInitializationString("1L"); //$NON-NLS-1$  赋值为1L
            field.setName("serialVersionUID"); //$NON-NLS-1$   设置字段名称为serialVersionUID
            field.setStatic(true); //添加static关键字
            field.setType(new FullyQualifiedJavaType("long")); //$NON-NLS-1$  声明类型
            field.setVisibility(JavaVisibility.PRIVATE);  //声明为私有
            context.getCommentGenerator().addFieldComment(field, introspectedTable);  //生成注解
		
            //把拼装好的方法DOM添加到topLevelClass中,完成接口的实现和字段的添加
            topLevelClass.addField(field);
        }
    }

 
  
 
  
 
  
 
 

你可能感兴趣的:(mybatis)