DBUtils框架

主要知识点:
一、多表关联操作(DBUtils框架)
1.一对多关系 ***
2.多对多关系 ***
3.一对一关系
二、分页
三、监听器组件(web三大组件之一)
四、踢人综合示例(监听器实现)

 

一.分页核心类总结:
1.分页必要性
select * from account;

分页的结果就是要让指定的记录加载到内存
1.人的习惯
2.内存的限度(核心问题)
3.屏幕的限度

2.如何做分页?
1.数据库层面的考虑
select * from account limit startIndex,size;//只是针对MySql而言
startIndex:起始索引(从0开始),代表第一行
size:记录数

select * from account limit 0,3; 第一页(1,2,3)
select * from account limit 3,3; 第二页(4,5,6)
startIndex:(pageNo:第几页) (pageNo-1)*size

2.程序代码上思考
1. 直接发送这样的sql语句
select * from account limit startIndex,size;
ResultSet中存的数据就是指定的记录

2. 发送一个查询所有的sql语句(一般不考虑,因为没有解决内存本质问题)
select * from account;
ResultSet 结果集中
在内存中处理结果(只加载前10条)

3.代码怎么写?

 

 


public class PageBean {
private int pageNo=1;
private int pageSize=3;
private int prep;
private int nextp;
private int startIndex;
private int totalRecordes;
private int totalPage;

private int startPageNo;
private int endPageNo;

private String url ;
private List recordes;


public int getStartPageNo() {
if(getTotalPage()<9){
startPageNo = 1;
endPageNo =getTotalPage();
}else{
startPageNo = pageNo-4;
endPageNo = pageNo+4;
if(startPageNo<1){
startPageNo = 1;
endPageNo = startPageNo+8;
}
if(endPageNo>getTotalPage()){
endPageNo=getTotalPage();
startPageNo = endPageNo-8;
}
}
return startPageNo;
}

public int getPrep() {
if(pageNo<=1){
prep=1;
}else{
prep=pageNo-1;
}
return prep;
}

public int getNextp() {
if(pageNo>=getTotalPage()){
nextp=totalPage;
}else{
nextp=pageNo+1;
}
return nextp;
}

public int getStartIndex() {
startIndex = (pageNo-1)*pageSize;
return startIndex;
}

public int getTotalPage() {
if(totalRecordes%pageSize==0){
totalPage = totalRecordes/pageSize;
}else{
totalPage = totalRecordes/pageSize+1;
}
return totalPage;
}

分离出去的页面:
<div style="text-align:center;">
第${pb.pageNo }/共${pb.totalPage }页 <a href="${pageContext.request.contextPath }${pb.url}?pageNo=${pb.prep}">上一页</a> &nbsp;&nbsp;&nbsp;
<c:forEach begin="${pb.startPageNo }" end="${pb.endPageNo }" var="vv">
<a href="${pageContext.request.contextPath}${pb.url}?pageNo=${vv}">${vv }</a> &nbsp;&nbsp;
</c:forEach>

<a href="${pageContext.request.contextPath }${pb.url}?pageNo=${pb.nextp}">下一页</a>
<select onchange="jump(this)">
<c:forEach begin="1" end="${pb.totalPage }" var="v">
<option value="${v }" ${pb.pageNo==v?'selected':'' }>${v }</option>
</c:forEach>
</select>
<script>
function jump(cobj){
window.location.href="${pageContext.request.contextPath}${pb.url}?pageNo="+cobj.value;
}
</script>
</div>

 


五、监听器概述(观察者模式)
事件:踢
事件源:人
事件处理程序: 女主人出来了,我跑了
监听器:相当于报警器


写一个自定义的监听程序,用于监听学生的学习状态
六、Servlet规范中的8个监听器
1、监听ServletContext、HttpSession、ServletRequest对象的创建和销毁的监听器
ServletContextListener:
HttpSessionListener:
ServletRequestListener:


//写一个Listener基本步骤:
// 1.写类 (实现相应Listener接口)
// 2.配置web.xml <listener><listener-class>包名.类名</listener-class></listener>

 

2、监听ServletContext、HttpSession、ServletRequest域中数据变化的监听器
ServletContextAttributeListener:
HttpSessionAttributeListener:user
ServletRequestAttributeListener:

3、感知型监听器。这些监听器不需要注册.给普通类用的。
HttpSessionBindingListener:普通类实现了该接口,感知自己何时被HttpSession绑和解绑
HttpSessionActivationListener:普通类实现了该接口,感知自己何时随着HttpSession钝化和激活。

七、显示在线用户,并能踢人
login.jsp(提供登录界面)--------------->LoginServlet----------------->HttpSession(存入登录的用户信息)

显示用户列表(所有登录在线的用户都要显示出来)------------>ServletContext域(何时加入到这个域中???)


------------>登录的时候就要加入到ServletContext---------->为了在登录时就可以向servletContext域中加入数据
------------>加入一个监听器(加什么样监听器?)-------->HttpSessionAttributeListener

posted @ 2017-07-07 22:04  怀念-2018  阅读(167)  评论(0编辑  收藏  举报