从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. android R.id.转化为view

    LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view ...

  2. 虚拟机LUN扩大后,重新分区

    [root@ywcrmdb ~]# fdisk -l Disk /dev/sda: 751.6 GB, 751619276800 bytes 255 heads, 63 sectors/track, ...

  3. 【转】Android adb shell操作时出现“ XXX ... Read-only file system”解决办法--不错

    原文网址:http://blog.csdn.net/whu_zhangmin/article/details/25364349 手机连接PC后 adb shell su rm -r /system/a ...

  4. N次剩余

    $$求解x^n\equiv a(\%P),其中P是质数,0\leq x<P$$ 设$g$是$P$的原根 那么$g^0,g^1,...,g^{P-2}$和$1,2,...,P-1$是一一对应的. ...

  5. SPOJ3267--D-query (主席树入门练习)

    题意:查找区间内不同数字的个数. 两种做法,一种是 树状数组离线,另一种就是主席树. 树状数组离线操作的链接 http://www.cnblogs.com/oneshot/p/4110415.html ...

  6. hdu 4746 Mophues

    莫比乌斯反演.先初始化出所有数有多少个质因子和mobius.然后处理mob_sum[ i ][ j ],表示当公因子的因子个数小于等于 j 个的mobius前 i 项和.然后分块求和即可. 分块处理部 ...

  7. 【转】asp.net mvc webapi+angular.js案例

    参考地址:http://www.mamicode.com/info-detail-892383.html 大家好,本文用一个简单的demo演示AngularJS在MVC中的使用,在学习这个demo之前 ...

  8. java反射--注解的定义与运用以及权限拦截

    自定义注解类编写的一些规则: 1. Annotation型定义为@interface, 所有的Annotation会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是 ...

  9. 对list集合去重操作

    import java.util.ArrayList; import java.util.List; //删除集合中重复的数据 public class RemoteTheSameDataInList ...

  10. muduo源代码分析--我对muduo的理解

    分为几个模块 EventLoop.TcpServer.Acceptor.TcpConnection.Channel等 对于EventLoop来说: 他仅仅关注里面的主驱动力,EventLoop中仅仅关 ...