博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyBatis Generator生成DAO——序列化
阅读量:4215 次
发布时间:2019-05-26

本文共 10581 字,大约阅读时间需要 35 分钟。

MyBatis Generator生成DAO 的时候,生成的类都是没有序列化的。

还以为要手工添加(开始是手工添加的委屈),今天遇到分页的问题,才发现生成的时候可以添加插件。既然分页可以有插件,序列化是不是也有呢。

果然SerializablePlugin,已经给我们提供好了。

马上高端大气了起来。每个model对象都乖乖的带上了Serializable接口。

无奈只有model对象是不够的,做分布式开发的话,Example对象也必须要序列化。

于是下载了SerializablePlugin的源码,model可以有,Example肯定也可以有。不出所料稍作修改就加上了。(直接用原来的源码添加了自己的代码)

import org.mybatis.generator.api.IntrospectedTable;import org.mybatis.generator.api.PluginAdapter;import org.mybatis.generator.api.dom.java.*;import java.util.List;import java.util.Properties;/** * Created by tiantao on 15-7-1. */public class SerializablePlugin extends PluginAdapter {    private FullyQualifiedJavaType serializable;    private FullyQualifiedJavaType gwtSerializable;    private boolean addGWTInterface;    private boolean suppressJavaInterface;    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
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; } /** * 添加给Example类序列化的方法 * @param topLevelClass * @param introspectedTable * @return */ @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,IntrospectedTable introspectedTable){ makeSerializable(topLevelClass, introspectedTable); return true; } protected void makeSerializable(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { if (addGWTInterface) { topLevelClass.addImportedType(gwtSerializable); topLevelClass.addSuperInterface(gwtSerializable); } if (!suppressJavaInterface) { topLevelClass.addImportedType(serializable); topLevelClass.addSuperInterface(serializable); Field field = new Field(); field.setFinal(true); field.setInitializationString("1L"); //$NON-NLS-1$ field.setName("serialVersionUID"); //$NON-NLS-1$ field.setStatic(true); field.setType(new FullyQualifiedJavaType("long")); //$NON-NLS-1$ field.setVisibility(JavaVisibility.PRIVATE); context.getCommentGenerator().addFieldComment(field, introspectedTable); topLevelClass.addField(field); } }}

哇咔咔,太好用了。Example都加上了。

不过问题还没有完,Example里还有内部类,如果不序列化还是会报错。

这次明显更刚才的套路不一样了。没有抱太大希望。

无意间发现了另一个插件类,也是包里自带的。发现了宝藏,这里竟然有对内部类的操作。

import java.util.List;import org.mybatis.generator.api.PluginAdapter;import org.mybatis.generator.api.IntrospectedColumn;import org.mybatis.generator.api.IntrospectedTable;import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;import org.mybatis.generator.api.dom.java.InnerClass;import org.mybatis.generator.api.dom.java.JavaVisibility;import org.mybatis.generator.api.dom.java.Method;import org.mybatis.generator.api.dom.java.Parameter;import org.mybatis.generator.api.dom.java.TopLevelClass;import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities;/** * This plugin demonstrates adding methods to the example class to enable * case-insensitive LIKE searches. It shows hows to construct new methods and * add them to an existing class. *  * This plugin only adds methods for String fields mapped to a JDBC character * type (CHAR, VARCHAR, etc.) *  * @author Jeff Butler *  */public class CaseInsensitiveLikePlugin extends PluginAdapter {    /**     *      */    public CaseInsensitiveLikePlugin() {        super();    }    public boolean validate(List
warnings) { return true; } @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { InnerClass criteria = null; // first, find the Criteria inner class for (InnerClass innerClass : topLevelClass.getInnerClasses()) { if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$ criteria = innerClass; break; } } if (criteria == null) { // can't find the inner class for some reason, bail out. return true; } for (IntrospectedColumn introspectedColumn : introspectedTable .getNonBLOBColumns()) { if (!introspectedColumn.isJdbcCharacterColumn() || !introspectedColumn.isStringColumn()) { continue; } Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.addParameter(new Parameter(introspectedColumn .getFullyQualifiedJavaType(), "value")); //$NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append(introspectedColumn.getJavaProperty()); sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); sb.insert(0, "and"); //$NON-NLS-1$ sb.append("LikeInsensitive"); //$NON-NLS-1$ method.setName(sb.toString()); method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance()); sb.setLength(0); sb.append("addCriterion(\"upper("); //$NON-NLS-1$ sb.append(Ibatis2FormattingUtilities .getAliasedActualColumnName(introspectedColumn)); sb.append(") like\", value.toUpperCase(), \""); //$NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.append("\");"); //$NON-NLS-1$ method.addBodyLine(sb.toString()); method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$ criteria.addMethod(method); } return true; }}
把原来的方法再优化一下下。

import org.mybatis.generator.api.IntrospectedTable;import org.mybatis.generator.api.PluginAdapter;import org.mybatis.generator.api.dom.java.*;import java.util.List;import java.util.Properties;/** * Created by tiantao on 15-7-1. */public class SerializablePlugin extends PluginAdapter {    private FullyQualifiedJavaType serializable;    private FullyQualifiedJavaType gwtSerializable;    private boolean addGWTInterface;    private boolean suppressJavaInterface;    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
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; } /** * 添加给Example类序列化的方法 * @param topLevelClass * @param introspectedTable * @return */ @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,IntrospectedTable introspectedTable){ makeSerializable(topLevelClass, introspectedTable); for (InnerClass innerClass : topLevelClass.getInnerClasses()) { if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$ innerClass.addSuperInterface(serializable); } if ("Criteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$ innerClass.addSuperInterface(serializable); } if ("Criterion".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$ innerClass.addSuperInterface(serializable); } } return true; } protected void makeSerializable(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { if (addGWTInterface) { topLevelClass.addImportedType(gwtSerializable); topLevelClass.addSuperInterface(gwtSerializable); } if (!suppressJavaInterface) { topLevelClass.addImportedType(serializable); topLevelClass.addSuperInterface(serializable); Field field = new Field(); field.setFinal(true); field.setInitializationString("1L"); //$NON-NLS-1$ field.setName("serialVersionUID"); //$NON-NLS-1$ field.setStatic(true); field.setType(new FullyQualifiedJavaType("long")); //$NON-NLS-1$ field.setVisibility(JavaVisibility.PRIVATE); context.getCommentGenerator().addFieldComment(field, introspectedTable); topLevelClass.addField(field); } }}

哇咔咔,成功了。

你可能感兴趣的文章
java事务大总结(一) 先理解数据库的事务以mysql为例
查看>>
java事务大总结(二) 理解JDBC事务的工作机制
查看>>
java事务大总结(三) 理解学习 JTA(Java Transaction API)
查看>>
java事务大总结(四)spring事务相关大总结
查看>>
驴妈妈管理的一点经验总结
查看>>
IOS开发学习的好资料大搜藏
查看>>
SSH的认证终结(无需密码的git操作或者ssh链接无需密码)
查看>>
Jetty 的工作原理以及与 Tomcat 的比较
查看>>
ssh-keygen的使用方法 注意权限问题
查看>>
zookeeper的server的集群配置实例[张振华-Jack]
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第一篇:互联网时代U盘化生存方式 【张振华.Jack】
查看>>
CentOS6.4配置Hadoop-2.6.0集群配置安装指南(经过实战演练)【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第二篇:专注的力量 [张振华.Jack]
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第三篇:我的舍与得的2014[张振华.Jack]
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第五篇:不要给自己找任何借口【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第七篇:请留意我们身边的风景 【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第八篇:坚持的力量 【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第九篇:春节那些事-过年回家不需要理由【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第十篇:程序员们请看看外面的世界吧【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第十一篇:马云乌镇40分钟演讲实录【张振华.Jack】
查看>>