发表于: 2018-03-20 21:52:46
1 466
今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)
昨天上午被空指针困扰,在师兄的帮助下找到原因是Aoutoweird标签没起作用
然后发现我对service标签的扫描写在了spring-mvc文件里,换到spring文件里就好了=。=
当时大意了写错地方。
完成了分页的功能:
1.创建实体类PageBean
存储当前页数、每页显示记录数、总记录数、总页数和每页显示的数据。
2.创建业务模型实体类映射文件
<!-- 根据分页数据start 和size查询数据 -->
<select id="findByPage" parameterType="Map" resultMap="StudentResult">
select
id, name, gender, age, qq, occupation, join_date, school, number,
daily_url, declaration, consoler
from students
<if test="start!=null and size!=null">
limit #{start},#{size}
</if>
</select>
<!--查询所有用户数据 -->
<select id="selectStudentList" resultMap="StudentResult">
select id, name, gender, age, qq, occupation, join_date, school, number,
daily_url, declaration, consoler from students
</select>
<!-- 查询用户记录总数 -->
<select id="selectCount" resultType="int">
select count(*) from students
</select>
DAO接口
List<Student> findByPage(HashMap<String,Object> map);
List<Student> selectStudentList();
int selectCount();
3.service
接口
//查询所有用户数据
List<Student> selectStudentList() throws Exception;
//查询用户记录总数
int selectCount() throws Exception;
//根据分页数据start 和size查询数据
PageBean<Student> findByPage(int currentPage) throws Exception;
实现
//查询所有用户数据
@Override
public List<Student> selectStudentList() throws Exception{
return studentMapper.selectStudentList();
}
//查询用户记录总数
@Override
public int selectCount() throws Exception{
return studentMapper.selectCount();
}
//根据分页数据start 和size查询数据
@Override
public PageBean<Student> findByPage(int currentPage) {
HashMap<String,Object> map = new HashMap<String,Object>();
PageBean<Student> pageBean = new PageBean<Student>();
//封装当前页数
pageBean.setCurrPage(currentPage);
//每页显示的数据
int pageSize=10;
pageBean.setPageSize(pageSize);
//封装总记录数
int totalCount = studentMapper.selectCount();
pageBean.setTotalCount(totalCount);
//封装总页数
double tc = totalCount;
Double num =Math.ceil(tc/pageSize);//向上取整
pageBean.setTotalPage(num.intValue());
map.put("start",(currentPage-1)*pageSize);
map.put("size", pageBean.getPageSize());
//封装每页显示的数据
List<Student> lists = studentMapper.findByPage(map);
pageBean.setLists(lists);
return pageBean;
}
4.controller层
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage,Model model) throws Exception{
model.addAttribute("student", studentService.findByPage(currentPage));//回显分页数据
return "list";
5.JSP文件的body部分
<body>
<%--<form action="${pageContext.request.contextPath }/student/list" method="post">--%>
<c:if test="${empty requestScope.student}">
没有任何用户信息!
</c:if>
<c:if test="${!empty requestScope.student}">
学员列表:
<table border="1" cellpadding="10" cellspacing="0" class="table1">
<tr>
<th>学员ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>QQ</th>
<th>修真类型</th>
<th>加入时间</th>
<th>学校</th>
<th>线上学号</th>
<th>日报链接</th>
<th>立愿</th>
<th>辅导师兄</th>
</tr>
<c:forEach items="${requestScope.student.lists}" var="student">
<tr>
<th>${student.id}</th>
<th>${student.name}</th>
<th>${student.gender}</th>
<th>${student.age}</th>
<th>${student.qq}</th>
<th>${student.occupation}</th>
<th>${student.joinDate}</th>
<th>${student.school}</th>
<th>${student.number}</th>
<th>${student.dailyUrl}</th>
<th>${student.declaration}</th>
<th>${student.consoler}</th>
</tr>
</c:forEach>
</table>
</c:if>
<table border="0" cellspacing="0" cellpadding="0" width="900px">
<tr>
<td class="td2">
<span>第${requestScope.student.currPage}/ ${requestScope.pagemsg.totalPage}页</span>
<span>总记录数:${requestScope.student.totalCount } 每页显示:${requestScope.student.pageSize}</span>
<span>
<c:if test="${requestScope.student.currPage != 1}">
<a href="${pageContext.request.contextPath }/student/list?currentPage=1">[首页]</a>
<a href="${pageContext.request.contextPath }/student/list?currentPage=${requestScope.student.currPage-1}">[上一页]</a>
</c:if>
<c:if test="${requestScope.student.currPage != requestScope.student.totalPage}">
<a href="${pageContext.request.contextPath }/student/list?currentPage=${requestScope.student.currPage+1}">[下一页]</a>
<a href="${pageContext.request.contextPath }/student/list?currentPage=${requestScope.student.totalPage}">[尾页]</a>
</c:if>
</span>
</td>
</tr>
</table>
结果:
明天计划的事情:(一定要写非常细致的内容)
完成其他页面接口
遇到的问题:(遇到什么困难,怎么解决的)
service层的方法不是特别理解
收获:(通过今天的学习,学到了什么知识)
分页显示从sql语句到java是如何操作的一个流程
spring autowired自动装配
byname bytype的装配
评论