MyBatis Generator插件之SerializablePlugin
org.mybatis.generator.plugins.SerializablePlugin
在generatorConfig.xml中加上配置:
- <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
运行MBG,生成Userinfo类,我们发现和不加SerializablePlugin插件之前生成的类相比较区别如下:
- public class Userinfo implements Serializable {
- ......
- private static final long serialVersionUID = 1L;
- ......
- }
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
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$ 实例化
- }
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<String> warnings) {
- // this plugin is always valid
- return true;
- }
public boolean validate(List<String> warnings) {// this plugin is always valid
return true;
}</pre>不需要参数,所以直接返回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$
- }
@Overridepublic 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.
- @Override
- public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
- IntrospectedTable introspectedTable) {
- makeSerializable(topLevelClass, introspectedTable);
- return true;
- }
@Overridepublic boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
makeSerializable(topLevelClass, introspectedTable);
return true;
}</pre>调用了makeSerializable方法给BaeRecordClass添加序列化接口
6.
- @Override
- public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
- IntrospectedTable introspectedTable) {
- makeSerializable(topLevelClass, introspectedTable);
- return true;
- }
@Overridepublic 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.
- @Override
- public boolean modelRecordWithBLOBsClassGenerated(
- TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
- makeSerializable(topLevelClass, introspectedTable);
- return true;
- }
@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); //生成注解
- n style="white-space:pre;"> </span>
- //把拼装好的方法DOM添加到topLevelClass中,完成接口的实现和字段的添加
- topLevelClass.addField(field);
- }
- }
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的更多相关文章
- Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数 (转)
众所周知,Mybatis本身没有提供基于数据库方言的分页功能,而是基于JDBC的游标分页,很容易出现性能问题.网上有很多分页的解决方案,不外乎是基于Mybatis本机的插件机制,通过拦截Sql做分页. ...
- Myeclipse2014添加mybatis generator插件
Myeclipse2014把mybatis generator插件直接放在dropins文件夹下,重启后不能成功安装mybatis插件. 既然离线安装不成功,可以选择在线安装 1.选择 Help-&g ...
- mybatis generator 插件安装及使用
现在Mybatis特别火,但是在开发中却要经常写实体类和配置文件,会不会特别烦人,所以可以利用Mybatis的代码生成插件来生成这部分代码: 1,打开eclipse,点击Help>Softwar ...
- Eclipse 使用mybatis generator插件自动生成代码
Eclipse 使用mybatis generator插件自动生成代码 标签: mybatis 2016-12-07 15:10 5247人阅读 评论(0) 收藏 举报 .embody{ paddin ...
- Eclipse MyBatis Generator插件安装
目录 Eclipse MyBatis Generator插件安装 Eclipse MyBatis Generator插件安装 1.进入Eclipse Marketplace [Help] -> ...
- Mybatis-Generator_学习_02_使用Mapper专用的MyBatis Generator插件
源码见:https://github.com/shirayner/tk-mybatis-generator 一.要点 二.具体实现 1.项目结构 2.配置 pm.xml <?xml versio ...
- Mybatis Generator插件和PageHelper使用
最近,开始接触web项目开发,项目使用springboot和mybatis,以前一直以为开发过程中实体类,mybatis的xml文件都需要自己手动的去创建. 同事推荐说Mybatis Generato ...
- mybatis generator插件系列--分页插件
1.首先定义分页插件 MysqlPagePlugin.java package com.demo.mybatis.plugin; import org.mybatis.generator.api.Co ...
- mybatis generator插件系列--lombok插件 (减少百分之九十bean代码)
经常使用mybatis generator生成代码的你 有没有因为生成的getter/setter而烦恼呢? 有没有生成后又手动加toString/hashCode/Equals方法呢? 有没有改一个 ...
随机推荐
- POJ——T 2796 Feel Good
http://poj.org/problem?id=2796 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 15375 ...
- 洛谷——P1525 关押罪犯
https://www.luogu.org/problem/show?pid=1525 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间 ...
- gdal读写图像分块处理
转自赵文原文 gdal读写图像分块处理(精华版) Review: 用gdal,感觉还不如直接用C++底层函数对遥感数据进行处理.因为gdal进行太多封装,如果你仅仅只是Geotif等格式进行处理,IO ...
- 控制面板项 .cpl 文件说明
控制面板项 .cpl 文件说明 appwiz.cpl 程序和功能.卸载或更改程序 bthprops.cpl 蓝牙控制面板 desk.cpl ...
- Shelled-out Commands In Golang
http://nathanleclaire.com/blog/2014/12/29/shelled-out-commands-in-golang/ Shelled-out Commands In Go ...
- 基于面向对象js的弹窗的组件的开发案例
var aInput = document.getElementsByTagName("input"); 2 aInput[0].onclick = function() { 3 ...
- Linux启动(续)
runlevel (启动级别): 查看命令 :who -r 或 runlevel 0:halt 关机 1:单用户模式,直接以管理员身份登录,不需要密码 ...
- 79.cgi硬盘查询个人信息
运行截图: 把cgi编码转为char*类型 //把cgi编码转为char*类型 char* change(char *str) { //分配内存 ); //x是tempstr的下标,y是str的下标 ...
- 软件——机器学习与Python,Python3的输出与输入
输出 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print('hello, world') p ...
- 【CS Round #43 B】Rectangle Partition
[链接]https://csacademy.com/contest/round-43/task/rectangle-partition/ [题意] 水题 [题解] 横着过去,把相邻的边的宽记录下来. ...