allBook.jsp
<form action="${pageContext.request.contextPath}/book/queryBook" method="post" style="float: right" class="form-inline">
<span style="color: red;font-weight: bold">${error}</span>
<input type="text" name="bookName" class="form-control" placeholder="请输入要查询的书籍名称">
<input type="submit" value="查询" class="btn btn-primary">
</form>
Controller
//通过名字查询书籍
@RequestMapping("/queryBook")
public String queryBook(String bookName,Model model){
Books books = booksService.queryBookByName(bookName);
List<Books> list =new ArrayList<>();
list.add(books);
if (books==null){
list=booksService.findAllBooks();
model.addAttribute("error","没有查询到结果");
}
model.addAttribute("list",list);
return "allBook";
}
mapper
//bookName这个形参对应着Mapper.xml文件里的#{形参名字} 若名字不对应的话,可以加注解@Param("bookName") bookName与 #{bookName} 对应;
Books queryBookByName(@Param("bookName") String bookName);
mapper.xml
<select id="queryBookByName" resultType="Books">
select * from `ssmbuild`.`books` where bookName = #{bookName};
</select>
Service
//根据名字查询书籍
Books queryBookByName(String bookName);
Impl
@Override
public Books queryBookByName(String bookName) {
return booksMapper.queryBookByName(bookName);
}
做到这,SSM整合项目算是基本结束了
成品展示