我是靠谱客的博主 淡定钢铁侠,这篇文章主要介绍MyBatis Generator插件之SerializablePlugin,现在分享给大家,希望可以做个参考。

复制代码
1
org.mybatis.generator.plugins.SerializablePlugin
复制代码
1
复制代码
1
在generatorConfig.xml中加上配置:
复制代码
1
复制代码
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

运行MBG,生成Userinfo类,我们发现和不加SerializablePlugin插件之前生成的类相比较区别如下:
复制代码
1
2
3
4
5
6
7
8
9
复制代码
public class Userinfo implements Serializable {
	......
    private static final long serialVersionUID = 1L;	
	......	
}

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

继承PluginAdapter;

2.

复制代码
1
2
3
4
5
6
7
8
9
10
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$ 实例化 }

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

复制代码
1
7.
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
复制代码
    @Override
    public boolean modelRecordWithBLOBsClassGenerated(
            TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        makeSerializable(topLevelClass, introspectedTable);
        return true;
    }
复制代码
调用了makeSerializable方法给RecordWithBLOBsClass添加序列化接口

8.接下来看看具体的实现方法
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
复制代码
    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);
        }
    }

复制代码
1
复制代码
1
复制代码
1
复制代码
1

最后

以上就是淡定钢铁侠最近收集整理的关于MyBatis Generator插件之SerializablePlugin的全部内容,更多相关MyBatis内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部