java分页查询接口的实现
分页要传入当前所在页数和每页显示记录数,再分页查询数据库,部分代码如下所示。
传入参数实体类:
public class MessageReq {
private String memberId;//会员id
private int currentPage;//当前页
private int pageSize;//一页多少条记录
private int startIndex;//从哪一行开始
private int endIndex;//从哪一行结束
public String getMemberId) {
return memberId;
}
public void setMemberIdString memberId) {
this.memberId = memberId;
}
public int getCurrentPage) {
return currentPage;
}
public int getStartIndex) {
return startIndex;
}
public int getEndIndex) {
return endIndex;
}
public void setCurrentPageint currentPage) {
this.currentPage = currentPage;
}
public void setStartIndexint startIndex) {
this.startIndex = startIndex;
}
public void setEndIndexint endIndex) {
this.endIndex = endIndex;
}
public int getPageSize) {
return pageSize;
}
public void setPageSizeint pageSize) {
this.pageSize = pageSize;
}
//根据当前所在页数和每页显示记录数计算出startIndex和endIndex
public void setStartIndexEndIndex){
this.startIndex=this.getCurrentPage)-1)*this.getPageSize);
this.endIndex= this.getCurrentPage)-1)*this.getPageSize)+this.getPageSize);
}
}
分页工具类:
public class Page<T>{
private int currentPage = 1; // 当前页
private int pageSize = 20; //每页显示记录数
private int startRecord = 1; //起始查询记录
private int totalPage = 0; //总页数
private int totalRecord = 0; //总记录数
private List<T> datas;
public Page){}
public Pageint currentPage, int pageSize) {
this.currentPage = currentPage;
this.pageSize = pageSize;
ifthis.currentPage <= 0) {
this.currentPage = 1;
}
ifthis.pageSize <=0) {
this.pageSize = 1;
}
}
public Pageint currentPage, int pageSize, int totalRecord) {
thiscurrentPage, pageSize);
this.totalRecord = totalRecord;
ifthis.totalRecord <=0) {
this.totalRecord = 1;
}
}
public int getCurrentPage) {
ifcurrentPage <= 0) {
return 1;
}
return currentPage;
}
public void setCurrentPageint currentPage) {
this.currentPage = currentPage;
}
public int getPageSize) {
return pageSize;
}
public void setPageSizeint pageSize) {
this.pageSize = pageSize;
}
public int getTotalRecord) {
iftotalRecord < 0) {
return 0;
}
return totalRecord;
}
public void setTotalRecordint totalRecord) {
this.totalRecord = totalRecord;
}
public List<T> getDatas) {
return datas;
}
public void setDatasList<T> datas) {
this.datas = datas;
}
public int getTotalPage) {
iftotalRecord <= 0) {
return 0;
}
int size = totalRecord / pageSize;//总条数/每页显示的条数=总页数
int mod = totalRecord % pageSize;//最后一页的条数
ifmod != 0) {
size++;
}
totalPage = size;
return totalPage;
}
public int getStartRecord) {
startRecord = getCurrentPage) - 1) * pageSize;
return startRecord;
}
}
Manager层
public interface MessageManager {
//分页查询消息
public Page<Message> queryMessageMessageReq req);
}
@Component
public class MessageManagerImpl implements MessageManager{
@Autowired
private MessageMapper messageMapper;
@Override
public Page<Message> queryMessageMessageReq req) {
Page<Message> page = new Page<Message>);
int pageCount = messageMapper.getMessageNumreq.getMemberId));//得到总条数
page = initPagepage, pageCount, req);
List<Message> message= messageMapper.queryMessagereq);
if !message.isEmpty)) {
page.setDatasmessage);
}
return page;
}
private Page<Message> initPagePage<Message> page, int pageCount,
MessageReq messageReq) {
page.setTotalRecordpageCount);
page.setCurrentPagemessageReq.getCurrentPage));
page.setPageSizemessageReq.getPageSize));
messageReq.setStartIndexEndIndex);
return page;
}
}
Dao层
public interface MessageMapper {
//分页查询
public List<Message> queryMessageMessagereq);
//查询总条数
public int getMessageNumString memberId);
}
mybatis的.xml文件
<?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.sf.ccsp.member.dao.mapper.MessageMapper">
<resultMap id="MessageResultMap" type="com.sf.ccsp.member.dao.domain.message.Message" >
<result column="ID" property="id" jdbcType="VARCHAR" />
<result column="MEMBERID" property="memberId" jdbcType="VARCHAR" />
<result column="MESSAGE_CLASSIFY" property="messageClassify" jdbcType="VARCHAR" />
<result column="MESSAGE_CODE" property="messageCode" jdbcType="VARCHAR" />
<result column="MESSAGE_CONTENT" property="messageContent" jdbcType="VARCHAR" />
<result column="MESSAGE_STATUS" property="messageStatus" jdbcType="VARCHAR" />
</resultMap>
<select id="queryMessage" resultMap="MessageResultMap" parameterType="com.sf.ccsp.member.client.request.MessageReq">
select *
from cx_customer_message
where MEMBERID = #{memberId, jdbcType=VARCHAR}
and ISVALID = '1'
LIMIT #{startIndex,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
</select>
<select id="getMessageNum" resultType="INTEGER" parameterType="String">
select count*)
from cx_customer_message
where MEMBERID = #{memberId, jdbcType=VARCHAR}
</select>
</mapper>