SpringMVC + Mybatis + maven 后台分页设计

一.前端jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ include file="/common/taglibs.jsp"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>房源管理</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<link href="${path}/public/css/commonpc.css" rel="stylesheet" type="text/css" />

<link href="${path}/public/css/admin.css" rel="stylesheet" type="text/css" />

<link href="${path}/public/css/bootstrap/bootstrap.css" type="text/css" rel="stylesheet" />

<link rel="stylesheet" href="${path}/public/css/compiled/user-list.css" type="text/css" media="screen" />

<script type="text/javascript" src="${path}/public/js/jquery-1.4.4.min.js"></script>

<script>

function searchHS(){

var housenum=$("#housenum").val();

var housename=$("#housename").val();

var para = "housenum=" + housenum ;

para += "&housename=" + housename ;

para = para.replace(/[\r\n]/g,"").replace(/undefined/g,"").replace(/[]/g,"");

window.location.href=encodeURI("${path}/house/list?" + para);

}

</script>

<style>

label {

display: block;

margin-bottom: 5px;

display: inline-table;

}

.input-btn {

width: 130px;

}

.mr5 {

margin-right: 5px;

}

.ss-btn {

border: 1px solid #1ba794;

background-color: #1ba794;

margin-top: 0px;

height: 24px;

color: #fff;

font-size: 14px;

}

input[type="text"]{

height: 23px;

margin-bottom: 0px;

}

</style>

</head>

<body style="padding-left:350px;">

<div class="rygl-tit">

<span class="rygl fl">房源列表</span>

<div class="fr pdr80 pdt20">

<button class="tjdr-btn mr30" onclick="window.location.href=‘${path}/house/add‘">添加</button>

</div>

</div>

<p style="clear:both;"></p>

<div style="padding-top: 20px;padding-left: 75px;">

<div class="fl">

<input type="hidden" id="pageNo" name="pageNo" />

<label>房源编号:</label>

<input class="input-btn mr5" type="text" id="housenum" name="housenum" value="${housenum}"/>

<label>房源名称:</label>

<input class="input-btn mr5" type="text" id="housename" name="housename" value="${housename}"/>

</div>

<input class="ss-btn" value="搜索" type="button" onclick="searchHS()"/>

</div>

<%-- 加载分页时请求路径 及 请求方式--%>

<form id="queryForm" name="queryForm"  action="${path}/house/list" method="post">

<input type="hidden" id="pageNo" name="pageNo" />

</form>

<div class="table-style">

<table  cellspacing="0">

<thead>

<tr>

<th>房源编号</th>

<th>房源名称</th>

<th>创建人</th>

<th>操作</th>

<th style="display:none">${userid}</th>

</tr>

</thead>

<c:forEach items="${houselist}" var="list">

<tbody>

<tr class="bg-con">

<td class="ddbh">${list.houseNum }</td>

<td class="ddyzf">${list.houseName}</td>

<td class="ddyzf" >${list.createPeople}</td>

<td class="cz">

<a class="ckxq" href="${path}/house/look/?id=${list.houseresid}">查看</a>

<a class="ckxq"  href="${path}/house/edit?id=${list.houseresid}">修改</a>

<a class="ckxq" href="${path}/house/del?id=${list.houseresid}" confirm(‘确定删除吗?‘)" >删除</a>

</td>

</tr>

</tbody>

</c:forEach>

</table>

</div>

${houseRec.pageNoDisp }

</body>

</html>

二.后台代码

    

    

/////////////////////////////////1.业务控制层(controller层)////////////////////////////

package com.sykj.haoguanjia.controller.system;

import java.util.List;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import com.sykj.haoguanjia.model.HouseRes;

import com.sykj.haoguanjia.service.HouseResService;

/**

* @author hykang

* @date 2017-05-09

*

*/

@Controller

@RequestMapping("/house")

public class HouseController {

@Autowired

private HouseResService houseResService;

@RequestMapping("/list")

public String houseList(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,String housenum,String housename,Model model){

List<HouseRes> pagelist = null;

try {

HouseRes pageRec = new HouseRes();

pageRec.setPageNo(pageNo);

pageRec.setHouseNum(housenum);

if(!"".equals(housename) && housename != null){

housename=new String(housename.getBytes("ISO-8859-1"),"UTF-8");

//housename = java.net.URLDecoder.decode(housename, "utf-8");

}

pageRec.setHouseName(housename);

pageRec.setDeleteFlag(0);

pagelist = houseResService.findPageData(pageRec);

model.addAttribute("houselist", pagelist);

model.addAttribute("houseRec", pageRec);

model.addAttribute("housenum", housenum);

model.addAttribute("housename", housename);

} catch (Exception e) {

e.printStackTrace();

}

return "house/list";

}

}

////////////////////////////////2.service层//////////////////////////////////////////

package com.sykj.haoguanjia.service;

import java.util.List;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Service;

import com.sykj.haoguanjia.dao.HouseResMapper;

import com.sykj.haoguanjia.model.HouseRes;

/**

* @author hekang

*

*/

@Service

public class HouseResService {

@Autowired

private HouseResMapper houseResMapper;

public List<HouseRes> findPageData(HouseRes record) throws DataAccessException{

return houseResMapper.findPageData(record);

}

}

////////////////////////////////////////3.DAO层///////////////////////////////////////

package com.sykj.haoguanjia.dao;

import java.util.List;

import com.sykj.haoguanjia.model.HouseRes;

public interface HouseResMapper {

List<HouseRes> findPageData(HouseRes record);

}

////////////////////////////////////////4.Mapper.xml/////////////////////////////////////

<resultMap id="BaseResultMap" type="com.sykj.haoguanjia.model.HouseRes">

<id column="houseresid" jdbcType="BIGINT" property="houseresid" />

<result column="house_num" jdbcType="VARCHAR" property="houseNum" />

<result column="house_name" jdbcType="VARCHAR" property="houseName" />

<result column="create_people" jdbcType="BIGINT" property="createPeople" />

</resultMap>

<select id="findPageData" parameterType="com.sykj.haoguanjia.model.HouseRes" resultMap="BaseResultMap">

select

<include refid="Base_Column_List"/>

from tb_house_res

<include refid="selectWhereClause"/>

<if test="orderByClause != null">

order by ${orderByClause}

</if>

</select>

////////////////////////////////////5.房源实体类////////////////////////////////////////////////////

package com.sykj.haoguanjia.model;

import java.math.BigDecimal;

import java.util.Date;

import com.sykj.haoguanjia.page.PageHelper;

public class HouseRes extends PageHelper{

private Long houseresid;

private String houseNum;

private String houseName;

private Long createPeople;

public Long getHouseresid() {

return houseresid;

}

public void setHouseresid(Long houseresid) {

this.houseresid = houseresid;

}

public String getHouseNum() {

return houseNum;

}

public void setHouseNum(String houseNum) {

this.houseNum = houseNum == null ? null : houseNum.trim();

}

public String getHouseName() {

return houseName;

}

public void setHouseName(String houseName) {

this.houseName = houseName == null ? null : houseName.trim();

}

public Long getCreatePeople() {

return createPeople;

}

public void setCreatePeople(Long createPeople) {

this.createPeople = createPeople;

}

}

//////////////////////////////////6.分页类///////////////////////////////////////////

package com.sykj.haoguanjia.page;

import java.util.List;

import java.util.Map;

import org.apache.log4j.Logger;

import org.codehaus.jackson.map.ObjectMapper;

import com.google.common.collect.Lists;

import com.google.common.collect.Maps;

/**

* 封装分页数据

*/

public class PageHelper {

private static final Logger logger = Logger.getLogger(PageHelper.class);

private static ObjectMapper mapper = new ObjectMapper();

public static String DEFAULT_PAGESIZE = "10";

private int pageNo; // 当前页码

private int pageSize; // 每页行数

private int totalRecord; // 总记录数

private int totalPage; // 总页数

private Map<String, String> params; // 查询条件

private Map<String, List<String>> paramLists; // 数组查询条件

private String searchUrl; // Url地址

private String pageNoDisp; // 可以显示的页号(分隔符"|",总页数变更时更新)

private String orderByClause;

public PageHelper() {

pageNo = 1;

pageSize = Integer.valueOf(DEFAULT_PAGESIZE);

totalRecord = 0;

totalPage = 0;

params = Maps.newHashMap();

paramLists = Maps.newHashMap();

searchUrl = "";

pageNoDisp = "";

}

public static PageHelper newBuilder(int pageNo, int pageSize, String url) {

PageHelper page = new PageHelper();

page.setPageNo(pageNo);

page.setPageSize(pageSize);

page.setSearchUrl(url);

return page;

}

/**

* 查询条件转JSON

*/

public String getParaJson() {

Map<String, Object> map = Maps.newHashMap();

for (String key : params.keySet()) {

if (params.get(key) != null) {

map.put(key, params.get(key));

}

}

String json = "";

try {

json = mapper.writeValueAsString(map);

} catch (Exception e) {

logger.error("转换JSON失败", e);

}

return json;

}

/**

* 数组查询条件转JSON

*/

public String getParaListJson() {

Map<String, Object> map = Maps.newHashMap();

for (String key : paramLists.keySet()) {

List<String> lists = paramLists.get(key);

if (lists != null && lists.size() > 0) {

map.put(key, lists);

}

}

String json = "";

try {

json = mapper.writeValueAsString(map);

} catch (Exception e) {

logger.error("转换JSON失败", e);

}

return json;

}

/**

* 总件数变化时,更新总页数并计算显示样式

*/

private void refreshPage() {

// 总页数计算

totalPage = totalRecord % pageSize == 0 ? totalRecord / pageSize : (totalRecord / pageSize + 1);

// 防止超出最末页(浏览途中数据被删除的情况)

if (pageNo > totalPage && totalPage != 0) {

pageNo = totalPage;

}

pageNoDisp = computeDisplayStyleAndPage();

}

/**

* 计算页号显示样式 这里实现以下的分页样式("[]"代表当前页号),可根据项目需求调整 [1],2,3,4,5,6,7,8..12,13

* 1,2..5,6,[7],8,9..12,13 1,2..6,7,8,9,10,11,12,[13]

*/

private String computeDisplayStyleAndPage() {

StringBuffer buffer = new StringBuffer();

List<Integer> pageDisplays = Lists.newArrayList();

if (totalPage <= 11) {

for (int i = 1; i <= totalPage; i++) {

pageDisplays.add(i);

}

} else if (pageNo < 7) {

for (int i = 1; i <= 8; i++) {

pageDisplays.add(i);

}

pageDisplays.add(0);// 0 表示 省略部分(下同)

pageDisplays.add(totalPage - 1);

pageDisplays.add(totalPage);

} else if (pageNo > totalPage - 6) {

pageDisplays.add(1);

pageDisplays.add(2);

pageDisplays.add(0);

for (int i = totalPage - 7; i <= totalPage; i++) {

pageDisplays.add(i);

}

} else {

pageDisplays.add(1);

pageDisplays.add(2);

pageDisplays.add(0);

for (int i = pageNo - 2; i <= pageNo + 2; i++) {

pageDisplays.add(i);

}

pageDisplays.add(0);

pageDisplays.add(totalPage - 1);

pageDisplays.add(totalPage);

}

buffer.append("<div style=‘text-align:right;padding-right:20px;‘>");

if (pageNo==1) {

buffer.append("首页");

buffer.append("上一页");

}else{

buffer.append("<a class=‘tcdNumber‘ href=‘javascript:_doQuery(1);‘>首页</a>");

buffer.append("<a class=‘tcdNumber‘ href=‘javascript:_doQuery("+(pageNo-1)+");‘>上一页</a>");

}

for(Integer i:pageDisplays){

if(i==0){

buffer.append("<a class=‘tcdNumber‘ >"+"..."+"</a>");

}else if( i == pageNo ) {

buffer.append("<a class=‘curNumber‘ href=‘javascript:_doQuery("+i+");‘>"+i+"</a>");

} else {

buffer.append("<a class=‘tcdNumber‘ href=‘javascript:_doQuery("+i+");‘>"+i+"</a>");

}

}

if (pageNo==totalPage) {

buffer.append("下一页");

buffer.append("尾页");

}else{

buffer.append("<a class=‘tcdNumber‘ href=‘javascript:_doQuery("+(pageNo+1)+");‘>下一页</a>");

buffer.append("<a class=‘tcdNumber‘ href=‘javascript:_doQuery("+totalPage+");‘>尾页</a>");

}

buffer.append("</div>");

buffer.append("<script type=‘text/javascript‘>");

buffer.append("function _doQuery(pageNo){document.queryForm.pageNo.value=pageNo;document.queryForm.submit();}");

buffer.append("</script>");

return buffer.toString();

}

public String getHTML(){

return null;

}

public int getPageNo() {

return pageNo;

}

public void setPageNo(int pageNo) {

this.pageNo = pageNo;

}

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

this.pageSize = pageSize;

}

public int getTotalRecord() {

return totalRecord;

}

public void setTotalRecord(int totalRecord) {

this.totalRecord = totalRecord;

refreshPage();

}

public int getTotalPage() {

return totalPage;

}

public void setTotalPage(int totalPage) {

this.totalPage = totalPage;

}

public Map<String, String> getParams() {

return params;

}

public void setParams(Map<String, String> params) {

this.params = params;

}

public Map<String, List<String>> getParamLists() {

return paramLists;

}

public void setParamLists(Map<String, List<String>> paramLists) {

this.paramLists = paramLists;

}

public String getSearchUrl() {

return searchUrl;

}

public void setSearchUrl(String searchUrl) {

this.searchUrl = searchUrl;

}

public String getPageNoDisp() {

return pageNoDisp;

}

public void setPageNoDisp(String pageNoDisp) {

this.pageNoDisp = pageNoDisp;

}

public String getOrderByClause() {

return orderByClause;

}

public void setOrderByClause(String orderByClause) {

this.orderByClause = orderByClause;

}

}


时间: 2024-10-10 05:21:16

SpringMVC + Mybatis + maven 后台分页设计的相关文章

Spring+SpringMVC+MyBatis+Maven框架整合

本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点 一.Maven需要引入的jar包 二.Spring与SpringMVC的配置分离 三.Spring与MyBatis的整合 一.Maven需要引入的jar包 本文默认读者已经掌握Maven的使用,Maven配置片段如下 Xml代码   <!-- 引入spring-webmvc与spring-jdbc --> <dependency> <groupId>org.sprin

idea 下搭建springMvc + mybatis+Maven

刚开始使用idea,发现idea与myEclipse差别还是很大的,从搭建的简单的SpringMvc开始,一路遇到种种困难.本文讲诉idea搭建springMvc + mybatis+Maven,环境:希望给新手带来帮助. 1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型

SpringMvc+Mybatis(maven工程)整理出来了,纯自己手工整理!!!!!!!!!!!!有图有真相!!!!!!!!!!!!!!!!求高分!!!!

原文:SpringMvc+Mybatis(maven工程)整理出来了,纯自己手工整理!!!!!!!!!!!!有图有真相!!!!!!!!!!!!!!!!求高分!!!! 源代码下载地址:http://www.zuidaima.com/share/1550463690738688.htm 部署直接可用. 开发工具:Eclipse jdk:1.6 部署工具:tomcat7 db driver:mysql,其中可以自己随意改了,这里不多说了.... 数据库就不给了,没什么用处,主要分享框架.  

idea+spring4+springmvc+mybatis+maven实现简单增删改查CRUD

在学习spring4+springmvc+mybatis的ssm框架,idea整合简单实现增删改查功能,在这里记录一下. 原文在这里:https://my.oschina.net/finchxu/blog/3007984 工作环境: Windows 10 jdk8(1.8) IntelliJ IDEA spring 4 和 springMVC MySQL 5.7 maven 3.3 mybatis 3.4 DBCP Tomcat 8.5 项目上传到了Github方便查看:https://gith

spring+springMVC+mybatis+maven+mysql环境搭建(二)

上一篇整合了spring+mybatis,基本上还不是web工程,接下来接入springMVC,Let's go! 一.工程转换成Web工程 首先右击项目-->properties-->project facets,观察是否出现下图配置 没出现也不要慌张,先把Dynamic Web Module√去掉,然后点击ok,再次右击项目,进入priperties-->project facets,勾上Dynamic Web Module,该配置就出现了,点击箭头所指,进入下图界面: 这时发现新增

Spring+SpringMVC+MyBatis+Maven 服务端XML配置

项目目录结构 spring-mybatis.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.spr

SSM三大框架整合详细教程Spring+SpringMVC+MyBatis+maven

[源码获取地址] 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目重新搭建了一次,而且比项目搭建的要更好一些.以前解决问题的过程和方法并没有及时记录,以后在自己的小项目中遇到我再整理分享一下.这次,先说说三大框架整合过程.个人认为使用框架并不是很难,关键要理解其思想,这对于我们提高编程水平很有帮助.不过,如果

springmvc mybatis maven 整合 事例

最近做了个maven管理的springmvc+spring+mybatis,还用到了阿里巴巴的 fastjson和druid连接池,配置文件如下 pom.xml文件 [html] view plain copy  <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="htt

SpringMVC+mybatis+maven+Ehcache缓存实现

所谓缓存,就是将程序或系统经常要调用的对象存在内存中,以便其使用时可以快速调用,不必再去创建新的重复的实例.这样做可以减少系统开销,提高系统效率. 缓存主要可分为二大类: 一.通过文件缓存,顾名思义文件缓存是指把数据存储在磁盘上,不管你是以XML格式,序列化文件DAT格式还是其它文件格式: 二.内存缓存,也就是实现一个类中静态Map,对这个Map进行常规的增删查. 一.EhCache缓存系统简介 EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点,是 Hibernate