读书人

分页代码范例

发布时间: 2012-10-28 09:54:44 作者: rapoo

分页代码实例

?分页用于从数据库搜索出符合条件的数据,分页显示

?

BO

public class exampleBOImpl implements exampleBO {private ExampleDAO exampleDAO;        //取出所有符合条件的数据@Overridepublic List<UserInfo> selectExampleListByPage(Example example, PageBean pb)throws BusinessException {return this.exampleDAO.selectExampleListByPage(example, pb);}        //计算当前数据的总是@Overridepublic 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")@Overridepublic 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);}@Overridepublic 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);@Autowiredprivate ExampleBO exampleBO;@RequestMapping("/list.htm")public ModelAndView listExample(@ModelAttribute("example") ExampleBean example,Integer pageNo) throws BusinessException {Map<String, Object> map = new HashMap<String, Object>();// 分页 构建pageBeanPageBean 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;// urlprivate 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" />
?

?

读书人网 >编程

热点推荐