1、首先定义分页插件

MysqlPagePlugin.java

package com.demo.mybatis.plugin;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement; import java.util.List;
/**
* <pre>
* add pagination using mysql limit.
* This class is only used in ibator code generator.
* </pre>
*/ /**
* mysql 分页生成插件
*/ public class MysqlPagePlugin extends PluginAdapter { /**
* 添加 分页 开始行数 和结束行数 属性
*/
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
// add field, getter, setter for limit clause addProperty(topLevelClass, introspectedTable, "limitStart", FullyQualifiedJavaType.getIntInstance()); addProperty(topLevelClass, introspectedTable, "limitEnd", FullyQualifiedJavaType.getIntInstance()); addProperty(topLevelClass, introspectedTable, "groupByClause", FullyQualifiedJavaType.getStringInstance()); return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
} /**
* 添加 映射 文件配置 limit 的配置
*/
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
XmlElement element, IntrospectedTable introspectedTable) { // XmlElement isParameterPresenteElemen = (XmlElement) element.getElements();
//设置 if 判断 节点
XmlElement limitElement = new XmlElement("if"); //$NON-NLS-1$ //给 节点添加 条件运算符
limitElement.addAttribute(new Attribute("test", "limitEnd > 0")); //$NON-NLS-1$ //$NON-NLS-2$
//如果条件成立 就进行分页查询
limitElement.addElement(new TextElement(
"limit #{limitStart,jdbcType=INTEGER} , #{limitEnd,jdbcType=INTEGER}"));
//添加节点到 配置文件中
element.addElement(limitElement); XmlElement groupbyElement = new XmlElement("if"); //$NON-NLS-1$ //给 节点添加 条件运算符
groupbyElement.addAttribute(new Attribute("test", "groupByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$
//如果条件成立 就进行分页查询
groupbyElement.addElement(new TextElement(
"group by ${groupByClause}"));
//添加节点到 配置文件中
element.addElement(groupbyElement); return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable); } /**
* 给对应的实体 实体添加 属性字段
*/
private void addProperty(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable, String name, FullyQualifiedJavaType fullyQualifiedJavaType) {
CommentGenerator commentGenerator = context.getCommentGenerator();
Field field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
field.setType(fullyQualifiedJavaType);
field.setName(name); // field.setInitializationString("-1"); commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field);
char c = name.charAt(0);
String camel = Character.toUpperCase(c) + name.substring(1);
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("set" + camel);
method.addParameter(new Parameter(fullyQualifiedJavaType, name)); method.addBodyLine("this." + name + "=" + name + ";");
commentGenerator.addGeneralMethodComment(method, introspectedTable);
topLevelClass.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(fullyQualifiedJavaType);
method.setName("get" + camel);
method.addBodyLine("return " + name + ";");
commentGenerator.addGeneralMethodComment(method, introspectedTable);
topLevelClass.addMethod(method);
} /**
* This plugin is always valid - no properties are required
*/
public boolean validate(List<String> warnings) {
return true;
} // public static void generate() {
// String config = MysqlPagePlugin.class.getClassLoader().getResource("generatorConfig-w5Log.xml").getFile();
// String[] arg = { "-configfile", config, "-overwrite" };
// ShellRunner.main(arg);
// }
// public static void main(String[] args) {
// generate();
// } }

2、然后为mybatisgenerator配置插件

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

3、使用示例

@Repository
public class UsUserInfoDao { @Autowired
private UsUserInfoMapper usUserMapper; /**
* 分页查询用户信息
* @param kinCode 用户类型
* @param page 分页参数(page为自定义的对象)
* @return
*/
public List<UsUserInfo> listUserInfo(String kinCode, Page page) {
UsUserInfoExample example = new UsUserInfoExample();
example.createCriteria().andKindCodeEqualTo(kinCode); //分页
example.setLimitStart(page.limitStart());
example.setLimitEnd(page.limitEnd()); //排序
example.setOrderByClause("create_time desc"); //查询总数
page.setTotalCount(usUserMapper.countByExample(example)); return usUserMapper.selectByExample(example);
}
}

本文所有代码及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. [py]py常用模块小结

    - python md5校验: https://blog.csdn.net/linda1000/article/details/17581035 import hashlib hashlib.md5( ...

  2. Razor中的@:和语法

    用Razor实现流畅编程 Razor尽量减少编写一个视图模板需要敲入的字符数,实现快速流畅的编程工作流.与大部分模板的语法不同,你不必在HTML中为了明确地标记出服务模块 的开始和结束而中断编程.Ra ...

  3. http协议基础(七)通用首部字段

    通用首部字段的意思,就是:请求和响应报文双方都会使用的首部 1.Cache-Control 通过指定它的指令,能操作缓存的工作机制 指令参数是可选的,多个指令通过“,”分隔 Cache-Control ...

  4. discuz formhash

    class.core.php中 $this->var['formhash'] = formhash();define('FORMHASH', $this->var['formhash']) ...

  5. SQL 中单引号 和一些特殊字符的处理

    为了防止程序SQL语句错误以及SQL注入,单引号必须经过处理.有2种办法: 1.使用参数,比如SELECT * FROM yourTable WHERE name = @name; 在Java中就是用 ...

  6. 来自阿里妈妈的iconfont(转)

    转自http://www.augsky.com/775.html 随便说说两者的优缺点 其实主要是说iconfont的优点和Font Awesome的缺点.-_-|||iconfont的图标库相当巨大 ...

  7. Ajax—web中ajax的常用方式

    什么Web2.0的特点? 1:注重用户贡献度 2:内容聚合RSS协议(每小块都个性化,单独加载单独请求,不用全部刷新--Ajax) 3:更丰富的用户体验 Ajax的概念? "Asynchro ...

  8. yii2中关联查询

    yii2 ActiveRecord多表关联以及多表关联搜索的实现 一个老生常谈的问题.最近通过群里的反馈,觉得很多人还是没有去理解这个问题.今天把这个问题讲明白了,看看yii2 ActiveRecor ...

  9. HTML ajax 上传文件限制文件的类型和文件大小

    html    <input type="file" name="excel" id="excel_input" accept=&qu ...

  10. seo标题关键字描述字数限制Title,keywords,description长度最长多长 ?

    seo标题关键字描述字数限制 seo优化各个搜索引擎收录Title,keywords,description长度最长多长 ?SEO网站优化中Title标签的作用为重中之重,好的Title也就成功了一半 ...