当前位置: 首页 > news >正文

mybatis结合generator进行分页插件PluginAdapter开发

mybatis结合generator生成的代码没有分页的功能,可以尝试自己继承分页插件PluginAdapter,进行开发,实现自己的分页插件这样generator生产的代码 带分页功能了。

MyBatis MySQL自动生成带分页插件

继承PluginAdapter类,实现相关方法


/*** MyBatis MySQL自动生成带分页插件* * @author */
public class MysqlPaginationPlugin extends PluginAdapter {@Overridepublic boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {addLimit(topLevelClass, introspectedTable, "limitStart");addLimit(topLevelClass, introspectedTable, "limitSize");return super.modelExampleClassGenerated(topLevelClass, introspectedTable);}/*** 为selectByExample添加limitStart和limitSize*/@Overridepublic boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,IntrospectedTable introspectedTable) {XmlElement isNotNullElement = new XmlElement("if");isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitSize >= 0"));isNotNullElement.addElement(new TextElement("limit #{limitStart} , #{limitSize}"));element.addElement(isNotNullElement);return super.sqlMapSelectByExampleWithoutBLOBsElementGenerated(element, introspectedTable);}/*** 为selectByExampleWithBLOBs添加limitStart和limitSize*/@Overridepublic boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element,IntrospectedTable introspectedTable) {XmlElement isNotNullElement = new XmlElement("if");isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitSize >= 0"));isNotNullElement.addElement(new TextElement("limit #{limitStart} , #{limitSize}"));element.addElement(isNotNullElement);return super.sqlMapSelectByExampleWithBLOBsElementGenerated(element, introspectedTable);}private void addLimit(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) {CommentGenerator commentGenerator = context.getCommentGenerator();/*** 创建类成员变量 如protected Integer limitStart;*/Field field = new Field();field.setVisibility(JavaVisibility.PROTECTED);field.setType(PrimitiveTypeWrapper.getIntegerInstance());field.setName(name);commentGenerator.addFieldComment(field, introspectedTable);topLevelClass.addField(field);/*** 首字母大写*/char c = name.charAt(0);String camel = Character.toUpperCase(c) + name.substring(1);/*** 添加Setter方法*/Method method = new Method();method.setVisibility(JavaVisibility.PUBLIC);method.setName("set" + camel);method.addParameter(new Parameter(PrimitiveTypeWrapper.getIntegerInstance(), name));StringBuilder sb = new StringBuilder();sb.append("this.");sb.append(name);sb.append(" = ");sb.append(name);sb.append(";");/*** 如 this.limitStart = limitStart;*/method.addBodyLine(sb.toString());commentGenerator.addGeneralMethodComment(method, introspectedTable);topLevelClass.addMethod(method);/*** 添加Getter Method 直接调用AbstractJavaGenerator的getGetter方法*/Method getterMethod = AbstractJavaGenerator.getGetter(field);commentGenerator.addGeneralMethodComment(getterMethod, introspectedTable);topLevelClass.addMethod(getterMethod);}public boolean validate(List<String> warnings) {return true;}public static void generate() {String config = PaginationMysqlPlugin.class.getClassLoader().getResource("generatorConfig.xml").getFile();String[] arg = { "-configfile", config, "-overwrite" };ShellRunner.main(arg);}public static void main(String[] args) {generate();}}

http://www.lryc.cn/news/417886.html

相关文章:

  • 面试:ArrayList和LinkedList
  • 【uniapp】uniapp+vue2微信小程序实现分享功能
  • WEB渗透Web突破篇-目录爆破
  • Windows设备文件同步平台
  • 用九方智投学习机,学会应对回撤风险
  • maven打包加入本地jar包
  • 从TiDB迁移到OceanBase的实践分享
  • DL00765-光伏故障检测高分辨率无人机热红外图像细粒度含数据集4000+张
  • CICD流水线
  • Sass/Scss基础
  • 【sx sb sz】Centos/Linux sx、sb、sz命令详细介绍
  • 【网络层】IP报文解析和网段划分
  • [GXYCTF2019]禁止套娃1
  • 人工智能时代,数字化工厂如何改革?提升竞争力?
  • 气膜建筑的抗风与防火性能:保障仓储的安全—轻空间
  • 【秋招笔试】2024-08-07-YT游戏(研发岗)-三语言题解(CPP/Python/Java)
  • 【Python知识】m.inplace = inplace 《==》是否执行原地操作
  • Go语言fmt包中print相关方法
  • 图片转为pdf怎么弄?亲测有效的8个pdf转换方法安利
  • 贪吃蛇(使用QT)
  • 【案例40】Apache中mod_proxy模块的使用
  • 简单安装Android Studio并使用
  • 在Python中,模块(Module)和包(Package)
  • Node版本管理工具
  • 创建并发布NPM模块
  • 20240807软考架构-------软考31-35答案解析
  • 简单实现二叉树(链表实现)
  • 搭建 Web 群集Haproxy
  • PDF隐写思路
  • Pycharm 常用快捷键