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类地址要写对,可以和插件类放一起

mysql mybatis-generator plugin 有page实体类的分页_xml

 

 

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>