尚筹网:分页显示

备注:一定要小心

流程

在这里插入图片描述

执行查询的sql语句

SELECT
	*
FROM
	t_admin
WHERE
	login_acct LIKE CONCAT("%", "", "%")
OR user_name LIKE CONCAT("%", "", "%")
OR email LIKE CONCAT("%", "", "%");

CONCAT()函数的作用是拼字符串,执行字符串连接。

MyBatis的PageHelper插件

作用

以完全非侵入的方式在原有查询基础上附加分页效果。从SQL层面来说,在SQL语句后面附加LIMIT子句。从Java代码来说,把原来返回的List类型封装为Page类型。

依赖

<!-- 父工程 -->
<!-- MyBatis分页插件 -->
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>4.0.0</version>
</dependency>

配置

所在工程:atcrowdfunding-admin-1-webui
配置文件:spring-persist-mybatis.xml
在SqlSessionFactoryBean中配置MyBatis插件

<!-- 配置插件 -->
		<property name="plugins">
			<array>
				<!-- PageHelper插件全类名 -->
				<bean class="com.github.pagehelper.PageHelper">
					<!-- 配置插件属性 -->
					<property name="properties">
						<props>
							<!-- 配置数据库方言 -->
							<!-- MySQL分页:LIMIT -->
							<!-- Oracle分页:TopN分析 -->
							<prop key="dialect">mysql</prop>
							
							<!-- 配置自动修正页码 -->
							<!-- pageNo的有效范围:1~总页数 -->
							<prop key="reasonable">true</prop>
						</props>
					</property>
				</bean>
			</array>
		</property>

AdminMapper

Mapper配置文件

所在工程:atcrowdfunding-admin-1-webui
文件:resources/mybatis/mapper/AdminMapper.xml

<select id="selectAdminListByKeyword" resultMap="BaseResultMap">
	select
	<include refid="Base_Column_List" />
	from t_admin
	WHERE
		login_acct LIKE CONCAT("%", #{keyword}, "%")
	OR user_name LIKE CONCAT("%", #{keyword}, "%")
	OR email LIKE CONCAT("%", #{keyword}, "%")
</select>

Mapper接口

所在工程:atcrowdfunding-admin-2-component
全类名:com.atguigu.crowd.funding.mapper.AdminMapper

List<Admin> selectAdminListByKeyword(String keyword);

AdminService

所在工程:atcrowdfunding-admin-2-component
接口方法:

PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword);

实现类方法:

@Override
public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
	
	// 1.调用PageHelper的工具方法,开启分页功能
	PageHelper.startPage(pageNum, pageSize);
	
	// 2.执行分页查询
	List<Admin> list = adminMapper.selectAdminListByKeyword(keyword);
	
	// 3.将list封装到PageInfo对象中
	return new PageInfo<>(list);
}

AdminHandler

所在工程:atcrowdfunding-admin-2-component
全类名:com.atguigu.crowd.funding.handler.AdminHandler

	@RequestMapping("/admin/query/for/search")
	public String queryForSearch(
			
			// 如果页面上没有提供对应的请求参数,那么可以使用defaultValue指定默认值
			@RequestParam(value="pageNum", defaultValue="1") Integer pageNum, 
			@RequestParam(value="pageSize", defaultValue="5") Integer pageSize, 
			@RequestParam(value="keyword", defaultValue="") String keyword,
			Model model) {
		
		PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
		
		model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
		
		return "admin-page";
	}
public static final String ATTR_NAME_PAGE_INFO = "PAGE-INFO";

页面显示

1、增加连接:
修改/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/include-sidebar.jsp的
在这里插入图片描述
/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/admin-page.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="UTF-8">
<%@ include file="/WEB-INF/include-head.jsp" %>
<body>

	<%@ include file="/WEB-INF/include-nav.jsp" %>
	<div class="container-fluid">
		<div class="row">
			<%@ include file="/WEB-INF/include-sidebar.jsp" %>
			<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
				<c:forEach items="${requestScope['PAGE-INFO'].list}" var="admin">
					${admin}<br>
				</c:forEach>
			</div>
		</div>
	</div>

</body>
</html>

3、插入测试数据
/atcrowdfunding-admin-1-webui/src/test/java/com/atguigu/crowd/funding/test/CrowdFundingTest.java

	@Autowired
	private AdminMapper adminMapper;
	@Test
	public void batchSaveAdmin() {
		for(int i = 0; i < 500; i++) {
			adminMapper.insert(new Admin(null, "loginAcct"+i, "1111111", "userName"+i, "email"+i+"@qq.com", null));
		}
	}

2、运行访问:

  • http://localhost:8080/atcrowdfunding-admin-1-webui/admin/query/for/search.html
  • http://localhost:8080/atcrowdfunding-admin-1-webui/admin/query/for/search.html?pageNum=2&pageSize=10

编写页面

环境搭建

  • 将尚筹网/前端/pagination_zh/lib/pagination.css复制到/atcrowdfunding-admin-1-webui/src/main/webapp/css目录下
  • 将尚筹网/前端/pagination_zh/lib/jquery.pagination.js复制到/atcrowdfunding-admin-1-webui/src/main/webapp/script目录下

页面编写

/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/admin-page.jsp内容:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="UTF-8">
<%@ include file="/WEB-INF/include-head.jsp"%>
<link rel="stylesheet" href="css/pagination.css" />
<script type="text/javascript" src="script/jquery.pagination.js"></script>
<script type="text/javascript">
$(function(){
	initPagination();
});

// 声明函数封装导航条初始化操作
function initPagination() {
	
	// 声明变量存储总记录数
	var totalRecord = ${requestScope['PAGE-INFO'].total};
	
	// 声明变量存储分页导航条显示时的属性设置
	var paginationProperties = {
		num_edge_entries :3,			//边缘页数
		num_display_entries : 5,		//主体页数
		callback : pageselectCallback,	//回调函数
		items_per_page : ${requestScope['PAGE-INFO'].pageSize},	//每页显示数据数量,就是pageSize
		current_page : ${requestScope['PAGE-INFO'].pageNum - 1},//当前页页码
		prev_text : "上一页",			//上一页文本
		next_text : "下一页"			//下一页文本
	};
	
	// 显示分页导航条
	$("#Pagination").pagination(totalRecord, paginationProperties);
}

// 在每一次点击“上一页”、“下一页”、“页码”时执行这个函数跳转页面
function pageselectCallback(pageIndex, jq) {
	
	// pageIndex从0开始,pageNum从1开始
	var pageNum = pageIndex + 1;
	
	// 跳转页面
	window.location.href = "?pageNum="+pageNum+"&keyword=${param.keyword}";
	
	return false;
}
</script>
<body>

	<%@ include file="/WEB-INF/include-nav.jsp"%>
	<div class="container-fluid">
		<div class="row">
			<%@ include file="/WEB-INF/include-sidebar.jsp"%>
			<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
				<div class="panel panel-default">
					<div class="panel-heading">
						<h3 class="panel-title">
							<i class="glyphicon glyphicon-th"></i> 数据列表
						</h3>
					</div>
					<div class="panel-body">
						<form action="admin/query/for/search.html" class="form-inline"
							role="form" style="float: left;" method="post">
							<div class="form-group has-feedback">
								<div class="input-group">
									<div class="input-group-addon">查询条件</div>
									<input name="keyword" class="form-control has-success"
										type="text" placeholder="请输入查询条件">
								</div>
							</div>
							<button type="submit" class="btn btn-warning">
								<i class="glyphicon glyphicon-search"></i> 查询
							</button>
						</form>
						<button type="button" class="btn btn-danger"
							style="float: right; margin-left: 10px;">
							<i class=" glyphicon glyphicon-remove"></i> 删除
						</button>
						<button type="button" class="btn btn-primary"
							style="float: right;" onclick="window.location.href='add.html'">
							<i class="glyphicon glyphicon-plus"></i> 新增
						</button>
						<br>
						<hr style="clear: both;">
						<div class="table-responsive">
							<table class="table  table-bordered">
								<thead>
									<tr>
										<th width="30">#</th>
										<th width="30"><input id="summaryBox" type="checkbox"></th>
										<th>账号</th>
										<th>名称</th>
										<th>邮箱地址</th>
										<th width="100">操作</th>
									</tr>
								</thead>
								<tbody>
									<c:if test="${empty requestScope['PAGE-INFO'].list }">
										<tr>
											<td style="text-align: center;" colspan="6">抱歉!没有符合您要求的查询结果!</td>
										</tr>
									</c:if>
									<c:if test="${!empty requestScope['PAGE-INFO'].list }">
										<c:forEach items="${requestScope['PAGE-INFO'].list }"
											var="admin" varStatus="myStatus">
											<tr>
												<td>${myStatus.count }</td>
												<td><input class="itemBox" type="checkbox"></td>
												<td>${admin.loginAcct }</td>
												<td>${admin.userName }</td>
												<td>${admin.email }</td>
												<td>
													<button type="button" class="btn btn-success btn-xs">
														<i class=" glyphicon glyphicon-check"></i>
													</button>
													<button type="button" class="btn btn-primary btn-xs">
														<i class=" glyphicon glyphicon-pencil"></i>
													</button>
													<button type="button" class="btn btn-danger btn-xs">
														<i class=" glyphicon glyphicon-remove"></i>
													</button>
												</td>
											</tr>
										</c:forEach>
									</c:if>
								</tbody>
								<tfoot>
									<tr>
										<td colspan="6" align="center">
											<div id="Pagination" class="pagination">
												<!-- 这里显示分页 -->
											</div>
										</td>
									</tr>

								</tfoot>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
</html>

备注:分页功能如果没有显示,看是否是因为jquery没有在head里面

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录: 1.无法添加依赖 2.执行逆向生成操作的 Maven 命令 命令:mybatis-generator:generate。 报错执行不了 3. The import org.springframework.test cannot be resolved 无法导包 4. 运行Junit测试报错java.io.FileNotFoundException: class path resource [mybatis-config.xml] cannot be opened because it:java.io.FileNotFoundException: class path resource [mybatis-config.xml] cannot be opened because it 5.B站看视频如果声音嘈杂,不清楚,小。右键,点击清澈人声。有时候弹幕真牛逼。 6. 看视频第30集,日志系统操作,测试日志时候添加类报错:LoggerFactory cannot be resolved 7,查错需要看控制台的Caused by:后边的内容。 8,测试testTx()报错3,是因为,缺少1这句话。 9. Eclipse如何还原和重置窗口布局复位是reset 10.idea缺少maven导航条 11.idea运行项目,报错404,基本都是Tomcat配置那里两个路劲配置不一致导致的。 12.问题未解决!!!45集测试ssm环境整合,跑不起来 报错: 13 将项目转移到idea中运行,并且进行一些配置修改,将以上12问题解决! 14.更改配置文件,或者有其他更改,最好要在build那里rebuild一下,要不然可能会出一些莫名其妙的错误,这是在用renrenfast框架时候,增加学生,教师等模块,界面一致装,不能正常显示,梦圆教我的。Rebuild后,再重启,即可解决问题。 15.idea加载一个新项目,第一需要调出右侧maven导航条,更新下maven,第二需要在artifact那边的lib里增加maven依赖。这样才能保证依赖和包齐全,项目才能正常运行。这是狂神教的 16.eclipse里边的project相当于idea里边的module。 17. idea 光标变粗如何恢复 18.出现问题:视频63集,无法做出和他一样的页面,HTML变红报错,网页没有按钮 19. 69集管理员登录测试,报400错误 20 问题19的启发,看视频遇到问题的话,那就载看一遍,很有可能是哪里敲错了,导致了问题的出现,好好再检查一遍流程,很多问题基本能够解决。 21 IDEa中Ctrl+f 是在当前这个页面搜索任何想找的代码 22. 70集管理员登录,重定向,报错404 23.最开始这边的弹框弹不出来 24.视频82集,页面显示主体数据,运行项目后,等录出错,出现空白页,控制台也有报错 25.点击用户维护,不想老师那样,出现tom相关信息,button按钮也有问题 26.第87集分页导航条查询有问题 27,95集新增功能,点击页面的新增报404错误 28.视频132,单条删除,无法删除,查看前端代码有报错 29.报500错误,说utf-8啥的, 30.给管理员添角色,左侧未添加列表为空,然而,视频中的却不为空 31. 177集admin分配role,点击向右添加,保存后报错: 32.jsp文件明明定义了某个函数,但是页面显示不出来,查看console,报错说没有这个函数,这是因为浏览器有缓存的原因,按ctrl+f5强制刷新,然以后在重启服务器即可解决问题。 33.感觉debug自己还不会,这是一项必备的技能,后边有必要在B站上搜索下debug教程看看。 34.在pom中加入依赖,在web.xml中增加配置后,需要在右侧maven导航条刷新,在上方artifact那边添加library,类里边如果导包不成功,那么重启idea就好了 35. p247运行项目直接报错: 36.老师讲要会看异常,后台框架里的异常,一般是后一个异常导致了前一个异常,所以看异常要最先看最后一个causeby:往往最后这个能解决99%问题。还有1%是xml文件写错了。 37. 252集测试2,我的roleOperator无法显示内容 38. 285集yml文件读取,测试test遇到找不到类异常 以下为解决办法: 39 288集spring整合mybaitis报错 40.项目看到290集,需要用到Redis,所以暂停项目学习,转入Redis学习。 41.Idea如何同时运行多个项目:点击edit configuration,然后如下图,报错即可,只要端口号不重复,就可以运行多个项目。 42. 重要错误parent的版本号对不上报错:Cann
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值