1. 需求分析
需求:实现多条件分页组合查询
2. UserVo编写
package com.lagou.domain;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
import java.util.List;
public class UserVo {
private Integer currentPage;
private Integer pageSize;
// 多条件查询:用户名(手机号)
private String username;
// 注册起始时间 2020/11/11 2020-08-04
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date startCreateTime;
// 注册结束时间
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date endCreateTime;
private List<Integer> roleIdList;
private Integer userId;
public List<Integer> getRoleIdList() {
return roleIdList;
}
public void setRoleIdList(List<Integer> roleIdList) {
this.roleIdList = roleIdList;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getStartCreateTime() {
return startCreateTime;
}
public void setStartCreateTime(Date startCreateTime) {
this.startCreateTime = startCreateTime;
}
public Date getEndCreateTime() {
return endCreateTime;
}
public void setEndCreateTime(Date endCreateTime) {
this.endCreateTime = endCreateTime;
}
}
2. Dao层:UserMapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lagou.dao.UserMapper">
<!--用户分页&多条件组合查询 public List<User> findAllUserByPage(UserVo userVo);-->
<select id="findAllUserByPage" parameterType="com.lagou.domain.UserVo" resultType="com.lagou.domain.User">
select * from user
<where>
<if test="true">
and is_del !=1
</if>
<if test="username !=null and username !=''">
and name = #{username}
</if>
<if test="startCreateTime !=null and endCreateTime !=null">
and create_time BETWEEN #{startCreateTime} AND #{endCreateTime}
</if>
</where>
</select>
</mapper>
4. Service层:UserService编写
package com.lagou.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.dao.UserMapper;
import com.lagou.domain.*;
import com.lagou.service.UserService;
import com.lagou.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
/*
用户分页&多条件组合查询
*/
@Override
public PageInfo findAllUserByPage(UserVo userVo) {
PageHelper.startPage(userVo.getCurrentPage(),userVo.getPageSize());
List<User> allUserByPage = userMapper.findAllUserByPage(userVo);
PageInfo<User> pageInfo = new PageInfo<>(allUserByPage);
return pageInfo;
}
}
5. Web层:UserController编写
package com.lagou.controller;
import com.github.pagehelper.PageInfo;
import com.lagou.domain.ResponseResult;
import com.lagou.domain.Role;
import com.lagou.domain.User;
import com.lagou.domain.UserVo;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/*
用户分页&多条件组合查询方法
*/
@RequestMapping("/findAllUserByPage")
public ResponseResult findAllUserByPage(@RequestBody UserVo userVo){
PageInfo pageInfo = userService.findAllUserByPage(userVo);
return new ResponseResult(true,200,"分页多条件查询成功",pageInfo);
}
}
节选自拉钩教育JAVA系列课程