分页代码实例

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

 

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" />
 

 

你可能感兴趣的:(java,mybatis,分页)