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

还以为要手工加入(開始是手工加入的),今天遇到分页的问题,才发现生成的时候能够加入插件。

既然分页能够有插件。序列化是不是也有呢。

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

 <plugin type="org.mybatis.generator.plugins.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<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;
} /**
* 加入给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<String> 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<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;
} /**
* 加入给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);
}
}
}

哇咔咔。成功了。

MyBatis Generator生成DAO——序列化的更多相关文章

  1. 使用MyBatis Generator生成DAO

    虽然MyBatis很方便,但是想要手写全部的mapper还是很累人的,好在MyBatis官方推出了自动化工具,可以根据数据库和定义好的配置直接生成DAO层及以下的全部代码,非常方便. 需要注意的是,虽 ...

  2. mybatis Generator生成代码及使用方式

    本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5889312.html 为什么要有mybatis mybatis 是一个 Java 的 ORM 框架,OR ...

  3. Maven下用MyBatis Generator生成文件

    使用Maven命令用MyBatis Generator生成MyBatis的文件步骤如下: 1.在mop文件内添加plugin <build> <finalName>KenShr ...

  4. 利用org.mybatis.generator生成实体类

    springboot+maven+mybatis+mysql 利用org.mybatis.generator生成实体类 1.添加pom依赖:   2.编写generatorConfig.xml文件 ( ...

  5. MyBatis Generator 生成的example 使用 and or 简单混合查询

    MyBatis Generator 生成的example 使用 and or 简单混合查询 参考博客:https://www.cnblogs.com/kangping/p/6001519.html 简 ...

  6. 【记录】Mybatis Generator生成数据对象Date/TimeStamp 查询时间格式化

    Mybatis Generator是很好的工具帮助我们生成表映射关联代码,最近博主遇到一个问题,找了很久才解决, 就是用Mybatis Generator生成实体类的时候,Date 时间无法格式化输出 ...

  7. Mybatis Generator生成Mybatis Dao接口层*Mapper.xml以及对应实体类

    [前言] 使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件,Mybatis-Generator的作用就是充当了一个代码生成器的角色,使用代码生成器不仅可以简化我 ...

  8. MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

    由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...

  9. MyBatis generator 生成生成dao model mappper

    MyBatis GeneratorXML配置文件参考 在最常见的用例中,MyBatis Generator(MBG)由XML配置文件驱动. 配置文件告诉MBG: 如何连接到数据库 什么对象要生成,以及 ...

随机推荐

  1. VMware Fault Tolerance 概述及功能

    VMware Fault Tolerance - 为您的应用程序提供全天候可用性 通过为虚拟机启用 VMware Fault Tolerance,最大限度地延长数据中心的正常运行时间,减少停机管理成本 ...

  2. root权限

    点击左侧终端标 步骤阅读 2 出现命令提示符 3 首先输入:sudo passwd root(设置root密码) 4 输入当前系统的账户密码(账户:admin-pc的密码) 5 输入新的root密码, ...

  3. Quartus2 通过Nativelink调用modelsim进行功能仿真(转载)

    quartus2建立工程后,编译并检查语法通过后(功能仿真都不需要综合) tips:这样你的工程层次化也同时完成了. 打开Assignment -> settings, 找到Simulation ...

  4. 两步完美解决 androud 模拟器太慢的问题

    androud 开发环境默认的 avd 管理器下载并启动的模拟器,运行速度非常慢,有时不可忍受,用下面两步可以解决这个问题: 下载 genymotion-2.3.1 (注意,最好是这个版本,试过2.4 ...

  5. 图片工具picpick

    picpick 功能 截屏滚动窗口可以用来截取长图

  6. python 拼图验证码

    基于python2.7 django 1.10 用谷歌浏览器验证测试没问题,写的很烂,纯属学习 项目地址 https://github.com/cainiaoit/-Jigsaw-verifying- ...

  7. ipv6nginx错误

    400 Bad Request The plain HTTP request was sent to HTTPS port错误参考官方文档解决方法如下: server {listen 80;liste ...

  8. hdu 5150(水题)

    Sum Sum Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. 树链剖分【P3833】 [SHOI2012]魔法树

    Description Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节点0是根节点,每个节点u的 ...

  10. Cookie和Session在Node.JS中的实践(一)

    Cookie和Session在Node.JS中的实践(一) Cookie和Session是一个非常有趣的概念,也是一个老生常谈的话题.然而,作者看了许多文章,也翻看了几本书籍,它们对Cookie和Se ...