SpringBoot分页条件查询操作
UserQuery
package com.xxxx.springboot.query;
/**
* 分页及条件查询
*/
public class UserQuery {
/*分页参数*/
private Integer pageNum = 1;//当前页
private Integer pageSize = 10;//每页显示的记录数量
/*条件查询的参数*/
private String userName;//查询条件:用户名
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;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
接口方法定义
package com.xxxx.springboot.dao;
import com.xxxx.springboot.po.User;
import com.xxxx.springboot.query.UserQuery;
import java.util.List;
public interface UserDao {
//分页条件查询
public List<User> queryUserList(UserQuery userQuery);
}
映射文件配置
<?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.xxxx.springboot.dao.UserDao">
<select id="queryUserList" parameterType="com.xxxx.springboot.query.UserQuery" resultType="com.xxxx.springboot.po.User">
select
*
from
student
<where>
<if test="userName != null and userName != ''">
and user_name like concat('%',#{userName},'%')
</if>
</where>
</select>
</mapper>
UserService
package com.xxxx.springboot.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.springboot.dao.UserDao;
import com.xxxx.springboot.po.User;
import com.xxxx.springboot.query.UserQuery;
import com.xxxx.springboot.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserService {
@Resource
private UserDao userDao;
/** 通过指定参数,分页查询用户列表
* @param userQuery
* @return
*/
public PageInfo<User> queryUserByParams(UserQuery userQuery) {
//开启分页
PageHelper.startPage(userQuery.getPageNum(), userQuery.getPageSize());
//返回分页对象
return new PageInfo<User>(userDao.queryUserList(userQuery));
}
}
UserController
package com.xxxx.springboot.controller;
import com.github.pagehelper.PageInfo;
import com.xxxx.springboot.exception.ParamsException;
import com.xxxx.springboot.po.ResultInfo;
import com.xxxx.springboot.po.User;
import com.xxxx.springboot.query.UserQuery;
import com.xxxx.springboot.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
public class UserController {
@Resource
private UserService userService;
/*** 通过指定参数,分页查询用户列表
* @param userQuery
* @return
*/
@GetMapping("user/list")
public PageInfo<User> list(UserQuery userQuery) {
return userService.queryUserByParams(userQuery);
}
}
浏览器测试
版权声明:本文为lln1540295459原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
发表回复