Bootstrap和Java分页实例第一篇(2)

package com.app.pagination.impl; import java.util.List; /** * 分页查询回调接口 * @author: super.wwz@hotmail.com * @ClassName: QueryHandler * @Version: v0.1 * @param <T> */ public interface QueryHandler<T> { /** * 获取数据总行数 * @return */ int getTotalElements(); /** * 获取当前页的数据 * @param pageIndex * @param pageSize * @return */ List<T> getCurrData(int pageIndex, int pageSize); }

BookDaoImpl

BookDao的实现类(BookDao接口已经省略)

package com.app.dao.impl; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import com.app.bean.Book; import com.app.dao.BaseDao; import com.app.dao.BookDao; public class BookDaoImpl extends BaseDao implements BookDao { @Override public int count() { // 查询数据总行数 String sql = "select count(1) from t_book"; try { return getQueryRunner().query(sql, new ScalarHandler<Integer>()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } @Override public List<Book> getBooks(int pageIndex, int pageSize) { // 关于SQLServer的查询分页sql StringBuffer sql = new StringBuffer(); sql.append("select * from ("); sql.append(" select row_number() over(order by(id)) new_id,* from t_book"); sql.append(") t where new_id between ? and ?"); try { return getQueryRunner().query(sql.toString(), new BeanListHandler<Book>(Book.class), pageSize * (pageIndex - 1) + 1,pageSize * pageIndex); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }

BookServiceImpl

BookService业务逻辑接口的实现类 (BookService已经省略)

package com.app.service.impl; import java.util.List; import com.app.bean.Book; import com.app.dao.BookDao; import com.app.dao.impl.BookDaoImpl; import com.app.pagination.Pagination; import com.app.pagination.impl.DefaultPagination; import com.app.pagination.impl.QueryHandler; import com.app.service.BookService; /** * 业务逻辑层查询分页数据示例 * @author: super.wwz@hotmail.com * @ClassName: BookServiceImpl * @Version: v0.1 */ public class BookServiceImpl implements BookService { private BookDao bookDao = new BookDaoImpl(); @Override public Pagination<Book> getBookList(int pageIndex, int pageSize,int pageLinkNumber) { // TODO Auto-generated method stub return new DefaultPagination<Book>(pageIndex, pageSize, new QueryHandler<Book>() { @Override public int getTotalElements() { // TODO Auto-generated method stub return bookDao.count(); } @Override public List<Book> getCurrData(int pageIndex, int pageSize) { // TODO Auto-generated method stub return bookDao.getBooks(pageIndex, pageSize); } },pageLinkNumber); } }

BookAction

有关图书的Servlet控制器

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwjsgp.html