一、查询所有学生信息
### 第一步:开发dao
(1) 在scoreDao接口中添加查询的方法,方法定义如下:
public abstract List<ScoreModel> selectAll() ;
(2)在映射文件(/luas/src/main/resources/mapper/score.xml)中配置select语句
<select id="selectAll" resultType="cn.itlaobing.models.ScoreModel">
SELECT * FROM score
</select>
第二步:开发service
在ScoreServices类中添加查询的方法,代码如下:
public List<ScoreModel> selectAll(){
return scoreDao.selectAll();
}
第三步:开发controller
在ScoreController类中定义查询的方法,代码如下:
@RequestMapping("/selectAll")
public String selectAll(Model model) {
List<ScoreModel> list = scoreService.selectAll();
model.addAttribute("scores", list);
return "score/list";
}
^ : 解析: Model接口是专门用于控制器中的数据传递到界面的作用。
第四步:开发视图
在views/score目录下创建list.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>
<head>
<meta charset="UTF-8">
<title>查询学生</title>
<link rel="stylesheet"
href="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.min.css" />
</head>
<body>
<table class="table table-hover ">
<tr>
<td><button type="button" class="btn btn-info">
<a href="${pageContext.request.contextPath }/score/selectAll">查询学生</a>
</button></td>
<td><button type="button" class="btn btn-info">
<a href="${pageContext.request.contextPath }/score/preparedInsert">添加学生</a>
</button></td>
</tr>
</table>
<table class="table table-bordered ">
<caption align="center">成绩列表</caption>
<tr>
<td align="center">序号</td>
<td align="center">姓名</td>
<td align="center">性别</td>
<td align="center">课程名称</td>
<td align="center">课程成绩</td>
</tr>
<c:forEach items="${scores }" var="item">
<tr>
<td align="center">${item.id}</td>
<td align="center">${item.stuName}</td>
<td align="center">${item.gender}</td>
<td align="center">${item.courseName}</td>
<td align="center">${item.courseScore}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
第五步:浏览器输入http://localhost:8080/luas/score/selectAll进行查看,成功如下图:
二、按id查询学生信息
第一步:开发dao
(1) 在scoreDao接口中添加按id查询的方法,方法定义如下:
// 按id查找学生信息接口
public abstract ScoreModel selectById(String id);
(2)在映射文件(/luas/src/main/resources/mapper/score.xml)中配置select语句
<select id="selectById" resultType="cn.itlaobing.models.ScoreModel" parameterType="string">
select * from score where id=#{value}
</select>
第二步:开发service
在ScoreServices类中添加查询的方法,代码如下:
public List<ScoreModel> selectAll(){
return scoreDao.selectAll();
}
第三步:开发controller
在ScoreController类中定义查询的方法,代码如下:
@RequestMapping("/search")
public String search() {
return "score/search";
}
@RequestMapping("/selectById")
public String selectById(String id, Model model) {
ScoreModel scoreModel = scoreService.selectById(id);
if (scoreModel == null) {
model.addAttribute("message", "查无此人");
}
model.addAttribute("id", id);
model.addAttribute("scoreModel", scoreModel);
return "score/search";
}
^ : 解析: Model接口是专门用于控制器中的数据传递到界面的作用。
第四步:开发视图
在views/score目录下创建list.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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet"
href="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.min.css" />
</head>
<body style="margin-top:200px ">
<center>
<div style="width:600px">
<form class="form-inline"
action="${pageContext.request.contextPath }/score/selectById"
method="get">
<div class="form-group">
<label class="sr-only" for="exampleInputAmount">Amount (in
dollars)</label>
<div class="input-group">
<div class="input-group-addon">ID</div>
<input type="text" class="form-control" id="exampleInputAmount"
placeholder="学号" name="id" value="${id }">
</div>
</div>
<button type="submit" class="btn btn-primary">查询</button>
</form>
${message }
<hr />
<table class="table table-bordered ">
<caption>添加学生成绩</caption>
<tr>
<td align="center">序号</td>
<td align="center">${scoreModel.id }</td>
</tr>
<tr>
<td align="center">姓名</td>
<td align="center">${scoreModel.stuName }</td>
</tr>
<tr>
<td align="center">性别</td>
<td align="center">${scoreModel.gender }</td>
</tr>
<tr>
<td align="center">课程名称</td>
<td align="center">${scoreModel.courseName }</td>
</tr>
<tr>
<td align="center">课程成绩</td>
<td align="center">${scoreModel.courseScore }</td>
</tr>
</table>
<table border="0">
<tr>
<td width="300px"><button type="button" class="btn btn-info">
<a href="${pageContext.request.contextPath }/score/selectAll">查询学生</a>
</button></td>
<td><button type="button" class="btn btn-info">
<a href="${pageContext.request.contextPath }/score/preparedInsert">添加学生</a>
</button></td>
</tr>
</table>
</div>
</center>
</body>
</html>