完成了Spring MVC的学习,即可使用Spring MVC进行项目实战,使用Spring MVC替换本系列第二篇中的学生模块中的Servlet技术。在这个过程中切记,Spring MVC是一个Controller层的技术,它取代的是Servlet,它不会对dao层和View造成改变。这也是分层开发的好处。
但是需要注意的是,因为使用Spring MVC,可以将方法和URL绑定,不再需要像Servlet还要使用一个type类型的参数定位方法,所以View层中对URL的调用方面需要有所改动。
本次我们会将修改功能和删除功能一并完成,期间前台会使用jQuery实现较为复杂的Dom操作。
StudentController.java:
package controller;
import dao.StudentDao;
import entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class StudentController {
@RequestMapping("show")
public ModelAndView search() {
StudentDao stuDao = new StudentDao();
List<Student> list = stuDao.search();
ModelAndView mv = new ModelAndView("show");
mv.addObject("stus", list);
return mv;
}
@RequestMapping("showAdd")
public ModelAndView showAdd() {
ModelAndView mv = new ModelAndView("showAdd");
return mv;
}
@RequestMapping("add")
public String add(Student stu) {
StudentDao stuDao = new StudentDao();
Boolean flag = stuDao.add(stu);
if (flag) {
return "redirect:show.do";
}
return null;
}
@RequestMapping("showUpdate")
public ModelAndView showUpdate(int id) {
StudentDao stuDao = new StudentDao();
Student stu = stuDao.search(id);
ModelAndView mv = new ModelAndView("showUpdate");
mv.addObject("stu", stu);
return mv;
}
@RequestMapping("update")
public String update(Student stu) {
StudentDao stuDao = new StudentDao();
Boolean flag = stuDao.update(stu);
if (flag) {
return "redirect:show.do";
}
return null;
}
@RequestMapping("delete")
public String delete(int id) {
StudentDao stuDao = new StudentDao();
Boolean flag = stuDao.delete(id);
if (flag) {
return "redirect:show.do";
}
return null;
}
}
StudentDao.java
package dao;
import entity.Student;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
//JDBC
//1.添加Jar包
public class StudentDao {
public List<Student> search() {
List<Student> list = new ArrayList<Student>();
try {
// 2.加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 3.建立连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "123456");
// 4.生成SQL执行器
Statement stat = conn.createStatement();
// 5.执行SQL
String sql = "select * from student";
ResultSet rs = stat.executeQuery(sql);
// 6.处理结果
while (rs.next()) {
Student stu = new Student();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
stu.setGender(rs.getString("gender"));
stu.setAge(rs.getInt("age"));
list.add(stu);
}
// 7.关闭资源链接
rs.close();
stat.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public boolean add(Student stu) {
int rs=0;
try {
// 2.加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 3.建立连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school?characterEncoding=utf-8", "root", "123456");
// 4.生成SQL执行器
Statement stat = conn.createStatement();
// 5.执行SQL
String sql = "insert into student (name,gender,age) values('"+stu.getName()+"','"+stu.getGender()+"',"+stu.getAge()+ ")";
rs = stat.executeUpdate(sql);
stat.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs>0;
}
public Student search(int id) {
Student stu = new Student();
try {
// 2.加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 3.建立连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "123456");
// 4.生成SQL执行器
Statement stat = conn.createStatement();
// 5.执行SQL
String sql = "select * from student where id="+id;
ResultSet rs = stat.executeQuery(sql);
// 6.处理结果
while (rs.next()) {
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
stu.setGender(rs.getString("gender"));
stu.setAge(rs.getInt("age"));
}
// 7.关闭资源链接
rs.close();
stat.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stu;
}
public Boolean update(Student stu) {
int rs=0;
try {
// 2.加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 3.建立连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school?characterEncoding=utf-8", "root", "123456");
// 4.生成SQL执行器
Statement stat = conn.createStatement();
// 5.执行SQL
String sql = "update student set name='"+stu.getName()+"',gender='"+stu.getGender()+"',age="+stu.getAge()+" where id="+stu.getId();
rs = stat.executeUpdate(sql);
stat.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs>0;
}
public Boolean delete(int id) {
int rs=0;
try {
// 2.加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 3.建立连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school?characterEncoding=utf-8", "root", "123456");
// 4.生成SQL执行器
Statement stat = conn.createStatement();
// 5.执行SQL
String sql = "delete from student where id="+id;
rs = stat.executeUpdate(sql);
stat.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs>0;
}
}
show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@page import="java.util.List,entity.*" %>
<!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="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<style>
#container {
width: 800px;
margin: 20px auto;
}
#container .selected {
background: #286090;
}
</style>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function () {
var id = 0;
$("#showAdd").click(function () {
location.href = "showAdd.do";
})
$(".data").click(function () {
id = $(this).children().eq(0).text();
$(".data").removeClass("selected")
$(this).addClass("selected")
})
$("#showUpdate").click(function () {
if (id > 0) {
location.href = "showUpdate.do?id="+id;
}else{
alert("请选中一条数据")
}
})
$("#delete").click(function () {
if (id > 0) {
location.href = "delete.do?id="+id;
}else{
alert("请选中一条数据")
}
})
})
</script>
</head>
<body>
<%
List<Student> list = (List<Student>) request.getAttribute("stus");
%>
<div id="container">
<table class="table table-striped table-bordered table-hover">
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<%
for (int i = 0; i < list.size(); i++) {
%>
<tr class="data">
<td><%=list.get(i).getId()%>
</td>
<td><%=list.get(i).getName()%>
</td>
<td><%=list.get(i).getGender()%>
</td>
<td><%=list.get(i).getAge()%>
</td>
</tr>
<%
}
%>
</table>
<button type="button" class="btn btn-primary" id="showAdd">新增</button>
<button type="button" class="btn btn-primary" id="showUpdate">修改</button>
<button type="button" class="btn btn-primary" id="delete">删除</button>
</div>
</body>
</html>
showAdd.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.List,entity.*"%>
<!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="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<style>
#container {
width: 800px;
margin: 20px auto;
}
</style>
</head>
<body>
<div id="container">
<form class="form-horizontal" action="add.do">
<div class="form-group">
<label class="col-sm-2 control-label">名字</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" placeholder="请输入名字">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">性别</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="gender" placeholder="请输入性别">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">年龄</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="age" placeholder="请输入年龄">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
</body>
</html>
showUpdate.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.List,entity.*"%>
<!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="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<style>
#container {
width: 800px;
margin: 20px auto;
}
</style>
</head>
<body>
<%Student stu=(Student)request.getAttribute("stu");%>
<div id="container">
<form class="form-horizontal" action="update.do">
<input type="hidden" name="id" value="<%=stu.getId()%>"/>
<div class="form-group">
<label class="col-sm-2 control-label">名字</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" placeholder="请输入名字" value="<%=stu.getName()%>">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">性别</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="gender" placeholder="请输入性别" value="<%=stu.getGender()%>">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">年龄</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="age" placeholder="请输入年龄" value="<%=stu.getAge()%>">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
</body>
</html>
部分效果图: