编写Controller层和视图层
bookController类编写
1、方法一:查询全部书籍
package com.star.controller;
import com.star.pojo.books;
import com.star.service.bookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/book")
public class bookController {
@Autowired
@Qualifier("bookServiceImpl")
private bookService bookService;
@RequestMapping("/allBook")
public String allBook(Model model){
List<books> books = bookService.queryAllBooks();
model.addAttribute("books",books);
return "allBook";
}
}
2、编写index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
<style type="text/css">
a {
text-decoration: none;
color: black;
font-size: 18px;
}
h3 {
180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: deepskyblue;
border-radius: 4px;
}
</style>
</head>
<body>
<h3>
<a href="${pageContext.request.contextPath}/book/allBook">点击进入列表页</a>
</h3>
</body>
</html>
3、书籍列表页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍列表</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>书籍列表 —— 显示所有书籍</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增</a>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>书籍编号</th>
<th>书籍名字</th>
<th>书籍数量</th>
<th>书籍详情</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${requestScope.get('books')}">
<tr>
<td>${book.getBookID()}</td>
<td>${book.getBookName()}</td>
<td>${book.getBookCounts()}</td>
<td>${book.getDetail()}</td>
<td>
<a class="btn btn-success" href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.getBookID()}">更改</a>
<a class="btn btn-danger" href="${pageContext.request.contextPath}/book/del/${book.getBookID()}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
4、编写方法二:添加书籍
@RequestMapping("/toAddBook")
public String toAdd(){
//跳转到添加书籍的页面
return "toAddBook";
}
@PostMapping("/addBook")
public String addBook(books book){
//接收添加书籍页面提交来的books并添加到数据库
bookService.addBook(book);
return "redirect:/book/allBook";//重定向到展示页面
}
5、添加书籍页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加页面</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>新增书籍</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/book/addBook" method="post">
书名:<input type="text" name="bookName"><br><br><br>
数量:<input type="text" name="bookCounts"><br><br><br>
描述:<input type="text" name="detail"><br><br><br>
<input type="submit" value="添加">
</form>
</div>
</body>
</html>
6、方法三:修改书籍
@RequestMapping("/toUpdateBook")
public String toUpdateBook(int id,Model model){
//通过传过来的id查找书籍并传给前端
books book = bookService.queryBookById(id);
model.addAttribute("book",book);
return "toUpdateBook";
}
@PostMapping("/UpdateBook")
public String UpdateBook(books book){
bookService.updateBook(book);
return "redirect:/book/allBook";
}
7、修改页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>修改页面</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>修改书籍</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/book/UpdateBook" method="post">
<input type="hidden" name="bookID" value="${book.getBookID()}">
书名:<input type="text" name="bookName" value="${book.getBookName()}"><br><br><br>
数量:<input type="text" name="bookCounts" value="${book.getBookCounts()}"><br><br><br>
描述:<input type="text" name="detail" value="${book.getDetail()}"><br><br><br>
<input type="submit" value="修改">
</form>
</div>
</body>
</html>
8、方法四:删除书籍
@RequestMapping("/del/{bookID}")
public String deleteBook(@PathVariable("bookID") int id){
//通过页面传递的id删除对应的书籍
bookService.deleteBookByID(id);
return "redirect:/book/allBook";
}
新增搜索功能
我们添加一个根据书籍名字查找书籍的功能
1、接口及xml编写
// 模糊查询
public List<books> selectBook(@Param("name") String name);
<select id="selectBook" parameterType="String" resultType="books">
select * from ssmbuild.books
where bookName like CONCAT ('%',#{name},'%')
-- 采用模糊查询
</select>
2、service层编写
bookService
// 模糊查询
public List<books> selectBook(@Param("name") String name);
bookServiceImpl
public List<books> selectBook(String name) {
return bookMapper.selectBook(name);
}
3、controller层编写
@PostMapping("/select")
public String selectBook(String bookName,Model model){
List<books> books = bookService.selectBook(bookName);
if(books.size()==0){//如果没找到就返回错误信息并展示所有的书籍
books = bookService.queryAllBooks();
model.addAttribute("error","未查到!");
}
model.addAttribute("books",books);
return "allBook";
}
4、给页面中添加搜索框错误信息标签
这两个div都写在row的div里面,与新增在同一行
<%--在新增帮加一个显示所有书籍的链接--%>
<a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook">显示全部书籍</a>
<div class="col-md-4 column">
<span style="color:red;font-weight: bold;font-size: 20px;">
${error}
</span>
</div>
<div class="col-lg-4">
<form action="${pageContext.request.contextPath}/book/select" method="post">
<div class="input-group">
<input type="text" class="form-control" name="bookName" placeholder="请输入要查找的书籍名称">
<span class="input-group-btn">
<button class="btn btn-primary" type="submit">查找</button>
</span>
</div>
</form>
</div>
效果展示: