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

ssm+jsp+自定义标签实现分页,可以通用(后端实现)的更多相关文章

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

    近期做了一些分页方面的开发,大致梳理一下 1 jsp页面上关于分页的代码 <tr> <td colspan="9"> <ule1:pagination ...

  2. JSP 自定义标签

    0 标签技术的API继承体系 1 作用 jsp自定义标签用于移除页面中的java代码 2 实现 2.1 标签处理类ViewIPTag.java package com.zsm.util; import ...

  3. JSP自定义标签开发入门

    一般情况下开发jsp自定义标签需要引用以下两个包 import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; 首先我们需要大致了解开发 ...

  4. 一个简单的jsp自定义标签

    学到了一个简单的jsp自定义标签,后面有更多的例子,会更新出来: 例子1: 步骤: 1.编写标签实现类: 继承javax.servlet.jsp.tagext.SimpleTagSupport; 重写 ...

  5. jsp自定义标签分析

    jsp自定义标签的优势体现在于jsp页面上面减少了java代码. jsp自定义标签有三大部分组成,首先是类继承TagSupport,实现doStartTag方法. public int doStart ...

  6. JSP自定义标签库

    总所周知,JSP自定义标签库,主要是为了去掉JSP页面中的JAVA语句 此处以格式化输出时间戳为指定日期格式为例,简单介绍下JSP自定义标签的过程. 编写标签处理类(可继承自javax.servlet ...

  7. JSP自定义标签配置

    JSP自定义标签配置 JSP自定义标签 <taglib>         <taglib-uri>/WEB-INF/you.tld</taglib-uri>     ...

  8. jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题

    jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题 之前在项目中根据需求,需要自定义标签,经过查询w3c文档,自己也踩了一些坑,特此记录自定义标签的步骤,下面就以我之前的一个例子中的定义一 ...

  9. java JSP自定义标签

    来至: http://blog.csdn.net/jiangwei0910410003/article/details/23915373 http://blog.csdn.net/jiangwei09 ...

随机推荐

  1. uva 10020 Minimal coverage

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. 网络编程中几个地址结构与转换(in_addr,sockaddr,sockaddr_in,pcap_addr)

    man socket可以查看相关信息. socket头文件: #include <sys/types.h> /* See NOTES */ #include <sys/socket. ...

  3. 二叉查找树:Python实现

    #coding:utf8 #author:HaxtraZ class BST(object): """二叉查找树的简单实现""" def _ ...

  4. BZOJ3392: [Usaco2005 Feb]Part Acquisition 交易

    3392: [Usaco2005 Feb]Part Acquisition 交易 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 26  Solved:  ...

  5. cf500D New Year Santa Network

    D. New Year Santa Network time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Vim--写在开始

    这是Vim的学习记录,结合Vim使用技巧一书.

  7. Codeforces Round #299 (Div. 1)C. Tavas and Pashmaks (凸壳)

    C. Tavas and Pashmaks   Tavas is a cheerleader in the new sports competition named "Pashmaks&qu ...

  8. matrix矩阵求逆 与解方程模板 留做备用 (有bug,待补充)

    // // main.cpp // 矩阵求逆 // // Created by 唐 锐 on 13-6-20. // Copyright (c) 2013年 唐 锐. All rights reser ...

  9. CentOS下MySQL无法正常启动错误

    一.非正常关机/退出MySQL时报错:/var/lib/mysql/mysql.sock 1.重启机器:shutdown -h now 2.删除或重命名:rm -r /var/lib/mysql/my ...

  10. java随机数生成器

    一.java.lang.Math.Random 调用这个Math.Random()函数能够返回带正号的double值,取值范围是[0.0,1.0)的左闭右开区间,并在该范围内(近似)均匀分布. 二.j ...