diff --git a/src/main/java/cn/ppdxzz/controller/DormController.java b/src/main/java/cn/ppdxzz/controller/DormController.java index e4b1ab3..f6d88c5 100644 --- a/src/main/java/cn/ppdxzz/controller/DormController.java +++ b/src/main/java/cn/ppdxzz/controller/DormController.java @@ -8,12 +8,12 @@ import cn.ppdxzz.service.DormService; import cn.ppdxzz.service.StudentService; import com.github.pagehelper.PageInfo; import org.apache.commons.io.IOUtils; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; - import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -21,110 +21,153 @@ import java.io.InputStream; import java.io.PrintWriter; import java.util.List; -/** - * Description: - * - * @Date: 2020/2/19 21:01 - * @Author: PeiChen - */ + @RequestMapping("/dorm") @Controller -public class DormController { +public class DormController +{ + // 定义宿舍服务、学生服务和管理员服务的私有成员变量 private DormService dormService; private StudentService studentService; private AdminService adminService; + // 使用@Autowired注解自动注入DormService实例 @Autowired - public void setStudentService(StudentService studentService) { + public void setStudentService(StudentService studentService) + { this.studentService = studentService; } + // 使用@Autowired注解自动注入StudentService实例 @Autowired - public void setDormService(DormService dormService) { + public void setDormService(DormService dormService) + { this.dormService = dormService; } + // 使用@Autowired注解自动注入AdminService实例 @Autowired - public void setAdminService(AdminService adminService) { + public void setAdminService(AdminService adminService) + { this.adminService = adminService; } /** * 查询所有宿舍信息 - * @param page - * @param size - * @param request - * @param response - * @return - * @throws Exception + * @param page 当前页码 + * @param size 每页显示的记录数 + * @param request HTTP请求对象 + * @param response HTTP响应对象 + * @return ModelAndView对象,包含页面视图名称和数据模型 + * @throws Exception 可能抛出的异常 */ + @RequestMapping("/findAll") - public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1")int page, @RequestParam(name = "size", required = true, defaultValue = "5") int size, HttpServletRequest request, HttpServletResponse response) throws Exception { + public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1" + )int page, @RequestParam(name = "size", required = true, defaultValue = "5") int size, + HttpServletRequest request, HttpServletResponse response) throws Exception + { + // 设置请求和响应的字符编码为UTF-8 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); + + // 创建ModelAndView对象 ModelAndView mv = new ModelAndView(); + + // 初始化宿舍列表 List dorms = null; + + // 获取搜索关键字 String keyword = request.getParameter("keyword"); - if (keyword == null || "".trim().equals(keyword)) { + if (keyword == null || "".trim().equals(keyword)) + { + // 根据分页参数查询所有宿舍信息 dorms = dormService.findAll(page,size); - }else { + } + else + { + // 根据关键字进行搜索 dorms = dormService.search(page,size,keyword); } + // 创建PageInfo对象,封装分页信息 PageInfo pageInfo = new PageInfo(dorms); + + // 将分页信息添加到ModelAndView对象中 mv.addObject("pageInfo",pageInfo); + + // 设置视图名称为"dorm-list" mv.setViewName("dorm-list"); + //返回ModelAndView对象 return mv; } /** * 转发到宿舍添加页面 - * @return - * @throws Exception + * @return 视图名称字符串 + * @throws Exception 可能抛出的异常 */ @RequestMapping("/toAdd") - public String addDorm() throws Exception { + public String addDorm() throws Exception + { + // 返回宿舍添加页面的视图名称 return "dorm-add"; } /** - * 添加宿舍 - * @param dorm - * @param response - * @throws Exception + * 添加宿舍信息 + * @param dorm 宿舍对象,包含宿舍信息 + * @param response HTTP响应对象 + * @throws Exception 可能抛出的异常 */ @RequestMapping("/add") - public void add(Dorm dorm,HttpServletResponse response) throws Exception { + public void add(Dorm dorm,HttpServletResponse response) throws Exception + { response.setCharacterEncoding("utf-8"); + + // 获取PrintWriter对象,用于向客户端输出内容 PrintWriter writer = response.getWriter(); + + // 检查宿舍对象及其必要属性是否为空 if (dorm == null || dorm.getDorm_id() == null || dorm.getDorm_intro() == null || dorm.getDorm_rps() == null - || dorm.getDorm_leader() == null || dorm.getTeacher() == null) { + || dorm.getDorm_leader() == null || dorm.getTeacher() == null) + { writer.write("false"); return; } + + // 根据宿舍ID查询是否存在相同ID的宿舍 Dorm isNull = dormService.findByDormId(dorm.getDorm_id()); - if (isNull != null) { + if (isNull != null) + { writer.write("false"); return; } + + // 调用服务层方法添加宿舍信息 dormService.add(dorm); writer.write("true"); } /** - * 通过宿舍号判断该宿舍是否存在,存在返回true - * @param request - * @param response - * @throws Exception + * 通过宿舍号判断该宿舍是否存在,存在返回true + * @param request HTTP请求对象 + * @param response HTTP响应对象 + * @throws Exception 可能抛出的异常 */ @RequestMapping("/isExist") - public void isExist(HttpServletRequest request,HttpServletResponse response) throws Exception { + public void isExist(HttpServletRequest request,HttpServletResponse response) throws Exception + { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter writer = response.getWriter(); + + // 获取请求中的宿舍ID参数 String dorm_id = request.getParameter("dorm_id"); Dorm isNull = dormService.findByDormId(dorm_id); - if (isNull != null) { + if (isNull != null) + { + // 如果存在相同ID的宿舍,则返回"true" writer.write("true"); return; } @@ -132,20 +175,26 @@ public class DormController { /** * 通过id查询宿舍信息用以修改宿舍信息操作之前的信息回显 - * @param request - * @return - * @throws Exception + * @param request HTTP请求对象 + * @return ModelAndView对象,包含宿舍信息和视图名称 + * @throws Exception 可能抛出的异常 */ @RequestMapping("/toUpdate") - public ModelAndView toUpdate(HttpServletRequest request) throws Exception { + public ModelAndView toUpdate(HttpServletRequest request) throws Exception + { request.setCharacterEncoding("utf-8"); ModelAndView mv = new ModelAndView(); String id = request.getParameter("id"); - if (id == null) { + if (id == null) + { return mv; } Dorm dorm = dormService.findById(id); + + // 将宿舍信息添加到ModelAndView对象中 mv.addObject("dorm",dorm); + + // 设置视图名称为"dorm-edit" mv.setViewName("dorm-edit"); return mv; @@ -153,59 +202,85 @@ public class DormController { /** * 修改宿舍信息 - * @param dorm - * @param response - * @throws Exception + * @param dorm 宿舍对象,包含要修改的宿舍信息 + * @param response HTTP响应对象 + * @throws Exception 可能抛出的异常 */ @RequestMapping("/update") - public void update(Dorm dorm,HttpServletResponse response) throws Exception { + public void update(Dorm dorm,HttpServletResponse response) throws Exception + { response.setCharacterEncoding("utf-8"); PrintWriter writer = response.getWriter(); - if (dorm == null ||dorm.getId() == null || dorm.getDorm_id() == null || dorm.getDorm_intro() == null || dorm.getDorm_rps() == null - || dorm.getDorm_leader() == null || dorm.getTeacher() == null) { + if (dorm == null ||dorm.getId() == null || dorm.getDorm_id() == null + || dorm.getDorm_intro() == null || dorm.getDorm_rps() == null + || dorm.getDorm_leader() == null || dorm.getTeacher() == null) + { writer.write("false"); return; } + + // 调用服务层方法更新宿舍信息 dormService.update(dorm); writer.write("true"); } /** * 导出宿舍信息 - * @param response - * @throws Exception + * @param response HTTP响应对象 + * @throws Exception 可能抛出的异常 */ @RequestMapping("/export") - public void export(HttpServletResponse response) throws Exception { + public void export(HttpServletResponse response) throws Exception + { + // 获取宿舍信息的输入流 InputStream is = dormService.getInputStream(); + + // 设置响应的内容类型为Excel文件格式 response.setContentType("application/vnd.ms-excel"); + + // 设置响应头,指定下载的文件名 response.setHeader("contentDisposition","attachment;filename=dormInfo.xls"); + + // 获取ServletOutputStream对象,用于向客户端输出二进制数据 ServletOutputStream outputStream = response.getOutputStream(); + + // 将输入流的数据复制到输出流中,实现文件下载 IOUtils.copy(is,outputStream); } /** * 转发到详情页 - * @param request - * @return - * @throws Exception + * @param request HTTP请求对象 + * @return ModelAndView对象,包含宿舍信息和视图名称 + * @throws Exception 可能抛出的异常 */ @RequestMapping("/look") - public ModelAndView look(HttpServletRequest request) throws Exception { + public ModelAndView look(HttpServletRequest request) throws Exception + { ModelAndView mv = new ModelAndView(); Dorm dorm = null; String id = request.getParameter("id"); String uid = request.getParameter("uid"); - if (id == null && uid != null) { + + // 根据不同的参数情况查询宿舍信息 + if (id == null && uid != null) + { + // 根据学生ID查询学生信息,再根据学生的宿舍ID查询宿舍信息 Student stu = studentService.findBySno(uid); dorm = dormService.findByDormId(stu.getDorm_id()); - }else if (id != null) { + } + else if (id != null) + { dorm = dormService.findById(id); - }else { + } + else + { return mv; } mv.addObject("dorm",dorm); + + // 设置视图名称为"look-dorm" mv.setViewName("look-dorm"); return mv; @@ -217,18 +292,33 @@ public class DormController { * @return * @throws Exception */ + @RequestMapping("/byDorm_leader") - public ModelAndView find(HttpServletRequest request) throws Exception { + //根据宿舍ID或学生ID查询宿舍学生信息 + public ModelAndView find(HttpServletRequest request) throws Exception + { request.setCharacterEncoding("utf-8"); + + // 创建ModelAndView对象,用于返回视图和数据 ModelAndView mv = new ModelAndView(); + + // 从请求中获取参数uid String uid = request.getParameter("uid"); String dorm_id = request.getParameter("dorm_id"); - if (dorm_id != null) { + if (dorm_id != null) + { + // 根据dorm_id查询学生信息 List studentsInfo = studentService.findByDormId(dorm_id, 1); + + // 将学生信息添加到ModelAndView对象中 mv.addObject("studentsInfo",studentsInfo); + + // 设置视图名称为"dormStudentsInfo" mv.setViewName("dormStudentsInfo"); return mv; } + + // 根据uid查询学生信息 Student stu = studentService.findBySno(uid); // Dorm dormInfo = dormService.findByDormId(stu.getDorm_id()); List studentsInfo = studentService.findByDormId(stu.getDorm_id(), 1); @@ -239,12 +329,20 @@ public class DormController { return mv; } + //根据教师ID查询该教师管理的宿舍信息 @RequestMapping("/byTeacher") - public ModelAndView find1(HttpServletRequest request) throws Exception { + public ModelAndView find1(HttpServletRequest request) throws Exception + { ModelAndView mv = new ModelAndView(); String uid = request.getParameter("uid"); + + // 根据uid查询管理员信息 Admin admin = adminService.checkUid(uid); + + // 根据管理员姓名查询宿舍信息 List dorms = dormService.findByTeacher(admin.getName()); + + // 将宿舍信息添加到ModelAndView对象中 mv.addObject("dorms",dorms); mv.setViewName("dormsTeacherInfo"); return mv; @@ -252,33 +350,47 @@ public class DormController { /** * 查询所有育人导师为teacher的学生集合 + * @param page 当前页码 + * @param size 每页显示的记录数 * @param request * @return * @throws Exception */ @RequestMapping("/findStudent") - public ModelAndView findStudents(@RequestParam(name = "page", required = true, defaultValue = "1")int page, @RequestParam(name = "size", required = true, defaultValue = "5") int size,HttpServletRequest request) throws Exception { + //根据教师姓名和关键词分页查询学生集合 + public ModelAndView findStudents(@RequestParam(name = "page", required = true, defaultValue = "1")int page, + @RequestParam(name = "size", required = true, defaultValue = "5") int size, + HttpServletRequest request) throws Exception + { request.setCharacterEncoding("utf-8"); ModelAndView mv = new ModelAndView(); + + // 初始化学生列表 List students = null; + + // 从请求中获取参数name(教师姓名)、keyword(搜索关键词) String teacher = request.getParameter("name"); String keyword = request.getParameter("keyword"); + + // 打印关键词到控制台 System.out.println(keyword); - if (keyword == null || "".trim().equals(keyword) || keyword.length() == 0) { + + if (keyword == null || "".trim().equals(keyword) || keyword.length() == 0) + { + // 根据教师姓名分页查询学生信息 students = studentService.findByTeacher(page,size,teacher); } - if (keyword != null){ + + if (keyword != null) + { students = studentService.searchStudent(page,size,teacher,keyword); } PageInfo pageInfo = new PageInfo(students); mv.addObject("pageInfo",pageInfo); + + // 设置视图名称为"studentsTeacher" mv.setViewName("studentsTeacher"); return mv; } - - - - - } diff --git a/src/main/java/cn/ppdxzz/dao/DormDao.java b/src/main/java/cn/ppdxzz/dao/DormDao.java index 1683b49..16ebeed 100644 --- a/src/main/java/cn/ppdxzz/dao/DormDao.java +++ b/src/main/java/cn/ppdxzz/dao/DormDao.java @@ -3,49 +3,50 @@ package cn.ppdxzz.dao; import cn.ppdxzz.domain.Dorm; import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Repository; - import java.util.List; -/** - * Description: - * - * @Date: 2020/2/19 18:28 - * @Author: PeiChen - */ @Repository -public interface DormDao { +public interface DormDao //DormDao 的接口,用于操作宿舍信息 +{ /** * 查询所有宿舍信息 * @return * @throws Exception */ - @Select("select * from dorms") - List findAll() throws Exception; + @Select("select * from dorms")//使用 @Select 注解指定 SQL 查询语句 + List findAll() throws Exception;//返回包含所有宿舍信息的一个列表 /** - * 模糊查询宿舍信息 + * 根据关键词模糊查询宿舍信息 * @param keyword * @return * @throws Exception */ - @Select("select * from dorms where dorm_id like '%${keyword}%' or dorm_intro like '%${keyword}%' or dorm_rps like '%${keyword}%' or dorm_leader like '%${keyword}%' or teacher like '%${keyword}%' ") - List search(@Param(value = "keyword") String keyword) throws Exception; + @Select("select * from dorms where dorm_id like '%${keyword}%' or dorm_intro " + + "like '%${keyword}%' or dorm_rps like '%${keyword}%' or dorm_leader " + + "like '%${keyword}%' or teacher like '%${keyword}%' ") + //返回一个包含匹配条件的宿舍信息的列表 + List search(@Param(value = "keyword") String keyword) throws Exception;//将方法参数 keyword 绑定到 SQL 语句中的占位符 /** - * 添加宿舍 + * 向数据库中添加新的宿舍信息 * @param dorm * @throws Exception */ - @Insert("insert into dorms(dorm_id,dorm_intro,dorm_rps,dorm_leader,teacher) values(#{dorm_id},#{dorm_intro},#{dorm_rps},#{dorm_leader},#{teacher})") + //注解指定 SQL 插入语句,占位符#{dorm_id}、#{dorm_intro}、#{dorm_rps}、#{dorm_leader} 和 #{teacher}表示 Dorm 对象的属性值 + @Insert("insert into dorms(dorm_id,dorm_intro,dorm_rps,dorm_leader,teacher) " + + "values(#{dorm_id},#{dorm_intro},#{dorm_rps},#{dorm_leader},#{teacher})") void add(Dorm dorm) throws Exception; /** - * 更新宿舍信息 + * 更新数据库中的宿舍信息 * @param dorm * @throws Exception */ - @Update("update dorms set dorm_id = #{dorm_id},dorm_intro = #{dorm_intro},dorm_rps = #{dorm_rps},dorm_leader = #{dorm_leader},teacher = #{teacher} where id = #{id}") + //注解指定了SQL 更新语句, + @Update("update dorms set dorm_id = #{dorm_id},dorm_intro = #{dorm_intro}," + + "dorm_rps = #{dorm_rps},dorm_leader = #{dorm_leader},teacher = #{teacher} where id = #{id}") void update(Dorm dorm) throws Exception; /** @@ -53,16 +54,19 @@ public interface DormDao { * @param id * @throws Exception */ - @Delete("delete from dorms where id = #{id}") + @Delete("delete from dorms where id = #{id}")//占位符#{id} 表示要删除的宿舍记录的ID。 void delete(String id) throws Exception; + //根据宿舍ID查询宿舍信息 @Select("select * from dorms where dorm_id = #{dorm_id}") - Dorm findByDormId(String dorm_id) throws Exception; + Dorm findByDormId(String dorm_id) throws Exception;//返回一个 Dorm 对象 + //根据记录ID查询宿舍信息 @Select("select * from dorms where id = #{id}") - Dorm findById(String id) throws Exception; + Dorm findById(String id) throws Exception;//返回一个 Dorm 对象 + //根据教师姓名查询宿舍信息 @Select("select * from dorms where teacher = #{teacher}") - List findByTeacher(String teacher) throws Exception; + List findByTeacher(String teacher) throws Exception;//返回一个包含匹配条件的宿舍信息的列表 } diff --git a/src/main/java/cn/ppdxzz/domain/Dorm.java b/src/main/java/cn/ppdxzz/domain/Dorm.java index e6b45d3..8bdd9c2 100644 --- a/src/main/java/cn/ppdxzz/domain/Dorm.java +++ b/src/main/java/cn/ppdxzz/domain/Dorm.java @@ -2,13 +2,8 @@ package cn.ppdxzz.domain; import java.io.Serializable; -/** - * Description:宿舍实体类 - * - * @Date: 2020/2/19 17:51 - * @Author: PeiChen - */ -public class Dorm implements Serializable { +public class Dorm implements Serializable //该类的实例可以被序列化,可以将其转换为字节流以便存储、传输 +{ private Integer id;//ID private String dorm_id;//宿舍号 private String dorm_intro;//宿舍简介 @@ -16,68 +11,88 @@ public class Dorm implements Serializable { private String dorm_leader;//宿舍长 private String teacher;//管辖育人导师 - public Dorm() { + //构造方法 + public Dorm() + { + } - public Dorm(Integer id, String dorm_id, String dorm_intro, String dorm_rps, String dorm_leader, String teacher) { + public Dorm(Integer id, String dorm_id, String dorm_intro, String dorm_rps, + String dorm_leader, String teacher) { this.id = id; this.dorm_id = dorm_id; this.dorm_intro = dorm_intro; this.dorm_rps = dorm_rps; this.dorm_leader = dorm_leader; this.teacher = teacher; + } - public Integer getId() { + //Getter 和 Setter 方法 + public Integer getId() + { return id; } - public void setId(Integer id) { + public void setId(Integer id) + { this.id = id; } - public String getDorm_id() { + public String getDorm_id() + { return dorm_id; } - public void setDorm_id(String dorm_id) { + public void setDorm_id(String dorm_id) + { this.dorm_id = dorm_id; } - public String getDorm_intro() { + public String getDorm_intro() + { return dorm_intro; } - public void setDorm_intro(String dorm_intro) { + public void setDorm_intro(String dorm_intro) + { this.dorm_intro = dorm_intro; } - public String getDorm_rps() { + public String getDorm_rps() + { return dorm_rps; } - public void setDorm_rps(String dorm_rps) { + public void setDorm_rps(String dorm_rps) + { this.dorm_rps = dorm_rps; } - public String getDorm_leader() { + public String getDorm_leader() + { return dorm_leader; } - public void setDorm_leader(String dorm_leader) { + public void setDorm_leader(String dorm_leader) + { this.dorm_leader = dorm_leader; } - public String getTeacher() { + public String getTeacher() + { return teacher; } - public void setTeacher(String teacher) { + public void setTeacher(String teacher) + { this.teacher = teacher; } + //重写toString() 方法,返回一个描述 Dorm 对象的字符串 @Override - public String toString() { + public String toString() + { return "Dorm{" + "id=" + id + ", dorm_id='" + dorm_id + '\'' + diff --git a/src/main/java/cn/ppdxzz/service/DormService.java b/src/main/java/cn/ppdxzz/service/DormService.java index 7c7c5e2..73bdb77 100644 --- a/src/main/java/cn/ppdxzz/service/DormService.java +++ b/src/main/java/cn/ppdxzz/service/DormService.java @@ -1,31 +1,33 @@ package cn.ppdxzz.service; import cn.ppdxzz.domain.Dorm; - import java.io.InputStream; import java.util.List; -/** - * Description: - * - * @Date: 2020/2/19 19:52 - * @Author: PeiChen - */ -public interface DormService { +public interface DormService +{ - List findAll(int page,int size) throws Exception; + //分页查询所有宿舍信息,参数page (当前页码),参数size (每页显示的记录数) + List findAll(int page,int size) throws Exception;//返回一个包含宿舍信息的列表(List) - List search(int page,int size,String keyword) throws Exception; + //根据关键字进行分页模糊查询宿舍信息,参数keyword (查询的关键字) + List search(int page,int size,String keyword) throws Exception;//返回一个包含匹配关键字的宿舍信息的列表(List) + //添加新的宿舍信息,参数dorm(包含宿舍信息的对象) void add(Dorm dorm) throws Exception; + //更新已有的宿舍信息,参数dorm(包含更新后宿舍信息的对象) void update(Dorm dorm) throws Exception; - InputStream getInputStream() throws Exception; + //导出宿舍信息为Excel文件 + InputStream getInputStream() throws Exception;//返回一个输入流(InputStream),用于读取生成的Excel文件 + //根据宿舍号查找宿舍信息,参数 dorm_id (宿舍号) Dorm findByDormId(String dorm_id) throws Exception; + //根据ID查找宿舍信息,参数 id(宿舍的唯一标识符) Dorm findById(String id) throws Exception; - List findByTeacher(String teacher) throws Exception; + //根据导师查找宿舍信息,参数 teacher(导师的名字或标识) + List findByTeacher(String teacher) throws Exception;//返回一个包含匹配导师的宿舍信息的列表(List) } diff --git a/src/main/java/cn/ppdxzz/service/impl/DormServiceImpl.java b/src/main/java/cn/ppdxzz/service/impl/DormServiceImpl.java index fc4296c..8b9d4aa 100644 --- a/src/main/java/cn/ppdxzz/service/impl/DormServiceImpl.java +++ b/src/main/java/cn/ppdxzz/service/impl/DormServiceImpl.java @@ -8,25 +8,22 @@ import com.github.pagehelper.PageHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - import java.io.InputStream; import java.util.ArrayList; import java.util.List; -/** - * Description: - * - * @Date: 2020/2/19 20:22 - * @Author: PeiChen - */ -@Transactional -@Service("dormService") -public class DormServiceImpl implements DormService { +//基于Spring框架的Java服务类,用于管理宿舍信息 +@Transactional//该类中的所有方法都支持事务管理,这些方法在执行过程中如果出现异常,将进行回滚操作 +@Service("dormService")//将这个类标记为一个Spring的服务组件,并指定其名称为"dormService",可以通过依赖注入的方式在其他类中使用它 +public class DormServiceImpl implements DormService +{ + //用于访问数据层(DAO) private DormDao dormDao; - @Autowired - public void setDormDao(DormDao dormDao) { + @Autowired//通过自动装配机制,将DormDao类型的Bean注入到dormDao变量中 + public void setDormDao(DormDao dormDao) + { this.dormDao = dormDao; } @@ -38,8 +35,11 @@ public class DormServiceImpl implements DormService { * @throws Exception */ @Override - public List findAll(int page, int size) throws Exception { + public List findAll(int page, int size) throws Exception + { + //使用MyBatis的分页插件进行分页设置 PageHelper.startPage(page,size); + //调用DAO层的findAll方法获取数据 return dormDao.findAll(); } @@ -52,8 +52,11 @@ public class DormServiceImpl implements DormService { * @throws Exception */ @Override - public List search(int page, int size, String keyword) throws Exception { + public List search(int page, int size, String keyword) throws Exception + { + //设置分页参数 PageHelper.startPage(page,size); + //调用DAO层的search方法进行模糊查询 return dormDao.search(keyword); } @@ -63,7 +66,9 @@ public class DormServiceImpl implements DormService { * @throws Exception */ @Override - public void add(Dorm dorm) throws Exception { + public void add(Dorm dorm) throws Exception + { + //调用DAO层的add方法保存数据 dormDao.add(dorm); } @@ -73,7 +78,9 @@ public class DormServiceImpl implements DormService { * @throws Exception */ @Override - public void update(Dorm dorm) throws Exception { + public void update(Dorm dorm) throws Exception + { + //调用DAO层的update方法更新数据 dormDao.update(dorm); } @@ -83,12 +90,18 @@ public class DormServiceImpl implements DormService { * @throws Exception */ @Override - public InputStream getInputStream() throws Exception { + public InputStream getInputStream() throws Exception + { //Excel中的每列列名,依次对应数据库的字段 String[] title = new String[]{"ID","宿舍号","宿舍简介","宿舍荣誉","宿舍长","育人导师"}; + //获取所有宿舍信息 List dorms = dormDao.findAll(); + // 创建一个列表存储数据 List datalist = new ArrayList<>(); - for (int i = 0; i < dorms.size(); i++) { + + //循环遍历宿舍信息,将每个宿舍的信息存入datalist中 + for (int i = 0; i < dorms.size(); i++) + { Object[] obj = new Object[6]; obj[0] = dorms.get(i).getId(); obj[1] = dorms.get(i).getDorm_id(); @@ -98,22 +111,27 @@ public class DormServiceImpl implements DormService { obj[5] = dorms.get(i).getTeacher(); datalist.add(obj); } + //创建Excel对象 WriteExcel excel = new WriteExcel(title,datalist); + //返回生成的Excel文件流 return excel.export(); } - @Override - public Dorm findByDormId(String dorm_id) throws Exception { + @Override//根据宿舍号查找宿舍信息 + public Dorm findByDormId(String dorm_id) throws Exception + { return dormDao.findByDormId(dorm_id); } - @Override - public Dorm findById(String id) throws Exception { + @Override//根据ID查找宿舍信息 + public Dorm findById(String id) throws Exception + { return dormDao.findById(id); } - @Override - public List findByTeacher(String teacher) throws Exception { + @Override//根据导师查找宿舍信息 + public List findByTeacher(String teacher) throws Exception + { return dormDao.findByTeacher(teacher); } } diff --git a/src/main/webapp/WEB-INF/jsp/dorm-add.jsp b/src/main/webapp/WEB-INF/jsp/dorm-add.jsp index d53d953..a991018 100644 --- a/src/main/webapp/WEB-INF/jsp/dorm-add.jsp +++ b/src/main/webapp/WEB-INF/jsp/dorm-add.jsp @@ -1,10 +1,3 @@ -<%-- - Created by IntelliJ IDEA. - User: user - Date: 2020/2/19 - Time: 21:16 - To change this template use File | Settings | File Templates. ---%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> @@ -22,6 +15,7 @@ + - - + + + +