ssm框架整合案例--模糊查询书籍功能
作者:互联网
ssm框架整合案例
01: ssm框架整合案例–整合Mybatis层
02: ssm框架整合案例–整合Spring层
03: ssm框架整合案例–整合SpringMVC层
04: ssm框架整合案例–查询书籍功能
05: ssm框架整合案例–添加书籍功能
06: ssm框架整合案例–修改删除书籍功能
07: ssm框架整合案例–模糊查询书籍功能
1. ssm框架整合案例–模糊查询书籍功能
1.1 新增一个模糊查询的方法
BookMapper.java
// 根据名称查询数据
List<Books> queryBooksByName(@Param("booksName") String booksName);
1.2 实现方法
BookMapper.xml
<select id="queryBooksByName" resultType="books">
select *
from ssmbuild.books
where bookName like #{booksName}
</select>
1.3 在Service层编写方法
BookService.java
// 根据名称查询数据
List<Books> queryBooksByName(String booksName);
1.4 在Service层实现方法
BookServiceImpl.java
public List<Books> queryBooksByName(String booksName) {
return bookMapper.queryBooksByName(booksName);
}
1.5 编写业务实现接口
BookController.java
package com.tian.controller;
import com.tian.pojo.Books;
import com.tian.service.BookService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/book")
public class BookController {
private final BookService bookService;
public BookController(@Qualifier("bookServiceImpl") BookService bookService) {
this.bookService = bookService;
}
@RequestMapping("/allBook")
public String list(Model model) {
List<Books> books = bookService.queryAllBook();
model.addAttribute("books", books);
return "allBook";
}
// 跳转到添加书籍页面
@RequestMapping("/toAddBook")
public String toAddPaper() {
return "addBook";
}
// 添加书籍的实现接口
@RequestMapping("/addBook")
public String addPaper(Books books) {
System.out.println(books);
bookService.addBook(books);
return "redirect:/book/allBook"; // 重定向到@RequestMapping("allBook")
}
// 跳转到添加修改页面
@RequestMapping("/toUpdateBook")
public String toUpdateBook(Model model, int id) {
Books books = bookService.queryBookById(id);
model.addAttribute("book", books);
return "updateBook";
}
// 修改数据的实现接口
@RequestMapping("/updateBook")
public String updateBook(Model model, Books book) {
System.out.println(book);
bookService.updateBook(book);
Books books = bookService.queryBookById(book.getBookID());
model.addAttribute("books", books);
return "redirect:/book/allBook";
}
// 删除数据的接口
@RequestMapping("/del/{bookId}")
public String deleteBook(@PathVariable("bookId") int id) {
bookService.deleteBookById(id);
return "redirect:/book/allBook";
}
// 根据名称查询数据
@RequestMapping("/queryBooks")
public String queryBooks(String booksName, Model model) {
List<Books> books = bookService.queryBooksByName("%" + booksName + "%");
model.addAttribute("books", books);
return "allBook";
}
}
1.6 修改前端allBook页面
allBook.jsp
<%@ 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 class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook">显示所有书籍</a>
</div>
<div class="col-md-4 column">
<form action="${pageContext.request.contextPath}/book/queryBooks" method="post">
<label>
<input type="text" name="booksName" class="form-control" placeholder="请输入书籍名称" required>
</label>
<input type="submit" value="查询" class="btn btn-primary">
</form>
</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 href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.getBookID()}">更改</a>
|
<a href="${pageContext.request.contextPath}/book/del/${book.getBookID()}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
1.7 运行测试
标签:String,bookService,--,书籍,查询,ssm,public,books,book 来源: https://blog.csdn.net/I_r_o_n_M_a_n/article/details/116890671