PagerModel类(通过传递一个查询实体进行查询)
/**
*
*/
package com.audit.util;
import java.util.List;
/**
* @author chenpeng
* @file_name PagerModel.java
* @create_time 2010-7-27:下午05:21:16
* @email onlychenpeng@msn.com
* TODO
*/
public class PagerModel {
//总数
private int total;
//当前页码,默认为1
private int currentPage=1;
//总页数
private int totalPage;
//页面大小,默认为10
private int pageSize;
//开始,默认为0
private int startCount=0;
//当前页面的长度
private int currentSize=0;
//是否存在下一页
private boolean hasNext;
//是否存在上一页
private boolean hasPre;
//ibatis的example查询实体
private Object obj;
//查询出来的数据
private List datas;
/**
* 首页
*/
public void first(){
this.init(1);
}
/**
* 首页,带页面大小
* */
public void first(int pageSize){
this.init(1, pageSize);
}
/**
* 下一页
* */
public void next(){
this.currentPage++;
this.init(this.currentPage);
}
/**
* 上一页
* */
public void pre(){
this.currentPage--;
this.init(this.currentPage);
}
/**
* 最后一页
* */
public void last(){
this.currentPage=this.totalPage;
this.init(this.currentPage);
}
/***/
/**
* 统一初始化所有参数
*/
public void init(int currentPage,int pageSize){
//当传入的数不大于0时,把当前页面设为了1,页面大小设为10
if(currentPage<1){
this.currentPage=1;
}else{
this.currentPage=currentPage;
}
if(pageSize<1){
this.pageSize=10;
}else{
this.pageSize=pageSize;
}
this.startCount=(this.currentPage-1)*this.pageSize;
}
/**
* 统一初始化所有参数
* @param currentPage
*/
public void init(int currentPage){
//当传入的数不大于0时,把当前页面设为了1,页面大小设为10
if(currentPage<1){
this.currentPage=1;
}else{
this.currentPage=currentPage;
}
this.startCount=(this.currentPage-1)*this.pageSize;
}
public void setTotal(int total) {
this.total = total;
//计算出总页数
int a=this.total/this.pageSize; //商
int b=this.total%this.pageSize; //余
if(0!=b){
a++;
}
this.totalPage=a;
}
//相应的getter&&setter
public int getStartCount() {
return startCount;
}
public void setStartCount(int startCount) {
this.startCount = startCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotal() {
return total;
}
public List getDatas() {
return datas;
}
public void setDatas(List datas) {
this.datas = datas;
this.currentSize=datas.size();
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
public int getCurrentSize() {
return currentSize;
}
public void setCurrentSize(int currentSize) {
this.currentSize = currentSize;
}
public boolean isHasNext() {
if(this.currentPage<this.totalPage){
return true;
}else {
return false;
}
}
public void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
public boolean isHasPre() {
if(this.currentPage>1){
return true;
}else{
return false;
}
}
public void setHasPre(boolean hasPre) {
this.hasPre = hasPre;
}
}
xml文件(要进行两次查询,一个是查询记录总数,一个是返回所有记录)
<!-- 查找总数 -->
<select id="findByExampleCount" parameterClass="PagerModel" resultClass="int">
select count(*) from "fTest"
<dynamic prepend="WHERE">
<isNotNull property="obj" prepend="AND" open="(" close=")" removeFirstPrepend="true">
<isNotNull property="obj.test" prepend="AND">
test like '$obj.test$%'
</isNotNull>
</isNotNull>
</dynamic>
</select>
<!-- 查找记录 -->
<select id="findByExample" parameterClass="PagerModel" resultClass="FTest" cacheModel="fTestCache">
select * from "fTest"
<dynamic prepend="WHERE">
<isNotNull property="obj" prepend="AND" open="(" close=")" removeFirstPrepend="true">
<isNotNull property="obj.test" prepend="AND">
test like '$obj.test$%'
</isNotNull>
</isNotNull>
</dynamic>
limit #pageSize# offset #startCount#
</select>
DAO类(返回一个PagerModel)
public PagerModel findByExample(PagerModel pm) throws SQLException{
Integer count=(Integer)sqlMapper.queryForObject("fTest.findByExampleCount",pm);
List datas=sqlMapper.queryForList("fTest.findByExample",pm);
pm.setDatas(datas);
pm.setTotal(count);
return pm;
}
单元测试代码
package com.audit.test;
import java.sql.SQLException;
import com.audit.model.dao.IFTestDAO;
import com.audit.model.dao.impl.FTestDAOImpl;
import com.audit.model.domain.FTest;
import com.audit.util.PagerModel;
import junit.framework.TestCase;
/**
* @author chenpeng
* @file_name TestPagerModel.java
* @create_time 2010-7-28:下午04:55:43
* @email onlychenpeng@msn.com
* 测试分页
*/
public class TestPagerModel extends TestCase {
public void testPager(){
PagerModel pm=new PagerModel();
pm.init(3, 3);
FTest test=new FTest();
test.setTest("test");
pm.setObj(test);
IFTestDAO ftDAO=new FTestDAOImpl();
try {
pm=ftDAO.findByExample(pm);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(null!=pm){
System.out.println("总记录数:"+pm.getTotal());
System.out.println("总页数:"+pm.getTotalPage());
System.out.println("当前页:"+pm.getCurrentPage());
System.out.println("当前页面长度:"+pm.getCurrentSize());
System.out.println("是否存在上一页:"+pm.isHasPre());
System.out.println("是否存在下一页:"+pm.isHasNext());
}
}
}
测试结果:
总记录数:21
总页数:7
当前页:1
当前页面长度:3
是否存在上一页:false
是否存在下一页:true
分享到:
相关推荐
在描述中提到了一个博客链接,虽然具体内容没有给出,但通常博主会分享关于iBATIS分页实现的详细步骤、常见问题以及可能的解决方案。iBATIS的分页实现主要有两种方法: 1. **基于存储过程的分页**:在数据库层面...
4. **自定义分页实现**:如果不想依赖第三方插件,可以自定义一个拦截器实现分页。通过AOP(面向切面编程)或者MyBatis的Executor拦截器,对执行的SQL语句进行拦截,添加分页逻辑。 5. **性能优化**:在处理大数据...
三、Ibatis分页实现 1. SQL配置 在Ibatis的Mapper XML文件中,我们需要编写一个带有参数的SQL查询,这些参数通常包括当前页码和每页记录数。例如: ```xml SELECT * FROM your_table != null and pageSize != ...
在iBATIS中实现分页功能,我们可以利用其提供的PageHelper插件或自定义标签来实现。 标题"ibatis分页功能"指的就是如何在iBATIS框架中实现数据库查询的分页效果。分页不仅提高了用户体验,还能减少不必要的数据库...
本项目基于ibatis框架实现了分页功能,覆盖了从底层数据库操作到页面展示的完整流程,包括DAO层、Service层、Action层以及JSP页面的展示。 首先,我们来了解一下什么是ibatis。Ibatis是一个优秀的持久层框架,它...
以上就是使用Struts2、Spring和iBatis实现分页功能的基本流程。在实际开发中,你还需要考虑异常处理、国际化、性能优化等方面,确保应用的稳定性和用户体验。通过熟练掌握这三个框架的组合,你可以构建出强大且灵活...
ibatis_likehbm高效分页组件ibatis_likehbm高效分页组件ibatis_likehbm高效分页组件ibatis_likehbm高效分页组件ibatis_likehbm高效分页组件ibatis_likehbm高效分页组件 ibatis_likehbm高效分页组件 ibatis_likehbm...
本知识点将深入探讨如何在Struts2框架中结合iBatis实现基于Freemarker模板的分页功能。 首先,我们需要理解iBatis,它是一个轻量级的Java持久层框架,它提供了一个SQL映射框架,允许开发者将SQL语句与Java代码分离...
公司的大部分项目都开始使用IBatis作为O/R Mapping了,但是在使用的过程中也发现了很多不方便和存在争议的地方,其中一个不方便的地方就是分页,目前的处理方式都是在sqlMap中写针对特定数据库的物理分页Sql语句,对于...
综上所述,这个实例展示了如何整合Struts2、Spring、iBatis和Oracle来构建一个完整的Web应用,实现了动态分页搜索和附件上传功能。这种架构具有良好的可扩展性和可维护性,适用于各种中大型企业级项目。开发者可以...
在2.3.4这个版本中,Ibatis 提供了数据库无关的分页功能,这是一种在不依赖特定数据库语法的情况下实现分页查询的方法,有助于提高代码的可移植性和维护性。 数据库无关分页的核心思想是将分页参数(如当前页数和每...
在IT行业中,Spring、iBatis和PostgreSQL是三个非常重要的技术组件,它们分别在不同的领域发挥着关键作用。Spring是一个全面的Java企业级应用框架,提供了依赖注入、AOP(面向切面编程)、MVC(模型-视图-控制器)等...
在Java Web开发中,Xwork和iBatis是两个非常重要的框架。Xwork是一个轻量级的MVC框架,主要用于处理Action...当然,实际项目中可能还需要考虑其他因素,比如查询优化、异常处理等,但基础的分页实现就是如此简单易懂。
在传统的iBatis框架中,分页通常采用逻辑分页的方式,即通过游标(ResultSet)来逐条处理数据,从而实现分页效果。这种方式虽然跨数据库兼容性好,但性能上不如数据库原生的物理分页高效。物理分页是直接在SQL语句中...
ibatis 物理分页jar ,与官方ibatis不冲突,可直接使用。
因此,我们需要通过修改Ibatis的源代码来实现物理分页,以提高查询效率。 物理分页是直接在数据库层面进行分页,避免了将所有数据加载到内存中的问题。下面我们将详细探讨如何在Ibatis中实现物理分页。 首先,了解...
Ibatis.NET提供了分页查询的实现,下面我们将深入探讨如何在Ibatis.NET中实现分页。 首先,理解分页的基本概念。分页通常涉及两个关键参数:当前页码(Page Number)和每页记录数(PageSize)。例如,如果当前页码...
iBatis分页源代码解析.chm,ibatis介绍等
Ibatis SQLServerDialect 2008 分页 可实现SQLServerDialect 分页 支持ibatis3
#### 分页实现步骤 1. **定义分页相关的XML映射文件**:如代码所示,创建一个独立的XML文件,包含用于分页的动态SQL语句。这包括`page_begin`和`page_end`两个部分,分别处理分页的起始和结束逻辑。 2. **编写动态...