您的位置:首页 > 其它

MyBatis Generator系列(六)----MyBatis Generator Plugin插件之SerializablePlugin

2017-12-02 16:24 429 查看
一、SerializablePlugin插件简介

很多时候我们创建的bean需要实现序列化,所有要实现Serializable类,所以MyBatis Generator为我们提供了SerializablePlugin插件用来在代码生成时实现序列化。

二、SerializablePlugin使用

在generatorConfig.xml中配置SerializablePlugin插件:

<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />



然后运行MBG,查看生成的代码



会发现实现了Serializable接口,并增加了private static final long serialVersionUID = 1L;

三、SerializablePlugin源码

SerializablePlugin的代码:

package org.mybatis.generator.plugins;

import java.util.List;
import java.util.Properties;

import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.TopLevelClass;

/**
* This plugin adds the java.io.Serializable marker interface to all generated
* model objects.
* <p>
* This plugin demonstrates adding capabilities to generated Java artifacts, and
* shows the proper way to add imports to a compilation unit.
* <p>
* Important: This is a simplistic implementation of serializable and does not
* attempt to do any versioning of classes.
*
* @author Jeff Butler
*
*/
public class SerializablePlugin extends PluginAdapter {

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$ 实例化
}

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;
}

/**
*
*@Title makeSerializable
*@Description: 添加序列化
*@Author fendo
*@Date 2017年12月2日 下午4:15:20
*@param topLevelClass
*@param introspectedTable
*@throws
*/
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);
}
}
}


首先是继承PluginAdapter,然后调用validate不需要参数,所以直接返回true,然后通过setProperties方法
@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参数,给成员变量赋值。然后通过modelBaseRecordClassGenerated

@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
makeSerializable(topLevelClass, introspectedTable);
return true;
}

调用makeSerializable方法,给BaeRecordClass添加序列化接口。

@Override
public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
makeSerializable(topLevelClass, introspectedTable);
return true;
}
通过modelPrimaryKeyClassGenerated调用makeSerializable方法给PrimaryKeyClass添加序列化接口。

@Override
public boolean modelRecordWithBLOBsClassGenerated(
TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
makeSerializable(topLevelClass, introspectedTable);
return true;
}

通过modelRecordWithBLOBsClassGenerated调用makeSerializable方法给RecordWithBLOBsClass添加序列化接口。

添加序列化方法如下:

/**
*
*@Title makeSerializable
*@Description: 添加序列化
*@Author fendo
*@Date 2017年12月2日 下午4:15:20
*@param topLevelClass
*@param introspectedTable
*@throws
*/
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: