发表于: 2018-06-10 20:35:05
1 905
今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)
今天实现了增删改查,然后进行了分页查询的学习。
首先分页查询需要确定几个属性。总页数,用户当前页数,每个页面的数据数。总数据数和每个页面显示的数据,以及执行sql语句的开始查询的数据编号。
首先写出相应的pojo类
package com.entity;
import java.util.List;
public class Page {
private int totalPage; //总页数
private int totalRecord; //总记录数
private int pageSize; //每页显示数据数
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
private int currentPage; //当前页面
private int startIndex; //从记录中哪一个开始分页查询。
private List<Student> list; //每页显示数据
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public List<Student> getList() {
return list;
}
public void setList(List<Student> list) {
this.list = list;
}
@Override
public String toString() {
return "Page{" +
"totalPage=" + totalPage +
", totalRecord=" + totalRecord +
", pageSize=" + pageSize +
", currentPage=" + currentPage +
", list=" + list +
'}';
}
}
然后是相应的sql语句
<!-- 分页查询 -->
<select id="selectCount" resultType="int">
select count(*) from biao
</select>
<select id="findByPage" parameterType="com.entity.Page" resultMap="userResultMap">
select * FROM biao
<if test="startIndex!=null and pageSize!=null">
limit #{startIndex},#{pageSize}
</if>
</select>
select count(*) from biao
用来获取表中数据行数
select * FROM biao
<if test="startIndex!=null and pageSize!=null">
limit #{startIndex},#{pageSize}
select * from 表名 limit a,b 用来进行分页查询。a是代表从何处开始进行查询。b是查询结果数目。
分页查询实现类
//分页查询
public Page findByPage(int currentPage) throws Exception {
Page page = new Page();
page.setCurrentPage(currentPage);
int pageSize = 10;
page.setPageSize(pageSize);
int totalRecord = studentDao.selectCount();
page.setTotalRecord(totalRecord);
int pages = 0;
pages=(totalRecord-1)/pageSize+1;
// if(totalRecord%pageSize == 0)
// pages = totalRecord/pageSize;
// else
// pages =totalRecord/pageSize + 1;
page.setTotalPage(pages);
page.setStartIndex((currentPage-1)*pageSize);
List<Student> list = studentDao.findByPage(page);
page.setList(list);
return page;
}
controller
//分页查询
@RequestMapping(value = "page", method = RequestMethod.GET)
public ModelAndView findByPage(HttpServletRequest request,@RequestParam(value = "currentPage",defaultValue = "1",required = false) int currentPage) throws Exception {
ModelAndView modelAndView = new ModelAndView();
Page page = new Page();
page = studentService.findByPage(currentPage);
List<Student> list = page.getList();
modelAndView.addObject("list",list);
modelAndView.addObject("page",page);
modelAndView.setViewName("findByPageResult");
return modelAndView;
}
jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: fei_duo
Date: 2018/6/10
Time: 13:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" charset="UTF-8" content="text/html">
<title>显示界面</title>
</head>
<body>
<center>
<h2>查询结果</h2>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>QQ</th>
<th>修真类型</th>
<th>预计开始时间</th>
<th>毕业学校</th>
<th>学号</th>
<th>日报链接</th>
<th>立愿</th>
<th>师兄</th>
<th>从何处知晓修真院</th>
<th>添加时间</th>
<th>最后修改时间</th>
<th>查询学员</th>
<td>删除学员</td>
</tr>
<c:forEach items="${list}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.qq}</td>
<td>${student.type}</td>
<td>${student.startDay}</td>
<td>${student.university}</td>
<td>${student.studyName}</td>
<td>${student.link}</td>
<td>${student.hope}</td>
<td>${student.tutorshipSenior}</td>
<td>${student.know}</td>
<td>${student.createAt}</td>
<td>${student.updateAt}</td>
<td><form action="/home/findbyname" method="post">
<input type="hidden" name="id" value="${student.id}">
<input type="submit" value="查询">
</form>
</td>
<td>
<form action="/home/delete" method="post">
<input type="hidden" name="id" value="${student.id}">
<input type="submit" value="删除">
</form>
</td>
</tr>
</th>
</c:forEach>
</table>
<div align="center">
第${page.currentPage}/共${page.totalPage}页
<a href="page?currentPage=1">首页</a>
<a href="page?currentPage=${page.currentPage-1}">上一页</a>
<a href="page?currentPage=${page.currentPage+1}">下一页</a>
<a href="page?currentPage=${page.totalPage}">尾页</a>
<form action="page" method="get">
<input type="text" name="currentPage" size="2" value="">
<input type="submit" value="go"><br>
</form>
</div>
<a href="crudpage">返回选择界面</a>
</center>
</body>
</html>
然后了解了一下。。
REST -- REpresentational State Transfer 直接翻译:表现层状态转移。
什么鬼。
然后找了很多,发现一句话概括的豁然开朗。
URL定位资源,用HTTP动词(GET,POST,DELETE,DETC)描述操作。
首先,之所以晦涩是因为前面主语被去掉了,全称是 Resource Representational State Transfer:通俗来讲就是:资源在网络中以某种表现形式进行状态转移。分解开来:
Resource:资源,即数据(前面说过网络的核心)。比如 newsfeed,friends等;
Representational:某种表现形式,比如用JSON,XML,JPEG等;
State Transfer:状态变化。通过HTTP动词实现。
REST出处论文:Roy Fielding的毕业论文 https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm
REST描述的是在网络中client和server的一种交互形式
http://api.qc.com/v1/newsfeed: 获取某人的新鲜;
http://api.qc.com/v1/friends: 获取某人的好友列表;
http://api.qc.com/v1/profile: 获取某人的详细信息;3. 用HTTP协议里的动词来实现资源的添加,修改,删除等操作。即通过HTTP动词来实现资源的状态扭转:
GET 用来获取资源,
POST 用来新建资源(也可以用于更新资源),
PUT 用来更新资源,
DELETE 用来删除资源。比如:
DELETE http://api.qc.com/v1/friends: 删除某人的好友 (在http parameter指定好友id)
POST http://api.qc.com/v1/friends: 添加好友
UPDATE http://api.qc.com/v1/profile: 更新个人资料
在RESTful架构中,每个网址代表一种资源(resource),所以网址中不能有动词,只能有名词,而且所用的名词往往与数据库的表格名对应。一般来说,数据库中的表都是同种记录的"集合"(collection),所以API中的名词也应该使用复数。
URI使用名词而不是动词,且推荐用复数。
URI中尽量使用连字符"-"代替下划线"_"的使用
URI中统一使用小写字母
URI中不要包含文件(脚本)的扩展名
CRUD的操作不要体现在URI中,HTTP协议中的操作符已经对CRUD做了映射。
GET方法用来获取资源
PUT方法可用来新增/更新Store类型的资源
PUT方法可用来更新一个资源
POST方法可用来创建一个资源
POST方法可用来触发执行一个Controller类型资源
DELETE方法用于删除资源
明天计划的事情:(一定要写非常细致的内容)
将代码改成rest风格- -
遇到的问题:(遇到什么困难,怎么解决的)
见上
收获:(通过今天的学习,学到了什么知识)
对springmvc了解多了一点。
评论