mybatis-generator 插件扩展,序列化针对 实体类(model)、 example类
2016-09-20 12:37
776 查看
摘要: mybatis-generator 插件扩展,自定义一个扩展插件(SerializablePlugin), 不仅仅针对生成的 实体类序列化(model),同样针对生成的 Example 类序列化, 这样在分布式环境中,就能够正常使用;
MyBatis Generator生成DAO 的时候,生成的类都是没有序列化的,查看源码,Mybatis Generator 默认提供的是有序列化插件的,但是这个插件是仅仅针对model类生成的,针对 Example 类是没有这个插件扩展功能的;
如果我们的环境是分布式环境开发的话,这个 Example 也是需要序列化的,故扩展这个插件,使他不仅仅针对model 类,同样针对 Example 类;
http://git.oschina.net/alexgaoyh/MutiModule-parent/blob/master/MutiModule-common/src/main/java/com/MutiModule/common/mybatis/plugin/serializable/SerializablePlugin.java/?dir=0&filepath=MutiModule-common/src/main/java/com/MutiModule/common/mybatis/plugin/serializable/SerializablePlugin.java&oid=a36d7cc2d1d4ce40fa1ddffbfdc7653b7be7e2ae&sha=377ef54737b723905ff27aeaf6442503b82409a0
MyBatis Generator生成DAO 的时候,生成的类都是没有序列化的,查看源码,Mybatis Generator 默认提供的是有序列化插件的,但是这个插件是仅仅针对model类生成的,针对 Example 类是没有这个插件扩展功能的;
如果我们的环境是分布式环境开发的话,这个 Example 也是需要序列化的,故扩展这个插件,使他不仅仅针对model 类,同样针对 Example 类;
http://git.oschina.net/alexgaoyh/MutiModule-parent/blob/master/MutiModule-common/src/main/java/com/MutiModule/common/mybatis/plugin/serializable/SerializablePlugin.java/?dir=0&filepath=MutiModule-common/src/main/java/com/MutiModule/common/mybatis/plugin/serializable/SerializablePlugin.java&oid=a36d7cc2d1d4ce40fa1ddffbfdc7653b7be7e2ae&sha=377ef54737b723905ff27aeaf6442503b82409a0
package com.MutiModule.common.mybatis.plugin.serializable; 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; /** * 分布式开发的话,Example对象也必须要序列化 * 扩展一个 mybatis generator 插件,用于不仅仅在生成的实体类 还有 *Example 类都序列化 * @author alexgaoyh * */ 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); } } }
相关文章推荐
- Maven插件-mybatis-generator(mybatis自动生成实体代码的插件)
- Maven插件之mybatis-generator(mybatis自动生成实体代码的插件)
- Maven插件之mybatis-generator(mybatis自动生成实体代码的插件)
- mybatis 之 generator插件使用 集成到了eclipse 自动生成实体,example,mapper
- MyBatis代码实例系列-08:MyBatisGenerator插件及扩展(中文注释和Mapper重命名为Dao)
- Maven使用Mybatis Generator插件生成Mapper接口 xml和实体
- java_model_dao_自动生成_generator-mybatis-generator-1.3.2 基于maven插件
- mybatis-generator 插件扩展,生成支持多种数据库的分页功能
- Mybatis-Generator插件自动生成Dao、Model、Mapping相关文件
- mybatis-generator-maven-plugin 插件扩展 增加自定义方法
- mybatis 之 generator插件使用 集成到了eclipse 自动生成实体,example,mapper
- Mybatis使用Generator插件自动生成Mapper,实体类
- mybatis-generator 插件离线安装
- 利用mybatis generator插件生成基于分页语句及分页解决方案
- Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数
- mybatis的generator自动生成xml、model。map等信息
- Myeclipse配置Mybatis_generator生成mapper,model,client
- maven+mybatis+mybatis-generator+sql server 2005自动生成代码,加上自定义分页插件和批量插入更新插件
- mybatis generator myeclipse 插件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)