1. Many2One
1.1 数据建模
关系图

表结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| drop table if exists t_clazz;
create table t_clazz ( bno varchar(20) not null, name varchar(20), primary key (bno) );
alter table t_clazz comment '班级表';
drop table if exists t_student;
create table t_student ( sid varchar(20) not null comment '学号 主键', name varchar(20) comment '姓名', age int(3) comment '年龄', sex int(1) comment '性别 0 男 1 女', bno varchar(20), primary key (sid) );
alter table t_student comment '学生信息表';
alter table t_student add constraint FK_stu_clazz_bno foreign key (bno) references t_clazz (bno) on delete restrict on update restrict;
|
表数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public abstract class BaseTest { protected JDBCTemplate jdbcTemplate = new JDBCTemplate() ; @Before public void before(){ jdbcTemplate.update("delete from t_student ") ; jdbcTemplate.update("delete from t_clazz ") ;
jdbcTemplate.update("insert into t_clazz(bno,name) values ('B001','Java177')") ; jdbcTemplate.update("insert into t_clazz(bno,name) values ('B002','Java178')") ; for (int i = 0; i < 68; i++) { jdbcTemplate.update("insert into t_student(sid,name,age,sex,bno) values ('S"+(i+1)+"','张三"+(i+1)+"',18,0,'B001')") ; } }
}
|
1.2 领域建模
1 2 3 4
| public class Clazz { private String bno ; private String name ; }
|
1 2 3 4 5 6 7
| public class Student { private String sid ; private String name ; private Integer age ; private Integer sex ; private Clazz clazz = new Clazz() ; }
|
1.3 列表
DAO
1 2
| select sid,s.name,age,sex,c.bno "clazz.bno",c.name "clazz.name" from t_student s left join t_clazz c on s.bno = c.bno where 1=1
|
1 2 3 4 5
| if (query!=null && StringUtils.isNotEmpty(query.getName())) { sb.append(" and s.name like ?") ; paramList.add("%"+query.getName()+"%"); }
|
jsp
list-stu.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <table class="table table-striped table-bordered table-hover"> <thead> <tr> <th><input type="checkbox" /></th> <th>学号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>班号</th> <th>班名</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach items="${studentPage.list}" var="student"> <tr> <td><input type="checkbox" name="sid" value="${student.sid}" /></td> <td>${student.sid}</td> <td>${student.name}</td> <td>${student.age}</td> <td>${student.sex eq 0 ?"男":"女"}</td> <td>${student.clazz.bno}</td> <td>${student.clazz.name}</td> <td> <a href="${pageContext.request.contextPath}/view-stu.do?sid=${student.sid}" class="btn btn-info btn-sm">修改</a> <a href="${pageContext.request.contextPath}/del-stu.do?sid=${student.sid}" class="btn btn-danger btn-sm">删除</a> </td> </tr> </c:forEach> </tbody> </table>
|
1.4 新增before
DAO
1 2 3
| public interface ClazzDAO { List<Clazz> selectAll() ; }
|
1 2 3 4 5 6 7 8 9 10 11
| public class ClazzDAOImpl implements ClazzDAO { private JDBCTemplate jdbcTemplate = new JDBCTemplate() ;
@Override public List<Clazz> selectAll() { String DQL = "select bno,name from t_clazz" ; Object[] paramAy = {} ; return jdbcTemplate.queryList(DQL,Clazz.class,paramAy); }
}
|
Service
1 2 3 4 5 6 7 8 9 10 11 12
| public class ClazzService { private ClazzDAO clazzDAO = new ClazzDAOImpl() ;
public Collection<Clazz> list(){ return clazzDAO.selectAll(); }
}
|
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @WebServlet(..."/toAddView-stu.do"}) public class StudentServlet extends HttpServlet { private ClazzService clazzService = new ClazzService() ; ... service(..){ else if ("/toAddView-stu.do".equals(requestURI)){ this.toAddView(request,response) ; } } protected void toAddView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Collection<Clazz> clazzList = clazzService.list(); request.setAttribute("clazzList",clazzList); request.getRequestDispatcher("/add-stu.jsp").forward(request,response); } }
|
Jsp
list-stu.jsp
1
| <a href="${pageContext.request.contextPath}/toAddView-stu.do" class="btn btn-primary">添加学生</a>
|
add-stu.jsp
1 2 3 4 5 6 7 8 9 10 11
| <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div class="form-group"> <label for="age">班级</label> <select name="clazz.bno"> <option value="">--请选择班级--</option> <c:forEach items="${clazzList}" var="clazz"> <option value="${clazz.bno}">${clazz.name}</option> </c:forEach> </select> </div>
|
1.5 新增
DAO
1 2 3 4 5 6
| @Override public boolean insert(Student student) { String DQL = "insert into t_student(sid,name,age,sex,bno) values (?,?,?,?,?)" ; Object[] paramAy = {student.getSid(),student.getName(),student.getAge(),student.getSex(),student.getClazz().getBno()} ; return jdbcTemplate.update(DQL,paramAy); }
|
Controller
1 2
| response.sendRedirect(request.getContextPath()+"/page-stu.do");
|
1.6 查看
jsp
modify-stu.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div class="form-group"> <label for="age">班级</label> <select name="clazz.bno" class="form-control"> <option value="">--请选择班级--</option> <c:forEach items="${clazzList}" var="clazz"> <option value="${clazz.bno}">${clazz.name}</option> </c:forEach> </select> </div>
$("select").val("${student.clazz.bno}")
|
Controller
1 2 3 4 5 6 7 8 9 10 11 12
| protected void view(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String sid = request.getParameter("sid"); Student student = studentService.get(sid); request.setAttribute("student",student); Collection<Clazz> clazzList = clazzService.list(); request.setAttribute("clazzList",clazzList); request.getRequestDispatcher("/modify-stu.jsp").forward(request,response); }
|
DAO
1
| String DQL = "select sid,s.name,age,sex,c.bno \"clazz.bno\",c.name \"clazz.name\" from t_student s left join t_clazz c on s.bno = c.bno where s.sid=?" ;
|
1.7 修改
DAO
1 2 3 4 5 6
| @Override public boolean update(Student student) { String DQL = "update t_student set name=?,age=?,sex=?,bno=? where sid=?" ; Object[] paramAy = {student.getName(),student.getAge(),student.getSex(),student.getClazz().getBno(),student.getSid()} ; return jdbcTemplate.update(DQL,paramAy); }
|
2. One2Many
2.1 列表[单表]
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| @WebServlet(name = "ClazzServlet",urlPatterns = {"/list-clazz.do"}) public class ClazzServlet extends HttpServlet { private ClazzService clazzService = new ClazzService() ;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestURI = request.getRequestURI(); requestURI = requestURI.replace(request.getContextPath(),""); if ("/list-clazz.do".equals(requestURI)){ this.list(request,response) ; } }
protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Collection<Clazz> clazzCollection = clazzService.list(); request.setAttribute("clazzList",clazzCollection); request.getRequestDispatcher("/list-clazz.jsp").forward(request,response); } }
|
jsp
index.jsp
1
| <a href="${pageContext.request.contextPath}/list-clazz.do">班级列表</a> <br>
|
list-clazz.jsp
1 2 3 4 5 6 7 8 9 10 11
| <c:forEach items="${clazzList}" var="clazz"> <tr> <td><input type="checkbox" name="bno" value="${clazz.sid}" /></td> <td>${clazz.bno}</td> <td>${clazz.name}</td> <td> <a href="${pageContext.request.contextPath}/view-clazz.do?sid=${clazz.bno}" class="btn btn-info btn-sm">修改</a> <a href="${pageContext.request.contextPath}/del-clazz.do?sid=${clazz.bno}" class="btn btn-danger btn-sm">删除</a> </td> </tr> </c:forEach>
|
2.2 新增准备【裸跳页面】
逻辑上: 直接怼到页面 比如 add-clazz.jsp
OCP A. 代码复用 B.新增班级 【权限访问控制?当前登录用户是否有权限?】
jsp
list-clazz.jsp
1
| <a href="${pageContext.request.contextPath}/toAddView-clazz.do" class="btn btn-primary">添加班级</a>
|
add-clazz.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <h1>新增班级</h1> <form action="${pageContext.request.contextPath}/add-clazz.do" method="post"> <div class="form-group"> <label for="sid">班号</label> <input type="text" class="form-control" id="sid" name="bno" value="${param.bno}" placeholder="请输入班号" /> </div> <div class="form-group"> <label for="sname">班名</label> <input type="text" class="form-control" id="sname" name="name" value="${param.name}" placeholder="请输入班名" /> </div> <div class="form-group" style="text-align: center"> <button type="submit" class="btn btn-primary">新增</button> <button type="submit" class="btn btn-default">重置</button> </div> </form>
|
Controller
1 2 3 4 5 6 7 8 9 10 11
| @WebServlet(name = "ClazzServlet",urlPatterns = {"/list-clazz.do","/toAddView-clazz.do"}) .... else if("/toAddView-clazz.do".equals(requestURI)){ this.toAddView(request,response) ; }
protected void toAddView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/add-clazz.jsp").forward(request,response); }
|
2.3 新增【单表】
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| @WebServlet(name = "ClazzServlet",urlPatterns = {"/list-clazz.do","/toAddView-clazz.do","/add-clazz.do"})
else if("/add-clazz.do".equals(requestURI)){ this.add(request,response) ; }
protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Clazz clazz = RequestUtil.parseParameter(request, Clazz.class); boolean saveRs = clazzService.save(clazz); if (saveRs){ response.sendRedirect(request.getContextPath()+"/list-clazz.do"); return; } request.setAttribute("errorMsg","班号已经存在!!!"); request.getRequestDispatcher("/toAddView-clazz.do").forward(request,response); }
|
Service
1 2 3 4 5 6 7 8 9 10 11 12 13
| ... ClazzService { public boolean save(Clazz clazz) { String bno = clazz.getBno() ; Clazz dbClazz = clazzDAO.selectById(bno); if (dbClazz!=null){ return false ; } return clazzDAO.insert(clazz) ; } }
|
DAO
1 2 3 4 5
| ... ClazzDAO{ Clazz selectById(String bno);
boolean insert(Clazz clazz); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ... ClazzDAOImpl ... { @Override public Clazz selectById(String bno) { String DQL = "select bno,name from t_clazz where bno=?" ; Object[] paramAy = {bno} ; return jdbcTemplate.queryObject(DQL,Clazz.class,paramAy); }
@Override public boolean insert(Clazz clazz) { String DQL = "insert into t_clazz(bno,name) values (?,?)" ; Object[] paramAy = {clazz.getBno(),clazz.getName()} ; return jdbcTemplate.update(DQL,paramAy); } }
|
2.4 删除【单表/子记录依赖】
jsp
list-clazz.jsp
1 2 3
| <form action='${pageContext.request.contextPath}/del-clazz.do'> <td><input type="checkbox" name="bno" value="${clazz.bno}" /></td> <a href="${pageContext.request.contextPath}/del-clazz.do?bno=${clazz.bno}" class="btn btn-danger btn-sm">删除</a>
|
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @WebServlet(name = "ClazzServlet",urlPatterns = {"/list-clazz.do","/toAddView-clazz.do","/add-clazz.do","/del-clazz.do"})
else if("/del-clazz.do".equals(requestURI)){ this.del(request,response) ; }
protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] idAy = request.getParameterValues("bno"); String id =""; for (String s : idAy) { id +=s+"," ; } id = id.substring(0,id.length()-1) ; boolean removeRs = clazzService.remove(id); String msg = removeRs?"":"del-fail" ; response.sendRedirect(request.getContextPath()+"/list-clazz.do?msg="+msg); }
|
Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class ClazzService { private StudentDAO studentDAO = new StudentDAOImpl() ;
public boolean remove(String id) { String[] sidAy = id.split(","); for (String bno : sidAy) { List<Student> studentList = studentDAO.selectByFk(bno); if (!studentList.isEmpty()){ return false ; } } return clazzDAO.delete(sidAy) ; } }
|
DAO
StudentDAO
1
| List<Student> selectByFk(String bno) ;
|
1 2 3 4 5 6
| @Override public List<Student> selectByFk(String bno) { String DQL = "select sid,name,age,sex from t_student where bno=?" ; Object[] paramAy = {bno} ; return jdbcTemplate.queryList(DQL,Student.class,paramAy); }
|
ClazzDAO
1
| boolean delete(String ... idAy) ;
|
1 2 3 4 5 6 7 8 9 10 11 12
| public class ClazzDAOImpl implements ClazzDAO { @Override public boolean delete(String... sidAy) { String DML = "delete from t_clazz where bno=?" ; Object[][] paramAy = new Object[sidAy.length][1] ; for (int i = 0; i < sidAy.length; i++) { paramAy[i][0] = sidAy[i] ; } return jdbcTemplate.updateBatch(DML,paramAy); }
}
|
2.5 查看【N+1】
jsp
list-clazz.jsp
1
| <a href="${pageContext.request.contextPath}/view-clazz.do?bno=${clazz.bno}" class="btn btn-info btn-sm">修改</a>
|
modify-clazz.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <input type="text" class="form-control" id="sname" name="name" value="${clazz.name}" placeholder="请输入班名" />
<c:forEach items="${clazz.studentList}" var="student"> <tr> <td>${student.sid}</td> <td>${student.name}</td> <td>${student.age}</td> <td>${student.sex eq 0 ?"男":"女"}</td> </tr> </c:forEach>
|
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @WebServlet(name = "ClazzServlet",urlPatterns = {"/list-clazz.do","/toAddView-clazz.do","/add-clazz.do","/del-clazz.do","/view-clazz.do"})
else if("/view-clazz.do".equals(requestURI)){ this.view(request,response) ; }
protected void view(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String bno = request.getParameter("bno"); Clazz clazz = clazzService.get(bno) ; request.setAttribute("clazz",clazz); request.getRequestDispatcher("/modify-clazz.jsp").forward(request,response); }
|
Service
1 2 3 4 5 6 7 8 9 10
| public Clazz get(String bno) { Clazz clazz = clazzDAO.selectById(bno); List<Student> studentList = studentDAO.selectByFk(bno); clazz.setStudentList(studentList); return clazz ; }
|
2.6 修改【单表】
jsp
modify-clazz.jsp
1 2
| <form action="${pageContext.request.contextPath}/modify-clazz.do" method="post"> ....
|
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @WebServlet(name = "ClazzServlet",urlPatterns = {"/list-clazz.do","/toAddView-clazz.do","/add-clazz.do","/del-clazz.do","/view-clazz.do","/modify-clazz.do"})
else if("/modify-clazz.do".equals(requestURI)){ this.modify(request,response) ; }
protected void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Clazz clazz = RequestUtil.parseParameter(request, Clazz.class); boolean modifyRs = clazzService.modify(clazz); if (modifyRs){ response.sendRedirect(request.getContextPath()+"/list-clazz.do"); return; } request.setAttribute("errorMsg","修改班级失败!!!"); request.getRequestDispatcher("/view-clazz.do").forward(request,response); }
|
Service
1 2 3
| public boolean modify(Clazz clazz) { return clazzDAO.update(clazz) ; }
|
DAO
1
| boolean update(Clazz clazz);
|
1 2 3 4 5 6
| @Override public boolean update(Clazz clazz) { String DQL = "update t_clazz set name=? where bno=?" ; Object[] paramAy = {clazz.getName(),clazz.getBno()} ; return jdbcTemplate.update(DQL,paramAy); }
|
3. Many2Many
3.1 数据建模
表关系

表结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| drop table if exists t_clazz;
create table t_clazz ( bno varchar(20) not null, name varchar(20), primary key (bno) );
alter table t_clazz comment '班级表';
drop table if exists t_student;
create table t_student ( sid varchar(20) not null comment '学号 主键', name varchar(20) comment '姓名', age int(3) comment '年龄', sex int(1) comment '性别 0 男 1 女', bno varchar(20), primary key (sid) );
alter table t_student comment '学生信息表';
drop table if exists t_teacher;
create table t_teacher ( tno varchar(20) not null, tname varchar(20), primary key (tno) );
alter table t_teacher comment '教师表';
drop table if exists t_student_teacher;
create table t_student_teacher ( tno varchar(20) not null, sid varchar(20) not null comment '学号 主键', primary key (tno, sid) );
alter table t_student_teacher comment '教师学生关系表';
alter table t_student_teacher add constraint FK_stu_teacher_teacher_tno foreign key (tno) references t_teacher (tno) on delete restrict on update restrict;
alter table t_student_teacher add constraint FK_stu_teacher_stu_sid foreign key (sid) references t_student (sid) on delete restrict on update restrict;
alter table t_student add constraint FK_stu_clazz_bno foreign key (bno) references t_clazz (bno) on delete restrict on update restrict;
|
表数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| @Before public void before(){ jdbcTemplate.update("delete from t_student_teacher ") ; jdbcTemplate.update("delete from t_teacher ") ; jdbcTemplate.update("delete from t_student ") ; jdbcTemplate.update("delete from t_clazz ") ;
jdbcTemplate.update("insert into t_clazz(bno,name) values ('B001','Java177')") ; jdbcTemplate.update("insert into t_clazz(bno,name) values ('B002','Java178')") ; for (int i = 0; i < 68; i++) { jdbcTemplate.update("insert into t_student(sid,name,age,sex,bno) values ('S"+(i+1)+"','张三"+(i+1)+"',18,0,'B001')") ; } jdbcTemplate.update("insert into t_teacher(tno,tname) values ('T001','赵贺贺')") ; jdbcTemplate.update("insert into t_teacher(tno,tname) values ('T002','赵健')") ; jdbcTemplate.update("insert into t_teacher(tno,tname) values ('T003','马美平')") ;
for (int i = 0; i < 68; i++) { jdbcTemplate.update("insert into t_student_teacher(sid,tno) values ('S"+(i+1)+"','T001'),('S"+(i+1)+"','T002')") ; } }
|
3.2 列表
pojo
1 2 3 4
| public class Teacher { private String tno ; private String tname ; }
|
1 2 3 4 5
| public class Student { ... private List<Teacher> teacherList ; }
|
DAO
1 2 3
| public interface TeacherDAO { List<Teacher> selectBySid(String sid) ; }
|
1 2 3 4 5 6 7 8 9
| public class TeacherDAOImpl implements TeacherDAO { private JDBCTemplate jdbcTemplate = new JDBCTemplate() ; @Override public List<Teacher> selectBySid(String sid) { String DQL = "select tno,tname from t_teacher where tno in (select tno from t_student_teacher where sid = ?)" ; Object[] paramAy = {sid} ; return jdbcTemplate.queryList(DQL, Teacher.class,paramAy); } }
|
service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class StudentService { private TeacherDAO teacherDAO = new TeacherDAOImpl() ;
public void selectPage(Page<Student, StudentQuery> page){ long count = studentDAO.count(page.getCond()); List<Student> studentList = studentDAO.selectPage(page); for (Student student : studentList) { List<Teacher> teacherList = teacherDAO.selectBySid(student.getSid()); student.setTeacherList(teacherList); } page.setTotalRecord(count); page.setList(studentList); } }
|
jsp
list-stu.jsp
1 2 3 4 5 6
| <td> <c:forEach items="${student.teacherList}" var="teacher" varStatus="vs"> ${teacher.tname} <c:if test="${not vs.last}">,</c:if> </c:forEach> </td>
|
3.3 显示关联教师
DAO
1 2 3
| public interface TeacherDAO { List<Teacher> selectAll(); }
|
1 2 3 4 5 6 7 8
| public class TeacherDAOImpl implements TeacherDAO { @Override public List<Teacher> selectAll() { String DQL = "select tno,tname from t_teacher" ; Object[] paramAy = {} ; return jdbcTemplate.queryList(DQL, Teacher.class,paramAy); } }
|
Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class StudentService { private TeacherDAO teacherDAO = new TeacherDAOImpl() ;
public Student get(String sid){ Student student = studentDAO.selectById(sid) ; List<Teacher> teacherList = teacherDAO.selectBySid(sid); student.setTeacherList(teacherList); return student ; } }
|
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @WebServlet(.... "/toSelectView-teacher.do"})
else if ("/toSelectView-teacher.do".equals(requestURI)){ this.toSelectTeacherView(request,response) ; }
protected void toSelectTeacherView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String sid = request.getParameter("sid"); Student student = studentService.get(sid); List<Teacher> teacherList = teacherService.list() ; request.setAttribute("student",student); request.setAttribute("teacherList",teacherList); request.getRequestDispatcher("/select-teacher.jsp").forward(request,response); }
|
Jsp
list-stu.jsp
1
| <a href="${pageContext.request.contextPath}/toSelectView-teacher.do?sid=${student.sid}" class="btn btn-info btn-sm">关联教师</a>
|
select-teacher.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| <div class="container"> <h1>学生选择教师</h1> <div class="widget-content padded clearfix"> <!-- 学生基本信息 --> <table class="table detail-view"> <tbody> <tr> <th>学号</th> <td>${student.sid}</td> </tr> <tr> <th>姓名</th> <td>${student.name}</td> </tr> <tr> <th>年龄</th> <td>${student.age}</td> </tr> <tr> <th>性别</th> <td>${student.sex eq 0 ?"男":"女"}</td> </tr> <tr> <th>班级</th> <td>${student.clazz.name}</td> </tr>
</tbody> </table> <!-- 教师信息 --> <form action=""> <table class="table table-striped table-bordered table-hover"> <thead> <tr> <th><input type="checkbox" /></th> <th>工号</th> <th>姓名</th> </tr> </thead> <tbody> <c:forEach items="${teacherList}" var="teacher"> <tr> <td><input type="checkbox" name="tno" value="${teacher.tno}"/></td> <td>${teacher.tno}</td> <td>${teacher.tname}</td> </tr> </c:forEach> </tbody> </table> <div class="form-group" style="text-align: center"> <button type="submit" class="btn btn-primary">关联教师</button> <button type="submit" class="btn btn-default">重置</button> </div> </form> </div> </div> <script> <c:forEach items="${student.teacherList}" var="teacher"> $(":checkbox[value='${teacher.tno}']").prop('checked',true) </c:forEach> </script>
|
3.4 设置关联教师
jsp
select-teacher.jsp
1 2 3 4 5 6
| <!-- 教师信息 --> <form action="${pageContext.request.contextPath}/set-teacher.do"> <input type="hidden" name="sid" value="${student.sid}"> .... <tr> <td><input type="checkbox" name="tno" value="${teacher.tno}"/></td>
|
DAO
1 2 3 4 5
| public interface StudentDAO { boolean deleteRefBySid(String sid) ;
boolean insertRef(Student student) ; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class StudentDAOImpl implements StudentDAO { @Override public boolean deleteRefBySid(String sid) { String DQL = "delete from t_student_teacher where sid = ?" ; Object[] paramAy = {sid} ; return jdbcTemplate.update(DQL,paramAy); }
@Override public boolean insertRef(Student student) { String sid = student.getSid(); List<Teacher> teacherList = student.getTeacherList(); String DML = "insert into t_student_teacher(sid,tno) values(?,?)" ; Object[][] paramAy = new Object[teacherList.size()][2] ; for (int i = 0; i < teacherList.size(); i++) { Teacher teacher = teacherList.get(i); paramAy[i][0] = sid ; paramAy[i][1] = teacher.getTno() ; } return jdbcTemplate.updateBatch(DML,paramAy); } }
|
Service
1 2 3 4 5 6 7 8 9 10
| public class StudentService {
public boolean setTeacher(Student student){ boolean delRs = studentDAO.deleteRefBySid(student.getSid()) ; boolean insertRs = studentDAO.insertRef(student) ; return delRs && insertRs ; } }
|
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| @WebServlet(..."/set-teacher.do"})
else if ("/set-teacher.do".equals(requestURI)){ this.setTeacher(request,response) ; }
protected void setTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String sid = request.getParameter("sid"); String[] tnoAy = request.getParameterValues("tno"); Student student = new Student(); student.setSid(sid); List<Teacher> teacherList = new ArrayList<>() ; for (String tno : tnoAy) { Teacher teacher = new Teacher(); teacher.setTno(tno); teacherList.add(teacher); } student.setTeacherList(teacherList); boolean rs = studentService.setTeacher(student) ; response.sendRedirect(request.getContextPath()+"/page-stu.do"); }
|