分页代码实例

 分页用于从数据库搜索出符合条件的数据,分页显示
 
bo
public class exampleboimpl implements examplebo {	private exampledao exampledao;        //取出所有符合条件的数据	@override	public list<userinfo> selectexamplelistbypage(example example, pagebean pb)			throws businessexception {		return this.exampledao.selectexamplelistbypage(example, pb);	}        //计算当前数据的总是	@override	public int selectexamplelistcount(example example) throws businessexception {		return this.exampledao.getexamplelistcount(example);	}}
 dao
public class exampledaoimpl extends sqlsessiondaosupport implements exampledao{		private string mapper = "com.xxxxxx.example.";	@suppresswarnings("unchecked")	@override	public list<example> selectexamplelistbypage(example example, pagebean pb) {		map<string, object> map = new hashmap<string, object>();		map.put("example", example);		if(null != pb){			map.put("start", pb.getstartrow() - 1);			map.put("pagesize", pb.getpagesize());		}		return getsqlsession().selectlist(mapper + "selectexamplelistbypage", map);	}	@override	public int selectexamplelistcount(example example) {		return (integer) getsqlsession().selectone(mapper + "selectexamplelistcount", example);	}}
 controller
@controller@requestmapping(value = "/example")@sessionattributes("employee")public class examplecontroller {	private static logger logger = logger.getlogger(examplecontroller.class);	@autowired	private examplebo examplebo;		@requestmapping("/list.htm")	public modelandview listexample(@modelattribute("example") examplebean example,integer pageno) throws businessexception {		map<string, object> map = new hashmap<string, object>();		// 分页 构建pagebean		pagebean pb = null;		pb = new pagebean(pageno, examplebo.getexamplelistcount(example),10,7);		map.put("paging", pb);		// 列表显示所有住户信息		list<example> allexamplelist = this.examplebo.getexamplelist(example, pb,);		map.put("example", example);		map.put("allexamplelist", allexamplelist);		return new modelandview("/example/list", map);	}}
 pagebean的配置:
import org.apache.log4j.logger;public class pagebean {	private static logger log = logger.getlogger(pagebean.class);	// 当前页码	private int currentpage = 1;	// 总数量	private int totalcount = 0;	// 总页数	private int totalpage = 1;	// 开始页	private int startpage = 1;	// 结束页	private int endpage = 1;	// 每页大小	private int pagesize = 10;	// 每次显示多少页	private int showpages = 7;	// 第一页	private boolean first = false;	// 最后页	private boolean last = false;	// 上一页	private boolean pre = false;	// 下一页	private boolean next = false;	// 开始行 从1开始计算	private int startrow = 1;	// 结束行	private int endrow = 1;	// url	private int url;	public pagebean(integer currentpage, integer totalcount) {		if (totalcount == null) {			this.totalcount = 0;		} else {			this.totalcount = totalcount;		}		if (currentpage == null || currentpage <= 0) {			this.currentpage = 1;		} else {			this.currentpage = currentpage;		}		this.init();	}	public pagebean(integer currentpage, integer totalcount, integer pagesize,			integer showpages) {		if (totalcount == null) {			this.totalcount = 0;		} else {			this.totalcount = totalcount;		}		if (currentpage == null || currentpage <= 0) {			this.currentpage = 1;		} else {			this.currentpage = currentpage;		}		if (pagesize == null || pagesize == 0) {			this.pagesize = 10;		} else {			this.pagesize = pagesize;		}		if (showpages == null || showpages == 0) {			this.showpages = 10;		} else {			this.showpages = showpages;		}		// 初始化		this.init();	}	private void init() {		if (this.totalcount != 0) {			if ((this.totalcount / this.pagesize) != 0					&& (this.totalcount % this.pagesize == 0)) {				this.totalpage = this.totalcount / this.pagesize;			} else {				this.totalpage = this.totalcount / this.pagesize + 1;			}			if ((this.currentpage / this.showpages) != 0					&& (this.currentpage % this.showpages == 0)) {				this.startpage = (this.currentpage / this.showpages - 1)						* this.showpages + 1;			} else {				this.startpage = (this.currentpage / this.showpages)						* this.showpages + 1;			}			if ((this.startpage + this.showpages) <= this.totalpage) {				this.endpage = (this.startpage + this.showpages - 1);			} else {				this.endpage = this.totalpage;			}			// 校验??			if (this.endpage < this.startpage) {				this.startpage = (this.endpage / this.showpages)						* this.showpages + 1;			}			if (this.endpage <= this.currentpage) {				this.currentpage = this.endpage;			}			if (this.currentpage == 1) {				this.first = false;				this.pre = false;			} else {				this.first = true;				this.pre = true;			}			if (this.currentpage == this.totalpage) {				this.last = false;				this.next = false;			} else {				this.last = true;				this.next = true;			}			this.startrow = (this.currentpage - 1) * this.pagesize + 1;			this.endrow = this.startrow + this.pagesize - 1;		}		log.debug("currentpage::" + this.currentpage);		log.debug("totalcount::" + this.totalcount);	}}
 xml里面的搜索语句
<select id="selectexamplelistbypage" parametertype="map" resultmap="exampleresult">  select * from example_table   <if test="example.name!=null and example.name!=''">    and name = #{example.name}  </if>				  <if test="start != null and pagesize != null">    limit #{start},#{pagesize}	  </if></select><select id="selectexamplelistcount" parametertype="example" resulttype="int">  select count(*)  from example_table </select>
 页面的配置,将此句写在form里:
<jsp:include page="../paging/paging.jsp" />
  
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值