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);
}
}
很多时候我们创建的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);
}
}
相关文章推荐
- MyBatis Generator系列(七)----MyBatis Generator Plugin插件之MapperConfigPlugin
- MyBatis Generator系列(五)----MyBatis Generator Plugin插件之ToStringPlugin
- 在IDEA下,使用maven的mybatis-generator-maven-plugin插件自动生成实体类
- MyBatis Generator插件之ToStringPlugin
- mybatis3-generator-plugin插件地址
- mybatis-generator-maven-plugin插件自动生成代码的配置方法
- MyBatis Generator插件之MapperConfigPlugin
- MyBatis Generator系列(八)----MyBatis Generator自定义插件实现自定义Mapper
- mybatis-generator的maven插件使用异常(mybatis-generator-maven-plugin):generate failed: Exception getting JDBC Driver
- mybatis (Ibatis ibator) generator - 1 mysql 分页插件(page/paging/pagination plugin)
- mybatis-generator-maven-plugin 插件扩展 增加自定义方法
- IDEA mybatis-generator-maven-plugin 插件的使用
- MyBatis插件的使用:mybatis-generator、mybatis-plugin、mybatis-pagehelper
- 在IDEA下,使用maven的mybatis-generator-maven-plugin插件自动生成实体类
- IntelliJ IDEA下使用 mybatis-generator-maven-plugin插件完成Mybatis逆向工程
- MyBatis代码实例系列-08:MyBatisGenerator插件及扩展(中文注释和Mapper重命名为Dao)
- eclipse中mybatis generator插件的安装及使用
- mybatis 之 generator插件使用 集成到了eclipse 自动生成实体,example,mapper
- Jenkins进阶系列之——04Publish Over FTP Plugin插件
- mybatis-generator-meven-plugin