基本过程
1.Controller
@RequestMapping("") @ResponseBody public Response userData(User user, Pager<User> pager, UserSearch<User> search) { //将用户账号查询设定为全模糊查询 search.like(User::getUserCode, Search.LIKE_BOTH); //将用户名查询设定为全模糊查询 search.like(User::getUserName, Search.LIKE_BOTH); //将查询设定为全模糊查询 PageHelper.startPage(pager.getPageNum(), pager.getPageSize(), pager.get()); List<UserDto> userDtoList = userService.findAll(user, search); PageResponse<UserDto> pageResponse = new PageResponse<>(userDtoList, pager); return Response.success(pageResponse); }
2.Service
List<UserDto> findAll(User user, Search<User> search);
3.ServiceImpl
@Override public List<UserDto> findAll(User user, Search<User> search) { return userDao.findAll(user,search); }
4.Dao
List<UserDto> findAll(@Param("user") User user, @Param("search") Search<User> search);
5.Mapper
<select id="findAll" resultMap="userDtoMap"> select <include refid="allColumn"/> from user where 1=1 <if test="user.id != null"> and id = #{user.id} </if> <if test="user.userCode != null and ''!= user.userCode"> and user_code = #{user.userCode} </if> <if test="user.userName != null and ''!= user.userName"> and user_name = #{user.userName} </if> <if test="user.password != null and ''!= user.password"> and password = #{user.password} </if> <if test="user.typeCode != null and ''!= user.typeCode"> and type_code = #{user.typeCode} </if> <if test="user.groupCode != null and ''!= user.groupCode"> and group_code = #{user.groupCode} </if> <if test="user.address != null and ''!= user.address"> and address = #{user.address} </if> <if test="user.mobile != null and ''!= user.mobile"> and mobile = #{user.mobile} </if> <if test="user.phoneNum != null and ''!= user.phoneNum"> and phone_num = #{user.phoneNum} </if> <if test="user.eMail != null and ''!= user.eMail"> and e_mail = #{user.eMail} </if> <if test="user.roleId != null"> and role_id = #{user.roleId} </if> <if test="user.userLock != null"> and user_lock = #{user.userLock} </if> and is_del != 2 </select>
分页使用到工具类
1.Pager
public class Pager<T> { /** * 升序 */ public static final String SORT_ASC = "asc"; /** * 降序 */ public static final String SORT_DESC = "desc"; /** * 存储排序的集合 */ private List<String> sortInfo = new ArrayList<>(); /** * url地址 */ private String pageUrl; /** * 当前页 */ private Integer pageNum = 1; /** * 每页分页数 */ private Integer pageSize = 20; /** * 添加排序信息 * * @author fengzx * @date 2020/3/26 19:55 */ public Pager<T> add(IGetter<T> fn, String sort) { //属性名 String propertyName = BeanUtils.convertToFieldName(fn); String columnName = BeanUtils.HumpToUnderline(propertyName); sortInfo.add(columnName + " " + sort); return this; } /** * 获取排序信息 * * @author fengzx * @date 2020/3/26 19:54 */ public String get() { return StringUtils.join(sortInfo.toArray(), ","); } public String getPageUrl() { return pageUrl; } public void setPageUrl(String pageUrl) { this.pageUrl = pageUrl; } public Integer getPageNum() { return pageNum; } public void setPageNum(Integer pageNum) { this.pageNum = pageNum; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } }
2.来自com.github.pagehelper.PageHelper的PageHelper类
3.返回结果类
public class PageResponse<T> extends PageInfo<T> { /** * ajax请求地址 */ private String pageUrl; public PageResponse(List<T> list, Pager pager) { super(list); this.pageUrl = pager.getPageUrl(); } public String getPageUrl() { return pageUrl; } public void setPageUrl(String pageUrl) { this.pageUrl = pageUrl; } }
返回结果
{ "type": "success", "data": { "navigatepageNums": [ 1 ], "startRow": 1, "hasNextPage": false, "prePage": 0, "nextPage": 0, "endRow": 3, "pageSize": 20, "list": [ { "gmtModified": 1624949479000, "address": "www", "gmtUserLock": 1614046814000, "gmtCreated": 1614046814000, "roleId": 100, "mobile": "13958352935", "phoneNum": "13958352935", "userLock": 1, "userName": "管理员", "userCode": "admin", "eMail": "2222", "typeCode": "100-100100", "password": "e10adc3949ba59abbe56e057f20f883e", "id": 1, "isDel": 1, "groupCode": "100-100100", "hospitalDeptName": "西京医院-眼科" }, { "gmtModified": 1625120041000, "address": "123123", "gmtCreated": 1625120041000, "roleId": 100, "mobile": "213123", "phoneNum": "123123", "userLock": 1, "userName": "312", "userCode": "123123", "eMail": "123123", "typeCode": "100-100100", "password": "e10adc3949ba59abbe56e057f20f883e", "id": 13, "isDel": 1, "groupCode": "100-100100", "hospitalDeptName": "西京医院-眼科" }, { "gmtModified": 1625191309000, "address": "杭州市滨江区", "gmtCreated": 1625191218000, "roleId": 100, "mobile": "13839448710", "phoneNum": "13839448710", "userLock": 1, "userName": "test1", "userCode": "test", "eMail": "13839448710@163.com", "typeCode": "100-100100", "password": "e10adc3949ba59abbe56e057f20f883e", "id": 14, "isDel": 1, "groupCode": "100-100100", "hospitalDeptName": "西京医院-眼科" } ], "pageNum": 1, "navigatePages": 8, "navigateFirstPage": 1, "total": 3, "pages": 1, "size": 3, "isLastPage": true, "hasPreviousPage": false, "navigateLastPage": 1, "isFirstPage": true }, "code": null, "msg": null }