首先编写对应的SQL,要完成的功能
1、在Mapper接口里对应返回值类型
Books queryBookByName(String bookName);
2、对应Mapper里编写SQL
<select id="queryBookByName" resultType="Books"> select * from ssmbuild.books where bookName like CONCAT('%',#{bookName},'%') </select>
3、编写Service层接口(和dao里的接口一样的)
Books queryBookByName(String bookName);
4、接口实现类
public Books queryBookByName(String bookName) { return bookMapper.queryBookByName(bookName); }
5、对应页面添加表单
<div class="col-md-4 column">
<form class="form-inline" action="${pageContext.request.contextPath}/query" method="post">
<span style="color: red; font-weight: bold" >${error}</span>
<input type="text" name="queryBookName" placeholder="请输入要查询的书籍名称" required>
<input type="submit" value="查询">
</form>
</div>
6、对应Controller /query
@RequestMapping("/query")
public String query(String queryBookName,Model model){
Books book=bookService.queryBookByName(queryBookName);
if (book==null){
model.addAttribute("error","未找到");
List<Books> books=bookService.queryAllBook();
model.addAttribute("list",books);
return "allBook";
}
List<Books> books=new ArrayList<Books>();
books.add(book);
model.addAttribute("list",books);
return "allBook";
}