SSM+Maven_手动分页

创建手动分页工具类:

import java.util.List;
/**
 * 分页工具类
 */
public class PageBean<T> {
    //当前页--路径中提交的参数
    private Integer currPage;
    //每页显示的数据量--路径中提交的参数
    private Integer pageSize;
    //总条目数--当前表的所有数据(通过查询数据库得知)select count(*) from product
    private Integer totalCount;
    //总页数--计算得出 Math.ceil(totalCount/pageSize) Math.ceil表示向上取整(该函数需要double类型的数据)
    private Integer totalPage;
    //当前页集合数据
    private List<T> list;

    public Integer getCurrPage() {
        return currPage;
    }

    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}

控制层:

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService productService;
    /**
     * 手动分页查询所有产品
     * @param model
     * @param currPage 当前页
     * @param pageSize 每页显示的数据量
     * @return
     */
    @RequestMapping("/findAll")
    public ModelAndView findAll(ModelAndView model, @RequestParam(name = "currPage",defaultValue ="1" ) Integer currPage, @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize){
    PageBean<Product> pageBean=productService.findByPage(currPage,pageSize);
        //封装数据
        model.addObject("pageBean",pageBean);
        //设置视图页面
        model.setViewName("product-list");
        return model;
    }
    }

业务层:

/**
 * 产品业务层
 */
public interface ProductService {
    /**
     * 分页查询所有产品
     * @param currPage 当前页
     * @param pageSize 每页显示的数据量
     * @return
     */
    PageBean<Product> findByPage(Integer currPage, Integer pageSize);
    }
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductDao productDao;
    @Override
    public PageBean<Product> findByPage(Integer currPage, Integer pageSize) {
        PageBean<Product> pageBean = new PageBean<>();
        //给pageBean设置5个属性
        pageBean.setCurrPage(currPage);
        pageBean.setPageSize(pageSize);
        //总条目需查询数据库
        Integer totalCount=productDao.findCount();
        pageBean.setTotalCount(totalCount);
        //计算得出总页数
        pageBean.setTotalPage((int) Math.ceil(totalCount*1.0/pageSize));
        //产品集合,是分页查询的集合数据:select * from product limit 数据起始值,每页数据量
        Integer begin=(currPage-1)*pageSize;

        Map<String,Object> map=new HashMap<>();
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        //mybatis 不建议方法传入多个参数,如果有多个参数常用方法有2种
        //1.将所有参数封装到POJO类,将POJO类作为参数
        //2.使用Map集合
        List<Product> list=productDao.findByPage(map);
        pageBean.setList(list);
        return pageBean;
    }
    }

持久层:

/**
 * 产品持久层
 */
public interface ProductDao {
    //查询总数量
    Integer findCount();
    //分页查询所有产品
    List<Product> findByPage(Map<String, Object> map);
    }

product-list.jsp:

<?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.ld.dao.ProductDao">
    <select id="findCount" resultType="int">
        select count(*) from product
    </select>
    <select id="findByPage" parameterType="map" resultType="product">
        select * from product limit #{begin},#{pageSize}
    </select>
    </mapper>
<table id="dataList"
								class="table table-bordered table-striped table-hover dataTable">
								<thead>
									<tr>
										<th class="" style="padding-right: 0px;"><input
											id="selall" type="checkbox" class="icheckbox_square-blue">
										</th>
										<th class="sorting_asc">ID</th>
										<th class="sorting_desc">编号</th>
										<th class="sorting_asc sorting_asc_disabled">产品名称</th>
										<th class="sorting_desc sorting_desc_disabled">出发城市</th>
										<th class="sorting">出发时间</th>
										<th class="text-center sorting">产品价格</th>
										<th class="sorting">产品描述</th>
										<th class="text-center sorting">状态</th>
										<th class="text-center">操作</th>
									</tr>
								</thead>
								<tbody>
									<c:forEach items="${pageBean.list}" var="product">
										<tr>
											<td><input name="ids" type="checkbox" value="${product.id}"></td>
											<td>${product.id }</td>
											<td>${product.productNum }</td>
											<td>${product.productName }</td>
											<td>${product.cityName }</td>
											<td><fmt:formatDate value="${product.departureTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
											<td class="text-center">${product.productPrice }</td>
											<td>${product.productDesc }</td>
											<td class="text-center">${product.productStatus=="1"?"开启":"关闭" }</td>
											<td class="text-center">
												<button type="button" class="btn bg-olive btn-xs" onclick="deleteOne(${product.id})">删除</button>
												<button type="button" class="btn bg-olive btn-xs"
												onclick="location.href='${pageContext.request.contextPath}/product/updateUI?id=${product.id}'">修改</button>
											</td>
										</tr>
									</c:forEach>
								</tbody>
<div class="box-footer">
						<div class="pull-left">
							<div class="form-group form-inline">
								总共${pageBean.totalPage} 页,共${pageBean.totalCount} 条数据。 每页
								<%--如果切换下拉数字,默认从第1页开始显示 --%>
								<select class="form-control" id="pageSize" onclick="gotoPage(1)">
									<%--value的值就是页面中pageSize的值--%>
									<option value="3">3</option>
									<option value="5" selected="selected">5</option>
									<option value="10">10</option>
									<option value="15">15</option>
								</select></div>
						</div>

						<div class="box-tools pull-right">
							<ul class="pagination">
								<li><a href="javascript:gotoPage(1)" aria-label="Previous">首页</a></li>
								<li><a href="javascript:gotoPage(${pageBean.currPage-1})">上一页</a></li>
                                <c:forEach begin="1" end="${pageBean.totalPage}" var="i">
								<li><a href="javascript:gotoPage(${i})">${i}</a></li>
								</c:forEach>
								<li><a href="javascript:gotoPage(${pageBean.currPage+1})">下一页</a></li>
								<li><a href="javascript:gotoPage(${pageBean.totalPage})" aria-label="Next">尾页</a></li>
							</ul>
						</div>

					</div>
<script
		src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
	<script type="text/javascript">
        //当选中下拉列表中的pageSize时,给当前<option>加上属性selected=selected
        $("#pageSize option[value=${pageBean.pageSize}]").prop("selected","selected");
		//手动分页相关内容
		function gotoPage(currPage) {
		    //获取下拉列表中pageSize的值
		    var pageSize=$("#pageSize").val();
		//用判断控制页码范围
			if(currPage<1){
				return;//终止代码执行
			}
			if (currPage>${pageBean.totalPage}){
				return;//终止代码执行
			}
			location.href="${pageContext.request.contextPath}/product/findAll?currPage="+currPage+"&pageSize="+pageSize;
		}

Mybatis分页助手_pageHelper 请参考:

https://blog.csdn.net/IT_LD/article/details/107917574

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值