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);
}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM