Flutter开发App的未来及其在各行业的应用潜力分析
1879
2023-05-05
Mybatis分页插件PageHelper配置及使用方法详解
环境
框架:spring+springmvc+mybatis
pom.xml
配置全局配置文件
在mybatis的全局配置文件中配置PageHelper分页插件
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
使用
例如:实现对用户的多条件查询
package com.szfore.model;
import java.util.Date;
import java.util.List;
public class User {
private Integer id;
private String uname;
private String pwd;
private String name;
private Integer sex;
private String phone;
private String company;
private String jobtitle;
private String birth;
private Date createdate;
private Date lastlogintime;
private List
public List
return roleList;
}
public void setRoleList(List
this.roleList = roleList;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname == null ? null : uname.trim();
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd == null ? null : pwd.trim();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company == null ? null : company.trim();
}
public String getJobtitle() {
return jobtitle;
}
public void setJobtitle(String jobtitle) {
this.jobtitle = jobtitle == null ? null : jobtitle.trim();
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth == null ? null : birth.trim();
}
public Date getCreatedate() {
return createdate;
}
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
public Date getLastlogintime() {
return lastlogintime;
}
public void setLastlogintime(Date lastlogintime) {
this.lastlogintime = lastlogintime;
}
}
UserMapper
注意:mapper中就按不分页的那种写法就好
package com.szfore.dao;
import com.szfore.model.User;
import com.szfore.model.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
/**
* 多条件分页查询
* @param userParam
* @return
*/
public List
}
UserMapper.xml
注意:sql中就不要写limit了,pageHelp会自己处理,sql就按不分页的那种写法就好
SELECT
*
FROM
`user`
AND id = #{id}
AND uname = #{uname}
AND name like '%${name}%'
AND phone like '%${phone}%'
AND company like '%${company}%'
AND jobTitle like '%${jobtitle}%'
AND birth like '%${birth}%'
UserServiceImpl
package com.szfore.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szfore.dao.MenuMapper;
import com.szfore.dao.UserMapper;
import com.szfore.dao.UserRoleMapper;
import com.szfore.model.*;
import com.szfore.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
imporbcqzjat org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl implements IUserService{
@Autowired
private UserMapper userMapper;
@Autowired
private MenuMapper menuMapper;
@Autowired
private UserRoleMapper userRoleMapper;
/**
* 多条件分页查询用户
* @param userParam
* @param pageNum
* @param pageSize
* @return
*/
public json queryByPage(User userParam,Integer pageNum,Integer pageSize) {
//利用PageHelper分页查询 注意:这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
PageHelper.startPage(pageNum, pageSize);
List
PageInfo
Json json = new Json();
json.setMsg("成功!");
json.setObj(pageInfo);
json.setSuccess(true);
return json;
}
}
说明:PageInfo是PageHelper自带的分页对象类,详情如下:
当前页
private int pageNum;
每页的数量
private int pageSize;
当前页的数量
private int size;
//由于startRow和endRow不常用,这里说个具体的用法
//可以在页面中"显示startRow到endRow 共size条数据"
当前页面第一个元素在数据库中的行号
private int startRow;
当前页面最后一个元素在数据库中的行号
private int endRow;
总记录数
private long total;
总页数
private int pages;
结果集
private List
第一页
private int firstPage;
前一页
private int prePage;
是否为第一页
private boolean isFirstPage = false;
是否为最后一页
private boolean isLastPage = false;
是否有前一页
private boolean hasPreviousPage = false;
是否有下一页
private boolean hasNextPage = false;
导航页码数
private int navigatePages;
所有导航页号
private int[] navigatbcqzjaepageNums;
通过PageInfo获取其他信息
PageHelper.startPage(req.getCurrentPage(), req.getPageSize(), true);
List
PageInfo page=new PageInfo(list);
page.getTotal();
page.xxxx
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~