编写service接口
package com.book.service; import com.book.bean.Bookmanage; import java.util.List; public interface BookmanageService { List<Bookmanage> getBookmanageList(Bookmanage bookmanage); int addBookmanage(Bookmanage bookmanage); Bookmanage getBookmanagebyid(int id); int updateBookmanage(Bookmanage bookmanage); int deleteBookmanagebyid(int id); }
实现service接口
package com.book.service.impl; import com.book.bean.Bookmanage; import com.book.mapper.BookmanageMapper; import com.book.service.BookmanageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class BookmanageServiceImpl implements BookmanageService { @Autowired BookmanageMapper bookmanageMapper; @Override public List<Bookmanage> getBookmanageList(Bookmanage bookmanage) { return bookmanageMapper.getBookmanageList(bookmanage); } @Override public int addBookmanage(Bookmanage bookmanage) { return bookmanageMapper.addBookmanage(bookmanage); } @Override public Bookmanage getBookmanagebyid(int id) { return bookmanageMapper.getBookmanagebyid(id); } @Override public int updateBookmanage(Bookmanage bookmanage) { return bookmanageMapper.updateBookmanage(bookmanage); } @Override public int deleteBookmanagebyid(int id) { return bookmanageMapper.deleteBookmanagebyid(id); } }
创建Controller文件并且编写增删改查方法
调用service层并创建对象
package com.book.controller; import com.book.bean.Bookmanage; import com.book.service.BookmanageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller @RequestMapping("/bookmanage") public class BookmanageController { @Autowired BookmanageService bookmanageService; }
查询
转发的页面要在jsp的包中
@RequestMapping("/getList") public String getList(Bookmanage bookmanage, Model model){ List<Bookmanage>list=bookmanageService.getBookmanageList(bookmanage); model.addAttribute("list",list); return "getbookList.jsp"; }
前端jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <table border="1"> <tr> <td>编号</td> <td>书名</td> <td>作者</td> <td>时间</td> <td>类型</td> <td> 操 作 </td> </tr> <c:forEach items="${list}" var="b"> <tr> <td>${b.id}</td> <td>${b.name}</td> <td>${b.author}</td> <td>${b.time}</td> <td class="typeBom">${b.type}</td> <td><a href="/bookmanage/goupdate?id=${b.id}">修改</a> <a href="/bookmanage/delete?id=${b.id}">删除</a></td> </tr> </c:forEach> </table> <a href="/bookmanage/goadd">添加书籍</a> </body> <script> //让状态的数值转换为汉字 var typeBom=document.getElementsByClassName("typeBom"); for (var i=0;i<typeBom.length;i++){ if (typeBom[i].innerHTML==1){ typeBom[i].innerHTML="计算机" }else if (typeBom[i].innerHTML==2){ typeBom[i].innerHTML="小说" }else{ typeBom[i].innerHTML="杂志" } } </script> </html>
显示
增加
goadd方法跳转到add页面,因为/WEB-INF/jsp/包下的文件是不能随便访问的
我们springmvc配置了转发可以访问,所以写一个只有转发的goadd方法
@RequestMapping("/goadd") public String goadd(){ return "addbook.jsp"; } @RequestMapping("/add") public String getList(Bookmanage bookmanage){ bookmanageService.addBookmanage(bookmanage); return "redirect:/bookmanage/getList"; }
add方法增加完重定项到查询的页面
前端jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/bookmanage/add" method="post"> 书名:<input type="text" name="name"> <br> 作者:<input type="text" name="author"> <br> 时间:<input type="text" name="time"> <br> 类型:<select name="type"> <option value="0">请选择</option> <option value="1">计算机</option> <option value="2">小说</option> <option value="3">杂志</option> </select> <input type="submit" value="提交"> </form> </body> </html>
显示
点击提交后自动重定项到查询页面
修改
修改要先根据id查一下要修改的列
@RequestMapping("/goupdate") public String goupdate(int id,Model model){ Bookmanage bookmanage=bookmanageService.getBookmanagebyid(id); model.addAttribute("bookmanage",bookmanage); return "updatebook.jsp"; } @RequestMapping("/update") public String update(Bookmanage bookmanage){ bookmanageService.updateBookmanage(bookmanage); return "redirect:/bookmanage/getList"; }
前端jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="b"%> <html> <head> <title>Title</title> </head> <body> <form action="/bookmanage/update" method="post"> <input type="hidden" name="id" value="${bookmanage.id}"> 书名:<input type="text" name="name" value="${bookmanage.name}"> <br> 作者:<input type="text" name="author" value="${bookmanage.author}"> <br> 时间:<input type="text" name="time" value="${bookmanage.time}"> <br> 类型:<select name="type"> <option value="0">请选择</option> <option value="1">计算机</option> <option value="2">小说</option> <option value="3">杂志</option> </select> <input type="submit" value="提交"> </form> </body> </html>
删除
只用前端页面提供的id就行
@RequestMapping("/delete") public String delete(int id){ bookmanageService.deleteBookmanagebyid(id); return "redirect:/bookmanage/getList"; }