1、首先定义分页插件

MysqlPagePlugin.java

  1. package com.demo.mybatis.plugin;
  2. import org.mybatis.generator.api.CommentGenerator;
  3. import org.mybatis.generator.api.IntrospectedTable;
  4. import org.mybatis.generator.api.PluginAdapter;
  5. import org.mybatis.generator.api.dom.java.*;
  6. import org.mybatis.generator.api.dom.xml.Attribute;
  7. import org.mybatis.generator.api.dom.xml.TextElement;
  8. import org.mybatis.generator.api.dom.xml.XmlElement;
  9. import java.util.List;
  10. /**
  11. * <pre>
  12. * add pagination using mysql limit.
  13. * This class is only used in ibator code generator.
  14. * </pre>
  15. */
  16. /**
  17. * mysql 分页生成插件
  18. */
  19. public class MysqlPagePlugin extends PluginAdapter {
  20. /**
  21. * 添加 分页 开始行数 和结束行数 属性
  22. */
  23. @Override
  24. public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
  25. IntrospectedTable introspectedTable) {
  26. // add field, getter, setter for limit clause
  27. addProperty(topLevelClass, introspectedTable, "limitStart", FullyQualifiedJavaType.getIntInstance());
  28. addProperty(topLevelClass, introspectedTable, "limitEnd", FullyQualifiedJavaType.getIntInstance());
  29. addProperty(topLevelClass, introspectedTable, "groupByClause", FullyQualifiedJavaType.getStringInstance());
  30. return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
  31. }
  32. /**
  33. * 添加 映射 文件配置 limit 的配置
  34. */
  35. @Override
  36. public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
  37. XmlElement element, IntrospectedTable introspectedTable) {
  38. // XmlElement isParameterPresenteElemen = (XmlElement) element.getElements();
  39. //设置 if 判断 节点
  40. XmlElement limitElement = new XmlElement("if"); //$NON-NLS-1$
  41. //给 节点添加 条件运算符
  42. limitElement.addAttribute(new Attribute("test", "limitEnd > 0")); //$NON-NLS-1$ //$NON-NLS-2$
  43. //如果条件成立 就进行分页查询
  44. limitElement.addElement(new TextElement(
  45. "limit #{limitStart,jdbcType=INTEGER} , #{limitEnd,jdbcType=INTEGER}"));
  46. //添加节点到 配置文件中
  47. element.addElement(limitElement);
  48. XmlElement groupbyElement = new XmlElement("if"); //$NON-NLS-1$
  49. //给 节点添加 条件运算符
  50. groupbyElement.addAttribute(new Attribute("test", "groupByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$
  51. //如果条件成立 就进行分页查询
  52. groupbyElement.addElement(new TextElement(
  53. "group by ${groupByClause}"));
  54. //添加节点到 配置文件中
  55. element.addElement(groupbyElement);
  56. return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
  57. }
  58. /**
  59. * 给对应的实体 实体添加 属性字段
  60. */
  61. private void addProperty(TopLevelClass topLevelClass,
  62. IntrospectedTable introspectedTable, String name, FullyQualifiedJavaType fullyQualifiedJavaType) {
  63. CommentGenerator commentGenerator = context.getCommentGenerator();
  64. Field field = new Field();
  65. field.setVisibility(JavaVisibility.PROTECTED);
  66. field.setType(fullyQualifiedJavaType);
  67. field.setName(name);
  68. // field.setInitializationString("-1");
  69. commentGenerator.addFieldComment(field, introspectedTable);
  70. topLevelClass.addField(field);
  71. char c = name.charAt(0);
  72. String camel = Character.toUpperCase(c) + name.substring(1);
  73. Method method = new Method();
  74. method.setVisibility(JavaVisibility.PUBLIC);
  75. method.setName("set" + camel);
  76. method.addParameter(new Parameter(fullyQualifiedJavaType, name));
  77. method.addBodyLine("this." + name + "=" + name + ";");
  78. commentGenerator.addGeneralMethodComment(method, introspectedTable);
  79. topLevelClass.addMethod(method);
  80. method = new Method();
  81. method.setVisibility(JavaVisibility.PUBLIC);
  82. method.setReturnType(fullyQualifiedJavaType);
  83. method.setName("get" + camel);
  84. method.addBodyLine("return " + name + ";");
  85. commentGenerator.addGeneralMethodComment(method, introspectedTable);
  86. topLevelClass.addMethod(method);
  87. }
  88. /**
  89. * This plugin is always valid - no properties are required
  90. */
  91. public boolean validate(List<String> warnings) {
  92. return true;
  93. }
  94. // public static void generate() {
  95. // String config = MysqlPagePlugin.class.getClassLoader().getResource("generatorConfig-w5Log.xml").getFile();
  96. // String[] arg = { "-configfile", config, "-overwrite" };
  97. // ShellRunner.main(arg);
  98. // }
  99. // public static void main(String[] args) {
  100. // generate();
  101. // }
  102. }

2、然后为mybatisgenerator配置插件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  3. <generatorConfiguration>
  4. <context id="context1">
  5. <!-- 使用自带序列化插件 -->
  6. <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
  7. <!-- 使用自定义的插件 -->
  8. <plugin type="com.demo.mybatis.plugin.MysqlPagePlugin"/>
  9. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  10. connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF8"
  11. userId="root" password="123456">
  12. </jdbcConnection>
  13. <javaModelGenerator targetPackage="com.ilovey.biz.entity.base"
  14. targetProject="ilovey.biz/src/main/java"/>
  15. <sqlMapGenerator targetPackage="com.ilovey.biz.mapper.base"
  16. targetProject="ilovey.biz/src/main/resources"/>
  17. <javaClientGenerator targetPackage="com.ilovey.biz.mapper.base"
  18. targetProject="ilovey.biz/src/main/java" type="XMLMAPPER"/>
  19. <table tableName="us_user_info" domainObjectName="UsUserInfo">
  20. <generatedKey column="id" sqlStatement="MySql" identity="true"/>
  21. </table>
  22. </context>
  23. </generatorConfiguration>

3、使用示例

  1. @Repository
  2. public class UsUserInfoDao {
  3. @Autowired
  4. private UsUserInfoMapper usUserMapper;
  5. /**
  6. * 分页查询用户信息
  7. * @param kinCode 用户类型
  8. * @param page 分页参数(page为自定义的对象)
  9. * @return
  10. */
  11. public List<UsUserInfo> listUserInfo(String kinCode, Page page) {
  12. UsUserInfoExample example = new UsUserInfoExample();
  13. example.createCriteria().andKindCodeEqualTo(kinCode);
  14. //分页
  15. example.setLimitStart(page.limitStart());
  16. example.setLimitEnd(page.limitEnd());
  17. //排序
  18. example.setOrderByClause("create_time desc");
  19. //查询总数
  20. page.setTotalCount(usUserMapper.countByExample(example));
  21. return usUserMapper.selectByExample(example);
  22. }
  23. }

本文所有代码及demo:https://gitee.com/chaocloud/generator-demo.git

mybatis generator插件系列--分页插件的更多相关文章

  1. MyBatis Generator实现MySQL分页插件

    MyBatis Generator是一个非常方便的代码生成工具,它能够根据表结构生成CRUD代码,可以满足大部分需求.但是唯一让人不爽的是,生成的代码中的数据库查询没有分页功能.本文介绍如何让MyBa ...

  2. MyBatis学习总结_17_Mybatis分页插件PageHelper

    如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该 ...

  3. SpringBoot 添加mybatis generator 自动生成代码插件

    自动生成数据层代码,提高开发效率 1.pom添加插件,并指定配置文件路径 <!-- mybatis generator 自动生成代码插件 --> <plugin> <gr ...

  4. Eclipse MyBatis generator 1.3.7插件的核心包(中文注释)

    一.最近刚搭建一个项目框架,使用springboot + mybatis,但是在使用Eclipse开发时发现开发mybatis的Dao.mapper.xml和entity时特别不方便,手工去写肯定是不 ...

  5. mybatis generator插件系列--lombok插件 (减少百分之九十bean代码)

    经常使用mybatis generator生成代码的你 有没有因为生成的getter/setter而烦恼呢? 有没有生成后又手动加toString/hashCode/Equals方法呢? 有没有改一个 ...

  6. SpringBoot+MyBatis多数据源使用分页插件PageHelper

    之前只用过单数据源下的分页插件,而且几乎不用配置.一个静态方法就能搞定. PageHelper.startPage(pageNum, pageSize); 后来使用了多数据源(不同的数据库),Page ...

  7. Springboot集成mybatis通用Mapper与分页插件PageHelper

    插件介绍 通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及 Example 相关的单表操作.通用 Mapper 是为了解决 MyBatis 使用 ...

  8. Mybatis generator 数据库反向生成插件的使用

    直接上干货: 可生成数据库表对应的po  mpper接口文件 mapper.xml文件.文件中自动配置了部分常用的dao层方法.用于快速快发. 1.pom中引入插件: <plugin> & ...

  9. 动手编写插件-javascript分页插件

    原来公司用的报表分页插件是C#编写的服务器插件,需要前后台交互,而且不支持ajax. 经过一段时间折腾,我编写了一个轻便的jquery分页插件,支持ajax.下面是插件代码 /* 插件名称:报表分页 ...

随机推荐

  1. [vue]vue-book

    我们打算要做这几个模块 首页 列表 收藏 添加 home.vue --> list.vue -->app.vue --> main.js 安装环境 npm i vue-cli -g ...

  2. 实习培训——Java基础(2)

    实习培训——Java基础(2) 1  Java 变量类型 在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifier [ = value][, identi ...

  3. CMFCPropertyGridProperty的使用

    设定初始值 CString str(_T("Button")); COleVariant cOlevariant(str); pTypeProperty->SetOrigin ...

  4. selenium python 启动Firefox

    我的火狐浏览器版本是最新的: 下载geckodrive:https://github.com/mozilla/geckodriver/releases/ 下载完后将exe文件放到这里“D:\firef ...

  5. [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  6. android studio 3.0 安装配置

    1.  安装jdk1.8 2.复制android sdk  设置代理  mirrors.neusoft.edu.cn  端口 80 http代理  更新sdk  安装  android support ...

  7. IN的另类写法

    EXPLAIN SELECT * FROM `tcb_capital_log` WHERE id IN(66,79,47) EXPLAIN SELECT * FROM ( SELECT 66 AS i ...

  8. Object-C-Foundation-NSDate

    NSDate 表达日期表达时间的方法 NSDate *now=[NSDate date]; 获得当前日期 NSDate *tomrrow=[now dateByAddingTimeInterval:2 ...

  9. JavaScript实现功能全集

    JavaScript就这么回事1:基础知识 1 创建脚本块 <script language="JavaScript">JavaScript code goes her ...

  10. html5 manifest 离线缓存知识点

    1.最大缓存容量为 5M. 2.manifest文件需要配置正确的MIME-type,即“text/cache-manifest”,这个是在web服务器上进行配置. ②编写.manifest文件,文件 ...