page实体类

package cn.zsmy.tmp;

import java.io.Serializable;

/**
* 分页对象.
*
*/public final class Page implements Serializable { /**
* 默认的序列化版本 id.
*/
private static final long serialVersionUID = 1L;
/**
* 分页查询开始记录位置.
*/
private int begin;
/**
* 分页查看下结束位置.
*/
private int end;
/**
* 每页显示记录数.
*/
private int length = 20;
/**
* 查询结果总记录数.
*/
private int totalRecords;
/**
* 当前页码.
*/
private int pageNo;
/**
* 总共页数.
*/
private int pageCount; public Page() {
} /**
* 构造函数.
*
* @param begin
* @param length
*/
public Page(int begin, int length) {
this.begin = begin;
this.length = length;
this.end = this.begin + this.length;
this.pageNo = (int) Math.floor((this.begin * 1.0d) / this.length) + 1;
} /**
* @param begin
* @param length
* @param count
*/
public Page(int begin, int length, int totalRecords) {
this(begin, length);
this.totalRecords = totalRecords;
} /**
* 设置页数,自动计算数据范围.
*
* @param pageNo
*/
public Page(int pageNo) {
this.pageNo = pageNo;
pageNo = pageNo > 0 ? pageNo : 1;
this.begin = this.length * (pageNo - 1);
this.end = this.length * pageNo;
} /**
* @return the begin
*/
public int getBegin() {
return begin;
} /**
* @return the end
*/
public int getEnd() {
return end;
}
/**
* @param end
* the end to set
*/
public void setEnd(int end) {
this.end = end;
} /**
* @param begin
* the begin to set
*/
public void setBegin(int begin) {
this.begin = begin;
if (this.length != 0) {
this.pageNo = (int) Math.floor((this.begin * 1.0d) / this.length) + 1;
}
} /**
* @return the length
*/
public int getLength() {
return length;
} /**
* @param length
* the length to set
*/
public void setLength(int length) {
this.length = length;
if (this.begin != 0) {
this.pageNo = (int) Math.floor((this.begin * 1.0d) / this.length) + 1;
}
} /**
* @return the totalRecords
*/
public int getTotalRecords() {
return totalRecords;
} /**
* @param totalRecords
* the totalRecords to set
*/
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
this.pageCount = (int) Math.floor((this.totalRecords * 1.0d) / this.length);
if (this.totalRecords % this.length != 0) {
this.pageCount++;
}
} /**
* @return the pageNo
*/
public int getPageNo() {
return pageNo;
} /**
* @param pageNo
* the pageNo to set
*/
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
pageNo = pageNo > 0 ? pageNo : 1;
this.begin = this.length * (pageNo - 1);
this.end = this.length * pageNo;
} /**
* @return the pageCount
*/
public int getPageCount() {
if (pageCount == 0) {
return 1;
}
return pageCount;
} /**
* @param pageCount
* the pageCount to set
*/
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
} @Override
public String toString() {
final StringBuilder builder = new StringBuilder("begin=").append(begin).append(", end=")
.append(end).append(", length=").append(length).append(", totalRecords=").append(
totalRecords).append(", pageNo=").append(pageNo).append(", pageCount=")
.append(pageCount); return builder.toString();
}
}

插件类

package cn.zsmy.tmp;

import java.util.List;

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.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
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.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement; /**
* MySQL 分页生成插件。
*
*/public final class MySQLPaginationPlugin extends PluginAdapter { @Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) { // add field, getter, setter for limit clause
addPage(topLevelClass, introspectedTable, "page"); return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
}
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
IntrospectedTable introspectedTable) {
XmlElement page = new XmlElement("if");
page.addAttribute(new Attribute("test", "page != null"));
page.addElement(new TextElement("limit #{page.begin} , #{page.length}"));
element.addElement(page); return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
} /**
* @param topLevelClass
* @param introspectedTable
* @param name
*/
private void addPage(TopLevelClass topLevelClass, IntrospectedTable introspectedTable,
String name) {
topLevelClass.addImportedType(new FullyQualifiedJavaType("cn.zsmy.tmp.Page"));
CommentGenerator commentGenerator = context.getCommentGenerator();
Field field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
field.setType(new FullyQualifiedJavaType("cn.zsmy.tmp.Page"));
field.setName(name);
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(new FullyQualifiedJavaType("cn.zsmy.tmp.Page"), name));
method.addBodyLine("this." + name + "=" + name + ";");
commentGenerator.addGeneralMethodComment(method, introspectedTable);
topLevelClass.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(new FullyQualifiedJavaType("cn.zsmy.tmp.Page"));
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;
}
}

要注意的地方,page类地址要写对,可以和插件类放一起

generator.xml配置

<?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>
<!-- oracle lib location -->
<classPathEntry location="E:\backup\repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 生成的pojo,将implements Serializable -->
<!-- <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> --> <plugin type="cn.zsmy.tmp.DeleteLogicByIdsPlugin"></plugin>
<plugin type="cn.zsmy.tmp.MySQLPaginationPlugin"></plugin> <commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.1.2:3306/palm_2_0_16" userId="root"
password="sqj888">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!-- model package and location -->
<javaModelGenerator targetPackage="cn.zsmy.entity" targetProject="palmdoctor.code\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- mapping package and location -->
<sqlMapGenerator targetPackage="cn.zsmy.mapper" targetProject="palmdoctor.code\src\main\java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- dao package and location -->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.zsmy.mapper" targetProject="palmdoctor.code\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator> <!-- enableSelectByExample不为true就不能生成分页的示例 -->
<table tableName="tb_hello" domainObjectName="Hello" /> </context>
</generatorConfiguration>

mysql mybatis-generator plugin 有page实体类的分页的更多相关文章

  1. 使用eclipse插件mybatis generator来自动生成实体类及映射文件

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguratio ...

  2. Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数 (转)

    众所周知,Mybatis本身没有提供基于数据库方言的分页功能,而是基于JDBC的游标分页,很容易出现性能问题.网上有很多分页的解决方案,不外乎是基于Mybatis本机的插件机制,通过拦截Sql做分页. ...

  3. mybatis的基本配置:实体类、配置文件、映射文件、工具类 、mapper接口

    搭建项目 一:lib(关于框架的jar包和数据库驱动的jar包) 1,第一步:先把mybatis的核心类库放进lib里

  4. MyBatis——解决字段名与实体类属性名不相同的冲突

    原文:http://www.cnblogs.com/xdp-gacl/p/4264425.html 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况 ...

  5. MyBatis解决字段名与实体类属性名不相同的冲突(四)

    一.创建表和表数据 CREATE TABLE orders( order_id INT PRIMARY KEY AUTO_INCREMENT, order_no ), order_price FLOA ...

  6. [转]【MyBatis】Decimal映射到实体类出现科学计数法问题

    原文地址:https://blog.csdn.net/harwey_it/article/details/80269388 问题: Mybatis查询Decimal字段映射到实体类后,出现科学计数法的 ...

  7. Mybatis解决字段名与实体类属性名不相同的冲突

    在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况下的如何解决字段名与实体类属性名不相同的冲突. 一.准备演示需要使用的表和数据 CREATE TAB ...

  8. 模拟实现MyBatis中通过SQL反射实体类对象功能

    话不多说,直接上干货! package cn.test; import java.lang.reflect.Method; import java.sql.Connection; import jav ...

  9. 高速创建和mysql表相应的java domain实体类

    今天创建了一个表有十几个字段,创建完之后必定要写一个与之相应的java domain实体类. 这不是反复的工作吗?为什么不先把这个表的全部的字段查出来,然后放到linux环境下,用sed工具在每一行的 ...

随机推荐

  1. HTML5的Server-Sent Events介绍////////////////zzz

    HTML5有一个Server-Sent Events(SSE)功能,允许服务端推送数据到客户端.(通常叫数据推送).我们来看下,传统的WEB应用程序通信时的简单时序图: 现在Web App中,大都有A ...

  2. 【001:go语言的一些语法基础】

    1. 循环语句 Go只有for一个循环语句关键字,但支持3种形式 初始化和步进表达式可以是多个值 条件语句每次循环都会被重新检查,因此不建议在条件语句中 使用函数,尽量提前计算好条件并以变量或常量代替 ...

  3. java文件编程总结

    文件编程之一就是文件操作,就是新建,删除,复制,更名 新建文件用createNewFile()方法,要处理IOException异常 新建文件夹用mkdirs()方法 删除一个文件和一个空的文件夹直接 ...

  4. Your intuition 你的直觉

    If you’re thinking just like everyone else, you aren’t really thinking. Follow your intuition. Do wh ...

  5. uexGaodeMap插件Android接入指引

    uexGaodeMap插件Android接入指引 高德地图插件是基于高德地图API封装的AppCan平台的插件模块.开发者集成及使用此插件,需要到高德开放平台为应用申请相应的APIKey,并将APIK ...

  6. 数据库备份checksum选项你会用么?

    SQL SERVER有好多好多功能,选项也一大堆,很多功能选项并不常用.但是如果真有这种需求的时候又想不起来~ 本篇我们就来聊聊备份里的选项checksum,这是个啥玩意?听都没听过?来看下图: 就是 ...

  7. redhat6.3下源码编译方式安装最新版git

    在linux下安装git,通过yum方式安装的不是最新版本. 要替换最新版需要以下操作. 1.安装依赖包 # yum install curl-devel expat-devel gettext-de ...

  8. Avg_row_length是怎么计算的?

    通过一下命令我们可以获取表的使用情况: root::>show table status like 'tbname'\G 结果: . row ************************** ...

  9. “You couldn’t see my tears cause I am in the water.“ Fish said to water.“But I could feel your tears cause you are in my heart..“ Answered water.

    “You couldn’t see my tears cause I am in the water.“ Fish said to water.“But I could feel your tears ...

  10. Java泛型详解 转载

    转载地址:http://blog.csdn.net/jinuxwu/article/details/6771121 比较好的讲解: http://blog.csdn.net/lonelyroamer/ ...