分页两种实现方式
pagehelper分页
1.controller
java
public JSONObject list(PageRegionReq pageRegionReq,HttpServletRequest request) {
JwAdminHeader aykYcmHeader=getAiyakuHeader(request);
if(null == aykYcmHeader){
return CommonUtil.errorJson(ErrorEnum.E_20011);
}
int pageNumber=pageRegionReq.getPageNumber();
if(null != pageRegionReq.getPage() && pageRegionReq.getPage()>0){
pageNumber=pageRegionReq.getPage();
}
pageRegionReq.setPageNumber(pageNumber);
Page<SysRegion> page=areaService.findRegionPage(pageRegionReq);
return CommonUtil.successPage(pageNumber, pageRegionReq.getPageSize(), page.getPages(),
page.getResult(), page.getTotal());
}
2.service实现
java
public Page<SysRegion> findRegionPage(PageRegionReq pageRegionReq) {
SysRegionExample sysRegionExample=new SysRegionExample();
SysRegionExample.Criteria criteria=sysRegionExample.createCriteria();
if(null != pageRegionReq.getRegionCode() && !"".equals(pageRegionReq.getRegionCode())){
criteria.andRegionCodeLike("%"+pageRegionReq.getRegionCode()+"%");
}
if(null != pageRegionReq.getRegionName() && !"".equals(pageRegionReq.getRegionName())){
criteria.andRegionNameLike("%"+pageRegionReq.getRegionName()+"%");
}
if(null != pageRegionReq.getIsProinve()){
criteria.andIsProinveEqualTo(pageRegionReq.getIsProinve());
}
if(null != pageRegionReq.getParentId()){
criteria.andParentIdEqualTo(pageRegionReq.getParentId());
}
criteria.andIsEnableEqualTo(true);
sysRegionExample.setOrderByClause(" region_orders asc ");
// TODO Auto-generated method stub
PageHelper.startPage(pageRegionReq.getPageNumber(),pageRegionReq.getPageSize());
return (Page<SysRegion>)sysRegionMapper.selectByExample(sysRegionExample);
}
mysql limit分页
1.controller
java
public JSONObject listUser(HttpServletRequest request) {
JwAdminHeader aykYcmHeader=getAiyakuHeader(request);
if(null == aykYcmHeader){
return CommonUtil.errorJson(ErrorEnum.E_20011);
}
return userService.listUser(CommonUtil.request2Json(request));
}
2.service实现
java
public JSONObject listUser(JSONObject jsonObject) {
CommonUtil.fillPageParam(jsonObject);
int count = userDao.countUser(jsonObject);
List<JSONObject> list = userDao.listUser(jsonObject);
if(null != list && list.size()>0){
List<SysPost> postList=findPostAll();
for (int i = 0; i < list.size(); i++) {
JSONObject json=list.get(i);
String postId=json.getString("postId");
String postName=getPostName(postList, postId);
json.put("postName", postName);
}
}
return CommonUtil.successPage(jsonObject, list, count);
}
3.xml配置
xml
<!-- 列表数量查询sql -->
<select id="countUser" resultType="Integer">
SELECT count(0)
FROM jw_sys_user u
WHERE u.delete_status = '1'
<if test="username!= null and username!=''">
and username like #{username}
</if>
<if test="mobile!= null and mobile!=''">
and mobile like #{mobile}
</if>
<if test="email!= null and email!=''">
and email like #{email}
</if>
</select>
<!-- 返回实体配置 -->
<resultMap id="userMap" type="com.jw.admin.util.model.One2Many">
<id column="userId" property="userId"/>
<result column="username" property="username"/>
<result column="nickname" property="nickname"/>
<result column="createTime" property="createTime"/>
<result column="updateTime" property="updateTime"/>
<result column="lastLoginTime" property="lastLoginTime"/>
<result column="sex" property="sex"/>
<result column="mobile" property="mobile"/>
<result column="email" property="email"/>
<result column="remark" property="remark"/>
<result column="roleName" property="roleName"/>
<result column="postName" property="postName"/>
<result column="postId" property="postId"/>
<result column="tenantName" property="tenantName"/>
</resultMap>
<!-- 列表查询sql -->
<select id="listUser" resultMap="userMap">
SELECT
u.id userId,
u.post_id postId,
u.username username,
u.is_enable isEnable,
u.nickname nickname,
u.is_admin isAdmin,
u.delete_status deleteStatus,
(select tenant_name from jw_sys_tenant t where t.tenant_id=u.tenant_id) as tenantName,
DATE_FORMAT(u.create_time, '%Y.%m.%d %T') createTime,
DATE_FORMAT(u.update_time, '%Y.%m.%d %T') updateTime,
sex,mobile,email,remark,
(select dept_name from jw_sys_dept d where d.dept_id=u.dept_id) as deptName,
(select GROUP_CONCAT(p.post_name) from jw_sys_post p where p.post_id in (u.post_id) ) as postName,
(select GROUP_CONCAT(r.role_name) from jw_sys_role r,jw_sys_user_role ur where ur.role_id=r.id and ur.user_id=u.id) as roleName
FROM jw_sys_user u
WHERE u.delete_status = '1'
<if test="username!= null and username!=''">
and u.username like concat('%', #{username}, '%')
</if>
<if test="nickname!= null and nickname!=''">
and u.nickname like concat('%', #{nickname}, '%')
</if>
<if test="mobile!= null and mobile!=''">
and u.mobile like concat('%', #{mobile}, '%')
</if>
<if test="email!= null and email!=''">
and u.email like concat('%', #{email}, '%')
</if>
ORDER BY u.id
LIMIT #{offSet}, #{pageRow}
</select>