ssm+jsp+自定义标签实现分页,可以通用(后端实现)

从controller到mapper.xml的后端实现

只梳理分页相关代码

1 controller里面相关

ModelAndView mv = new ModelAndView("/listDesc/listingDescPage");
int pageSize = 2;
List<ListingDesc> dataDto = new ArrayList<ListingDesc>();
List<ListingDesc> totalList;
ListingDescServiceClient listingDescServiceClient = ItemTools
.getListingDescServiceClient();
// 获取总记录数,准备分页
totalList = listingDescServiceClient
.findListingDescByIListingDesc(listingDesc);
// 分页工具
PaginationList<ListingDesc> pageMode = new PaginationList<ListingDesc>();
pageMode.setPageIndex(currentPage);
pageMode.setPageSize(pageSize);
dataDto = listingDescServiceClient.findListingByCondition(
listingDesc, pageMode);
log.info("共查询出》》》》" + totalList.size() + "条数据");
this.getPage(request, pageSize, totalList.size());

private int getPage(HttpServletRequest request, Integer recordPageCount,
Integer totalCount) {

request.setAttribute("totalPageCount", totalPageCount);
request.setAttribute("totalCount", totalCount);
request.setAttribute("pageSize", 2);
return startNum;}

getpage()方法里面只需把三个参数总页数,当前页,每页显示条数封装进request即可,我这当前页在别的地方已封装过所以此处没封装,在此处封装了记录总条数,其实在自定义标签里是用不到的。

2 service

List<ListingDesc> list = null;
try{
if(listingDesc!= null){
//获取总记录数
int totalRecordCount = listingDescService.findListingDescByListingDesc(listingDesc).size();
pageMode.setTotalRecords(totalRecordCount);
//计算每页起始记录
int firstresult = (pageMode.getPageIndex()-1)*pageMode.getPageSize();
list =listingDescService.findListingByCondition(listingDesc, firstresult, pageMode.getPageSize());
logger.info("ListingDescServiceClient:findListingByCondition method end=====returnList is "+ list);
}else{
logger.info("ListingDescServiceClient:findListingByCondition method end=====params listingDesc is null.");
return null;
}
}catch(Exception e){
e.getStackTrace();
}
return (PaginationList<ListingDesc>)list;

我用的是ejb架构,在此和ssm架构稍有不同,这段代码直接拿到service下改造一下即可

3 dao相关


// 根据listingDesc,firstResult,maxResult查找并分页
@Override
public PaginationList<ListingDesc> findListingByCondition(
ListingDesc listingDesc, int firstResult, int maxResults) {
return listingDescDao.findListingByCondition(listingDesc, firstResult,
maxResults);
}

@Override
public PaginationList<ListingDesc> findListingByCondition(
ListingDesc listingDesc, int firstResult, int maxResults) {
return (PaginationList<ListingDesc>) this.findByExampleForPage(
"findListingDescByCondition", listingDesc, firstResult,
maxResults);
}

public abstract class BaseDao<T, PK> implements IDao<T, PK> {

@Autowired
private SqlSessionTemplate sqlSession;

*/
protected PaginationList findByExampleForPage(String sqlId, Object param, int firstResult, int maxResults) {
UleRowBounds rowBounds = new UleRowBounds(firstResult, maxResults, UleRowBounds.TYPE_PAGE_SELECT);
return (PaginationList) sqlSession.selectList(this.getClass().getName() + "." + sqlId, param, rowBounds);
}

4 mapper

<select id="findListingDescByCondition" resultType="ListingDesc"
parameterType="ListingDesc">
SELECT
SEQ_ID seqId,
LIST_ID listId,
DESC_NAME descName,
DESC_VALUE
descValue,
SORT_NUM sortNum,
DEL_FLAG delFlag,
CREATE_TIME createTime,
UPDATE_TIME updateTime
FROM LISTING_DESC
<where>
<if test="seqId != null"> SEQ_ID = #{seqId}</if>
<if test="listId != null"> AND LIST_ID = #{listId}</if>
<if test="descName != null and descName != ''"> AND DESC_NAME = #{descName}</if>
</where>
</select>

至此分页后端完成,关于dao层的mybatis实现自己可以查看一些相关资料。主要是dao实现类继承baseDao:

@Repository
public class ListingDescDaoImpl extends BaseDao<ListingDesc, Long> implements ListingDescDao 

然后baseDao实现IDao接口

public abstract class BaseDao<T, PK> implements IDao<T, PK> {

@Autowired
private SqlSessionTemplate sqlSession;

里面可以用 SqlSessionTemplate sqlSession的api实现相关增删改查。

这是Idao里关于分页的内容:

public interface IDao<T, PK> {

/**
* 分页查询数据,返回分页结构对象PaginationList,该方法包含两次SQL查询,一次查询数据,一次统计总结果数
* @param t 查询参数对象
* @param firstResult 查询结果第一次记录的偏移量,表示从第几条数据开始返回
* @param maxResults 返回结果集大小
* @return
*/
public PaginationList findByExampleForPage(T t, int firstResult, int maxResults);

/**
* 分页查询数据,返回分页结构对象PaginationList,该方法包含两次SQL查询,一次查询数据,一次统计总结果数
* @param t 查询参数对象
* @param firstResult 查询结果第一次记录的偏移量,表示从第几条数据开始返回
* @param maxResults 返回结果集大小
* @param orders SQL排序子串
* @return
*/
public PaginationList findByExampleForPage(T t, int firstResult, int maxResults, Order... orders);

public static class Order{
private boolean ascending;
private String propertyName;

public String toString() {
return propertyName + ' ' + (ascending?"asc":"desc");
}
protected Order(String propertyName, boolean ascending) {
this.propertyName = propertyName;
this.ascending = ascending;
}

public static Order asc(String propertyName) {
return new Order(propertyName, true);
}

public static Order desc(String propertyName) {
return new Order(propertyName, false);
}
}
}

原文地址:https://www.cnblogs.com/zhaoblog/p/5309743.html