org.mybatis.generator.plugins.SerializablePlugin

在generatorConfig.xml中加上配置:

  1. <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<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. }
public class Userinfo implements Serializable {
......
private static final long serialVersionUID = 1L;
......

}

区别1:实现了Serializable接口

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

下面我们看SerializablePlugin的代码:
1.

  1. public class SerializablePlugin extends PluginAdapter
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. public SerializablePlugin() {
  6. super();
  7. serializable = new FullyQualifiedJavaType("java.io.Serializable"); //$NON-NLS-1$   实例化
  8. gwtSerializable = new FullyQualifiedJavaType("com.google.gwt.user.client.rpc.IsSerializable"); //$NON-NLS-1$  实例化
  9. }
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.

  1. public boolean validate(List<String> warnings) {
  2. // this plugin is always valid
  3. return true;
  4. }
    public boolean validate(List<String> warnings) {
    // this plugin is always valid
return true;
}</pre>不需要参数,所以直接返回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. }
    @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$
}</pre>获取addGWTInterface 和 suppressJavaInterface参数,给成员变量赋值。

5.

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

6.

  1. @Override
  2. public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
  3. IntrospectedTable introspectedTable) {
  4. makeSerializable(topLevelClass, introspectedTable);
  5. return true;
  6. }
    @Override
public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
makeSerializable(topLevelClass, introspectedTable);
return true;
}</pre><pre style="background-color:rgb(255,255,255);font-family:'宋体';">调用了makeSerializable方法给PrimaryKeyClass添加序列化接口
7.

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

}

}





                </div>

MyBatis Generator插件之SerializablePlugin的更多相关文章

  1. Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数 (转)

    众所周知,Mybatis本身没有提供基于数据库方言的分页功能,而是基于JDBC的游标分页,很容易出现性能问题.网上有很多分页的解决方案,不外乎是基于Mybatis本机的插件机制,通过拦截Sql做分页. ...

  2. Myeclipse2014添加mybatis generator插件

    Myeclipse2014把mybatis generator插件直接放在dropins文件夹下,重启后不能成功安装mybatis插件. 既然离线安装不成功,可以选择在线安装 1.选择 Help-&g ...

  3. mybatis generator 插件安装及使用

    现在Mybatis特别火,但是在开发中却要经常写实体类和配置文件,会不会特别烦人,所以可以利用Mybatis的代码生成插件来生成这部分代码: 1,打开eclipse,点击Help>Softwar ...

  4. Eclipse 使用mybatis generator插件自动生成代码

    Eclipse 使用mybatis generator插件自动生成代码 标签: mybatis 2016-12-07 15:10 5247人阅读 评论(0) 收藏 举报 .embody{ paddin ...

  5. Eclipse MyBatis Generator插件安装

    目录 Eclipse MyBatis Generator插件安装 Eclipse MyBatis Generator插件安装 1.进入Eclipse Marketplace [Help] -> ...

  6. Mybatis-Generator_学习_02_使用Mapper专用的MyBatis Generator插件

    源码见:https://github.com/shirayner/tk-mybatis-generator 一.要点 二.具体实现 1.项目结构 2.配置 pm.xml <?xml versio ...

  7. Mybatis Generator插件和PageHelper使用

    最近,开始接触web项目开发,项目使用springboot和mybatis,以前一直以为开发过程中实体类,mybatis的xml文件都需要自己手动的去创建. 同事推荐说Mybatis Generato ...

  8. mybatis generator插件系列--分页插件

    1.首先定义分页插件 MysqlPagePlugin.java package com.demo.mybatis.plugin; import org.mybatis.generator.api.Co ...

  9. mybatis generator插件系列--lombok插件 (减少百分之九十bean代码)

    经常使用mybatis generator生成代码的你 有没有因为生成的getter/setter而烦恼呢? 有没有生成后又手动加toString/hashCode/Equals方法呢? 有没有改一个 ...

随机推荐

  1. POJ——T 2796 Feel Good

    http://poj.org/problem?id=2796 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15375   ...

  2. 洛谷——P1525 关押罪犯

    https://www.luogu.org/problem/show?pid=1525 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间 ...

  3. gdal读写图像分块处理

    转自赵文原文 gdal读写图像分块处理(精华版) Review: 用gdal,感觉还不如直接用C++底层函数对遥感数据进行处理.因为gdal进行太多封装,如果你仅仅只是Geotif等格式进行处理,IO ...

  4. 控制面板项 .cpl 文件说明

    控制面板项 .cpl 文件说明 appwiz.cpl               程序和功能.卸载或更改程序 bthprops.cpl             蓝牙控制面板 desk.cpl      ...

  5. Shelled-out Commands In Golang

    http://nathanleclaire.com/blog/2014/12/29/shelled-out-commands-in-golang/ Shelled-out Commands In Go ...

  6. 基于面向对象js的弹窗的组件的开发案例

    var aInput = document.getElementsByTagName("input"); 2 aInput[0].onclick = function() { 3 ...

  7. Linux启动(续)

    runlevel (启动级别):    查看命令 :who -r 或 runlevel         0:halt 关机         1:单用户模式,直接以管理员身份登录,不需要密码       ...

  8. 79.cgi硬盘查询个人信息

    运行截图: 把cgi编码转为char*类型 //把cgi编码转为char*类型 char* change(char *str) { //分配内存 ); //x是tempstr的下标,y是str的下标 ...

  9. 软件——机器学习与Python,Python3的输出与输入

    输出 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print('hello, world') p ...

  10. 【CS Round #43 B】Rectangle Partition

    [链接]https://csacademy.com/contest/round-43/task/rectangle-partition/ [题意] 水题 [题解] 横着过去,把相邻的边的宽记录下来. ...