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工具在每一行的 ...
随机推荐
- 一个.net程序员的安卓之旅-Eclipse设置代码智能提示功能
一个.net程序员的安卓之旅-代码智能提示功能 过完年回来就决心开始学安卓开发,就网上买了个内存条加在笔记本上(因为笔记本原来2G内存太卡了,装了vs2010.SQL Server 2008.orac ...
- Java多线程开发系列之一:走进多线程
对编程语言的基础知识:分支.选择.循环.面向对象等基本概念理解后,我们需要对java高级编程有一定的学习,这里不可避免的要接触到多线程开发. 由于多线程开发整体的系统比较大,我会写一个系列的文章总结介 ...
- css2 [lang|=en] 误区
[lang|=en] w3c说明:css2选择器,选择以en开头的的lang属性. w3c的这个解释是有误区的,en开头,但是en后面必须要有-,也就是说是选择的是en-开头
- dev TreeList拖拽
一.说明 使用dev控件,TreeList1向TreeList2拖拽 二.属性 //允许拖拽 treeList1.AllowDrop = true; tre ...
- List view优化
ListView 针对每个item,要求 adapter "返回一个视图" (getView),也就是说ListView在开始绘制的时候,系统首先调用getCount()函数,根据 ...
- js 自带的 map() 方法
1. 方法概述 map() 方法返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组. 2. 例子 2.1 在字符串中使用map 在一个 String 上使用 map 方法获取字符串中 ...
- hadoop多硬盘配置注意点
[一].实验环境 1.1.环境 hadoop 2.6.0 namenode 2个 各增加 2个1T硬盘 datanode 3个 各增加3个1T硬盘 1.2.挂载硬盘 fdisk 分区 ( ...
- Django进阶2
一.ORM操作进阶 ForeignKey关联 示例models from django.db import models # Create your models here. class User(m ...
- 解决对含有第三方jar包的项目打包出现java.lang.NoClassDefFoundError问题
用eclipse普通的打包方式,对含有第三方jar包的项目进行打包.调用方法后一只出现java.lang.NoClassDefFoundError问题. 从网上搜寻,很多都是在MANIFEST.MF文 ...
- iOS高性能设置圆角
自建一个分类可以设置. -(void)cornerImageWithSize:(CGSize)size fillColor:(UIColor *)fillColor completion:(void( ...