mysql mybatis-generator plugin 有page实体类的分页
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实体类的分页的更多相关文章
- 使用eclipse插件mybatis generator来自动生成实体类及映射文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguratio ...
- Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数 (转)
众所周知,Mybatis本身没有提供基于数据库方言的分页功能,而是基于JDBC的游标分页,很容易出现性能问题.网上有很多分页的解决方案,不外乎是基于Mybatis本机的插件机制,通过拦截Sql做分页. ...
- mybatis的基本配置:实体类、配置文件、映射文件、工具类 、mapper接口
搭建项目 一:lib(关于框架的jar包和数据库驱动的jar包) 1,第一步:先把mybatis的核心类库放进lib里
- MyBatis——解决字段名与实体类属性名不相同的冲突
原文:http://www.cnblogs.com/xdp-gacl/p/4264425.html 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况 ...
- MyBatis解决字段名与实体类属性名不相同的冲突(四)
一.创建表和表数据 CREATE TABLE orders( order_id INT PRIMARY KEY AUTO_INCREMENT, order_no ), order_price FLOA ...
- [转]【MyBatis】Decimal映射到实体类出现科学计数法问题
原文地址:https://blog.csdn.net/harwey_it/article/details/80269388 问题: Mybatis查询Decimal字段映射到实体类后,出现科学计数法的 ...
- Mybatis解决字段名与实体类属性名不相同的冲突
在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况下的如何解决字段名与实体类属性名不相同的冲突. 一.准备演示需要使用的表和数据 CREATE TAB ...
- 模拟实现MyBatis中通过SQL反射实体类对象功能
话不多说,直接上干货! package cn.test; import java.lang.reflect.Method; import java.sql.Connection; import jav ...
- 高速创建和mysql表相应的java domain实体类
今天创建了一个表有十几个字段,创建完之后必定要写一个与之相应的java domain实体类. 这不是反复的工作吗?为什么不先把这个表的全部的字段查出来,然后放到linux环境下,用sed工具在每一行的 ...
随机推荐
- 。。。欢乐捕鱼App WeX5 连接打包代理服务失败,请检查代理服务地址是否正确。。。
今天学习了WeX5,第一次使用,使用它打包一个Web App 欢乐捕鱼的时候,在最终打包生成Native App的时候突然报错了,说:"连接打包代理服务失败,请检查代理服务地址是否正确&qu ...
- js执行环境的深入理解
第一个例子中 :之所以每个函数都返回不同的值的原因 有2点 (简写如下文) 就是[SCOPE]内部属性,函数可能拥有相同的父作用域时,多个函数引用同一个[SCOPE]属性,所以return i的值还是 ...
- Cassandra数据类型:
Cassandra在CQL语言层面支持多种数据类型[12]. CQL类型 对应Java类型 描述 ascii String ascii字符串 bigint long 64位整数 blob ByteBu ...
- C#获取EF实体对象或自定义属性类的字段名称和值
在年前上班的时候遇到了一个问题是这样描述的:我前台设计一个页面,是标签和文本框,当用户修改了哪个文本框的值,将该修改前的值.修改后的值,该值对应的字段,该值对应的行id获取到保存到数据库的某张表里.现 ...
- Swift2.3 --> Swift3.0 的变化
Swift3.0语法变化 首先和大家分享一下学习新语法的技巧: 用Xcode8打开自己的Swift2.3的项目,选择Edit->Convert->To Current Swift Synt ...
- Oracle安装错误“程序异常终止
Oracle安装错误"程序异常终止.发生内部错误.请将以下文件提供给oracle技术支持部 "程序异常终止.发生内部错误.请将以下文件提供给oracle技术支持部门:" ...
- [MKRCVCD]Burning SDK report AddFile error
在使用Pipe通信的使用,我使用GetProcessExitCode这个函数来获取返回值.而ExitCode的定义为DWORD DWORD的原型为unsigned long,在32位程序中,DWORD ...
- sql 查询表中所有字段的名称
最近工作用到SQL语句查询表中所有字段的名称,网上查询,发现不同数据库的查询方法不同,例如: SQL server 查询表的所有字段名称:Select name from syscolumns Whe ...
- ES数据-MySql处理Date类型的数据导入处理
用ES的小伙伴们,相信大家都遇到过Mapping处理Date类型的数据头疼问题吧. 不用头疼了,我来给你提供一种解决方案: 1.Maping定义为: { "mappings": ...
- 。net 之view筛选
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...