1.BaseAction
// 默认每页显示条数
protected static final Integer DEFUALT_ROWS = 10;
// 默认显示第几页
protected static final Integer DEFUALT_PAGE = 1;
private Integer rows;// 每页显示的记录数
private Integer page;// 当前第几页
public Integer getPage() {
if (this.page == null) {
this.page = DEFUALT_PAGE;
}
return page;
}
public Integer getRows() {
if (this.rows == null) {
this.rows = DEFUALT_ROWS;
}
return rows;
}
这两个方法在调用action层调用分页查询时调用
this.buserPage = this.buserAdminService.getBusersByCondition(buserId, buserName, super.getPage(), super.getRows(), true);
@Override
public ReturnDataListWithPage<MvBuser> getBusersByCondition(Long buserId, String bUserName, int currentPage, int onePageSize, boolean isNeedTotal) {
ReturnDataListWithPage<MvBuser> returnData = new ReturnDataListWithPage<MvBuser>(currentPage, onePageSize);
if (currentPage < 1 || onePageSize < 1) {
returnData.setReturnCode(ReturnCode.EX_PARAM.getCode());
return returnData;
}
try {
if (isNeedTotal) {
int count = dao.getBusersByConditionCount(buserId, bUserName);
returnData.setTotalResults(count);
}
int startRow = returnData.getFirstResult();
List<MvBuser> result = dao.getBusersByCondition(buserId, bUserName, startRow, onePageSize);
returnData.setResultData(result);
returnData.setReturnCode(ReturnCode.PASS_OK.getCode());
} catch (Exception e) {
log.error("MvBuserServiceImpl exception:get busers by page failed ", e);
returnData.setReturnCode(ReturnCode.EX_UNKNOWN.getCode());
}
在returnData对象的构造方法中初始化 查询开始条数,通过get方法获得其值
public ReturnDataListWithPage(int currentPage, int onePageSize) {
if (currentPage > 1)
this.currentPage = currentPage;
else
this.currentPage = 1;
this.onePageSize = onePageSize;
this.firstResult = (this.currentPage - 1) * this.onePageSize;
}
<select id="getBusersByConditionCount" resultType="int">
SELECT COUNT(*) FROM mv_buser
WHERE 1 = 1
<if test="buser_id != null">AND buser_id = #{buser_id} </if>
<if test="user_name != null "> AND user_name LIKE CONCAT(CONCAT(‘%‘, #{user_name}),‘%‘)</if>
</select>
<select id="getBusersByCondition" resultType="mvBuser">
SELECT * FROM mv_buser
WHERE 1 = 1
<if test="buser_id != null">AND buser_id = #{buser_id} </if>
<if test="user_name != null "> AND user_name LIKE CONCAT(CONCAT(‘%‘, #{user_name}),‘%‘)</if>
ORDER BY buser_id
LIMIT #{startRow}, #{onePageSize}
</select>
原文:http://www.cnblogs.com/yangfei-beijing/p/4959863.html