SSM——开发查询信息业务

本文详细介绍了如何通过开发DAO、Service和Controller实现学生信息的全量查询(selectAll)及按ID查询(selectById),包括SQL映射、Java代码和视图页面的整合。展示了从数据库读取到界面展示的完整流程。
摘要由CSDN通过智能技术生成

一、查询所有学生信息

### 第一步:开发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>

第五步:浏览器输入http://localhost:8080/luas/score/search进行查看,成功如下图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值