- public int getTotalRows() {
- return totalRows;
- }
- public void first() {
- currentPage = 1 ;
- startRow = 0 ;
- }
- public void previous() {
- if (currentPage == 1 ) {
- return ;
- }
- currentPage -- ;
- startRow = (currentPage - 1 ) * pageSize;
- }
- public void next() {
- if (currentPage < totalPages) {
- currentPage ++ ;
- }
- startRow = (currentPage - 1 ) * pageSize;
- }
- public void last() {
- currentPage = totalPages;
- startRow = (currentPage - 1 ) * pageSize;
- }
- public void refresh( int _currentPage) {
- currentPage = _currentPage;
- if (currentPage > totalPages) {
- last();
- }
- }
- }
PagerService 类,主要有个 getPager 方法返回 Pager 类。代码如下:


- package com.game.commons;
- public class PagerService {
- public Pager getPager(String currentPage,String pagerMethod, int totalRows) {
- // 定义pager对象,用于传到页面
- Pager pager = new Pager(totalRows);
- // 如果当前页号为空,表示为首次查询该页
- // 如果不为空,则刷新pager对象,输入当前页号等信息
- if (currentPage != null ) {
- pager.refresh(Integer.parseInt(currentPage));
- }
- // 获取当前执行的方法,首页,前一页,后一页,尾页。
- if (pagerMethod != null ) {
- if (pagerMethod.equals( " first " )) {
- pager.first();
- } else if (pagerMethod.equals( " previous " )) {
- pager.previous();
- } else if (pagerMethod.equals( " next " )) {
- pager.next();
- } else if (pagerMethod.equals( " last " )) {
- pager.last();
- }
- }
- return pager;
- }
- }
这个分页方法比较简单,而且功能也齐全,许多页面级的开源 table 中分页很多也是基于这个原理,所以理解了这个分页,对其他各种分页技术的理解也就迎刃而解了。
服务层的代码就这些了,接下来就可以写 spring 的配置文件来用 spring 管理这些 Dao 和 Service 了。
在 spring-context 包中新建 applicationContext.xml 。配置的写法如下:
- <? xml version="1.0" encoding="ISO-8859-1" ?>
- <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
- < beans >
- <!-- dataSource config -->
- < bean id ="dataSource" class ="org.springframework.jndi.JndiObjectFactoryBean" >
- < property name ="jndiName" >
- < value > java:comp/env/jdbc/game </ value >
- </ property >
- </ bean >
- <!-- SessionFactory -->
- < bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
- < property name ="dataSource" >
- < ref bean ="dataSource" />
- </ property >
- < property name ="configLocation" >
- < value > classpath:com\game\bean\hibernate\hibernate.cfg.xml </ value >
- </ property >
- </ bean >
- <!-- TransactionManager -->
- < bean id ="transactionManager"
- class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
- < property name ="sessionFactory" >
- < ref local ="sessionFactory" />
- </ property >
- </ bean >
- <!-- DAO -->
- < bean id ="productsDao" class ="com.game.products.dao.hibernate.ProductsMapDao" >
- < property name ="sessionFactory" >
- < ref bean ="sessionFactory" />
- </ property >
- </ bean >
- <!-- Services -->
- < bean id ="productsService" class ="com.game.products.services.ProductsServiceImp" >
- < property name ="productsDao" >
- < ref bean ="productsDao" />
- </ property >
- </ bean >
- < bean id ="pagerService" class ="com.game.commons.PagerService" >
- </ bean >
- </beans>
配置文件不难,主要是些 IOC 控制。数据库链接我采用的是数据源方式,需要在 tomcat 的conf文件夹下的 server.xml 中添加数据源,添加的数据如下:
- < Context path ="/game" docBase ="D:\tomcat-5.5.20\webapps\game" debug ="0" reloadable ="true" >
- < Resource
- name ="jdbc/game"
- type ="javax.sql.DataSource"
- password =""
- driverClassName ="net.sourceforge.jtds.jdbc.Driver"
- maxIdle ="2"
- maxWait ="5000"
- username ="sa"
- url ="jdbc:jtds:sqlserver://127.0.0.1:16899/game"
- maxActive ="4" />
- </ Context >
这个数据源是针对 tomcat 5.5 以上版本的,以下版本的写法有所不同,不同之处可以用 google 搜索得知。这个数据源很简单,并没有过多的配置来优化系统,只是为了让项目更容易让人理解。需要注意都是,我的数据链接的JDBC包是jtds包,而不是普通的那个三个jar包。