文章目录
一、mybatis层
1.1 任务
- 编写数据库
- 创建Maven项目,项目名为ssmbulid,添加web支持
- 配置pom.xml,导入依赖包以及设置资源过滤
- 建立基本目录
- 数据库配置文件database.properties
- 编写mybatis-config.xml配置文件
- IDEA关联数据库
- 在pojo包下编写实体类Books
- 在dao包下编写mapper接口 以及需要对应的Mapper.xml
- 编写Service层接口和实现类
1.2 具体内容
1.数据库
CREATE DATABASE ssmbuild;
USE ssmbuild;
DROP TABLE IF EXISTS books;
CREATE TABLE books(
bookID INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
bookName VARCHAR(100) NOT NULL COMMENT '书名',
bookCounts INT(11) NOT NULL COMMENT '书量',
detail VARCHAR(200) NOT NULL COMMENT '描述',
KEY bookID (bookID)
)ENGINE = INNODB DEFAULT CHARSET=utf8;
INSERT INTO books(bookID,bookName,bookCounts,detail)VALUES
(1,'java',1,'从入门到放弃'),
(2,'mysql',10,'从删库到跑路'),
(3,'linux',6,'从进门到喝茶');
2.创建Maven项目,项目名为ssmbulid,添加web支持
3.配置pom.xml,导入依赖包以及设置资源过滤
需要用到的依赖有:junit、数据库驱动、数据库连接池、servlet、jsp、mybatis、mybatis-spring、spring,lombok 。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>ssmbulid</artifactId>
<version>1.0-SNAPSHOT</version>
<!--依赖问题-->
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池 c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<!--静态资源导出问题问题-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
4.建立基本目录
- java
- com.palen
- controller
- service
- dao(可改成mapper)
- pojo
- com.palen
- resources
- web
- WEB-INF
- jsp
- WEB-INF
- pom.xml
5.数据库配置文件database.properties
注意:这里useSSL=false!!!useSSL=true是进行安全验证,一般通过证书或者令牌等,useSSL=false就是通过账号密码进行连接,通常使用useSSL=false!!!>
jdbc.driver=com.mysql.jdbc.Driver
#如果使用的mysql8.0以上还需要增加时区设计!!!!!&serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=13543503528
6.mybatis-config.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置数据源,交给spring去做-->
<!--取别名-->
<typeAliases>
<package name="com.palen.pojo"/>
</typeAliases>
<!--绑定mapper-->
<mappers>
<mapper class="com.palen.dao.BookMapper"/>
</mappers>
</configuration>
7.IDEA关联数据库
8.在pojo包下编写实体类Books
package com.palen.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
9.在dao包下编写mapper接口 以及需要对应的Mapper.xml
-
BookMapper接口
package com.palen.dao; import com.palen.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; public interface BookMapper { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById(@Param("bookID") int id); //查询一本书 Books queryBookById(@Param("bookID") int id); //查询所有书 List<Books> queryAllBook(); //更新一本书 int updateBook(Books books); }
-
BookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.palen.dao.BookMapper"> <!--增加一个Book--> <insert id="addBook" parameterType="Books"> insert into ssmbuild.books(bookName,bookCounts,detail) values (#{bookName},#{bookCounts},#{detail}); </insert> <!--根据id删除一个Book--> <delete id="deleteBookById" parameterType="int"> delete from ssmbuild.books where bookID=#{bookID} </delete> <!--更新Book--> <update id="updateBook" parameterType="Books"> update ssmbuild.books set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail} where bookID = #{bookID} </update> <!--根据id查询,返回一个Book--> <select id="queryBookById" resultType="Books"> select * from ssmbuild.books where bookID = #{bookID} </select> <!--查询全部Book--> <select id="queryAllBook" resultType="Books"> SELECT * from ssmbuild.books </select> </mapper>
10.编写Service层接口和实现类
-
BookService接口
package com.palen.service; import com.palen.pojo.Books; import java.util.List; public interface BookService { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById(int id); //查询一本书 Books queryBookById(int id); //查询所有书 List<Books> queryAllBook(); //更新一本书 int updateBook(Books books); }
-
BookServiceImpl实现类
package com.palen.service; import com.palen.dao.BookMapper; import com.palen.pojo.Books; import java.util.List; public class BookServiceImpl implements BookService { private BookMapper bookMapper;//调用dao层 public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } //增加一本书 public int addBook(Books books) { return bookMapper.addBook(books); } //删除一本书 public int deleteBookById(int id) { return bookMapper.deleteBookById(id); } //查询一本书 public Books queryBookById(int id) { return bookMapper.queryBookById(id); } //查询所有书 public List<Books> queryAllBook() { return bookMapper.queryAllBook(); } //更新一本书 public int updateBook(Books books) { return bookMapper.updateBook(books); } }
二、spring层
2.1 任务
- 编写spring-dao.xml ,整合spring-mybatis
- 编写spring-service.xml整合service层
2.2 具体内容
1.编写spring-dao.xml ,整合spring-mybatis
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis -->
<!-- 1.关联数据库文件 -->
<context:property-placeholder location="classpath:database.properties"/>
<!-- 2.数据库连接池 -->
<!--数据库连接池
dbcp 半自动化操作 不能自动连接
c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBatis全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.palen.dao"/>
</bean>
</beans>
2.编写spring-service.xml整合service层
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1. 扫描service相关的bean -->
<context:component-scan base-package="com.palen.service" />
<!-- 2. BookServiceImpl注入到IOC容器中-->
<!-- 此处还可以通过注解直接实现注入 -->
<bean id="BookServiceImpl" class="com.palen.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!-- 3. 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 4. aop事务支持(待定) -->
</beans>
三、springMVC层
3.1 任务
- 编写web.xml
- 编写spring-mvc.xml
- 编写applicationContext.xml文件,整合所有配置文件
- 编写controller类以及jsp页面
3.2 具体内容
1.编写web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--DispatcherServlet-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了,不是单纯的spring-mvc.xml!-->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--encodingFilter-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
2.编写spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1.开启SpringMVC注解驱动 -->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />-->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.palen.controller" />
</beans>
3.编写applicationContext.xml文件,整合所有配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
</beans>
4.编写controller类以及jsp页面
实现功能:增删查改
-
查询所有书(详情看3.3小节)
-
增加一本书(详情看3.4小节)
-
删除一本书(详情看3.5小节)
-
修改一本书(详情看3.6小节)
总BookController
package com.palen.controller;
import com.palen.pojo.Books;
import com.palen.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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class BookController {
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
//查询全部书籍
@RequestMapping("/allBook")
public String list(Model model){
List<Books> list = bookService.queryAllBook();
model.addAttribute("list",list);
return "allBook";//allBook页面显示所有查询到的书
}
//跳转到增加书籍页面
@RequestMapping("/toAddBook")
public String toAddPage(){
return "addBook";
}
//添加书籍请求
@RequestMapping("/addBook")
public String addBook(Books books){
//注意前端的form表单中的名字必须和Books的属性名称一致
System.out.println("addBook=>"+books);
bookService.addBook(books);
return "redirect:/allBook";//重定向查询书籍页面
}
//跳转到修改页面
@RequestMapping("/toUpdateBook")
public String toUpdate(int id,Model model){
Books books = bookService.queryBookById(id);
model.addAttribute("book",books);
return "updateBook";
}
//修改书籍
@RequestMapping("/updateBook")
public String updateBook(Books book){
System.out.println("book=>"+book);
int i = bookService.updateBook(book);
if (i>0){
System.out.println("添加成功!");
}
return "redirect:/allBook";
}
//删除书籍
@RequestMapping("/del/{bookId}")
public String del(@PathVariable("bookId") int id){
int i = bookService.deleteBookById(id);
return "redirect:/allBook";
}
//模糊查询某本书
@RequestMapping("/searchBook")
public String searchBook(String bookName,Model model){
List<Books> list = bookService.queryBookByName(bookName);
model.addAttribute("list",list);
System.out.println(list);
return "allBook";
}
}
3.3 查询所有书
index.jsp页面发送请求“/allBook” -> BookController响应请求 -> allBook.jsp 展示书籍页面
3.3.1 index.jsp
<body>
<h3>
<a href="${pageContext.request.contextPath}/allBook">点击进入列表页</a>
</h3>
</body>
3.3.2 BookController
//查询全部书籍
@RequestMapping("/allBook")
public String list(Model model){
List<Books> list = bookService.queryAllBook();
model.addAttribute("list",list);
return "allBook";//allBook页面显示所有查询到的书
}
3.3.3 allBook.jsp
<table class="table table-hover table-striped">
<thead>
<tr>
<th>书籍编号</th>
<th>书籍名字</th>
<th>书籍数量</th>
<th>书籍详情</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${requestScope.get('list')}">
<tr>
<td>${book.getBookID()}</td>
<td>${book.getBookName()}</td>
<td>${book.getBookCounts()}</td>
<td>${book.getDetail()}</td>
</tr>
</c:forEach>
</tbody>
</table>
3.4 增加一本书
allBook.jsp发送请求“/toAddBook” -> BookController响应跳转请求 -> addBook.jsp发送请求"/addBook" -> BookController响应请求,并重定向 -> allBook.jsp展示所有书籍
3.4.1 allBook.jsp
<div class="row">
<div class="col-md-4 column">
<a href="${pageContext.request.contextPath}/toAddBook">新增书籍</a>
</div>
</div>
3.4.2 BookController
//跳转到增加书籍页面
@RequestMapping("/toAddBook")
public String toAddPage(){
return "addBook";
}
3.4.3 addBook.jsp
<form action="${pageContext.request.contextPath}/addBook" method="post">
书籍名称:<input type="text" name="bookName" required><br><br><br>
书籍数量:<input type="text" name="bookCounts" required><br><br><br>
书籍详情:<input type="text" name="detail"required><br><br><br>
<input type="submit" value="添加">
</form>
3.4.4 BookController
//添加书籍请求
@RequestMapping("/addBook")
public String addBook(Books books){
//注意前端的form表单中的名字必须和Books的属性名称一致
System.out.println("addBook=>"+books);
bookService.addBook(books);
return "redirect:/allBook";//重定向查询书籍页面
}
3.4.5 allBook.jsp
<table class="table table-hover table-striped">
<thead>
<tr>
<th>书籍编号</th>
<th>书籍名字</th>
<th>书籍数量</th>
<th>书籍详情</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${requestScope.get('list')}">
<tr>
<td>${book.getBookID()}</td>
<td>${book.getBookName()}</td>
<td>${book.getBookCounts()}</td>
<td>${book.getDetail()}</td>
</tr>
</c:forEach>
</tbody>
</table>
3.5 删除一本书
allBook.jsp发送请求“/del/{bookId}” -> BookController响应请求 -> allBook.jsp
3.5.1 allBook.jsp
<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('list')}">
<tr>
<td>${book.getBookID()}</td>
<td>${book.getBookName()}</td>
<td>${book.getBookCounts()}</td>
<td>${book.getDetail()}</td>
<td>
<a href="${pageContext.request.contextPath}/del/${book.bookID}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
3.5.2 BookController
//删除书籍
@RequestMapping("/del/{bookId}")
public String del(@PathVariable("bookId") int id){
int i = bookService.deleteBookById(id);
return "redirect:/allBook";
}
3.5.3 allBook.jsp
(同3.5.1)
3.6 修改一本书
allBook.jsp发送请求“/toUpdateBook” -> BookController响应跳转请求 -> updateBook.jsp发送请求"/updateBook" -> BookController响应请求,并重定向 -> allBook.jsp展示所有书籍
3.6.1 allBook.jsp
<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('list')}">
<tr>
<td>${book.getBookID()}</td>
<td>${book.getBookName()}</td>
<td>${book.getBookCounts()}</td>
<td>${book.getDetail()}</td>
<td>
<a href="${pageContext.request.contextPath}/toUpdateBook?id=${book.getBookID()}">修改</a> |
<a href="${pageContext.request.contextPath}/del/${book.bookID}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
3.6.2 BookController
//跳转到修改页面
@RequestMapping("/toUpdateBook")
public String toUpdate(int id,Model model){
Books books = bookService.queryBookById(id);
model.addAttribute("book",books);
return "updateBook";
}
3.6.3 updateBook.jsp
<form action="${pageContext.request.contextPath}/updateBook" method="post">
<input type="hidden" name="bookID" value="${book.bookID}">
书籍名称:<input type="text" name="bookName" required value="${book.bookName}"><br><br><br>
书籍数量:<input type="text" name="bookCounts" required value="${book.bookCounts}"><br><br><br>
书籍详情:<input type="text" name="detail" required value="${book.detail}"><br><br><br>
<input type="submit" value="修改">
</form>
3.6.4 BookController
//修改书籍
@RequestMapping("/updateBook")
public String updateBook(Books book){
System.out.println("book=>"+book);
int i = bookService.updateBook(book);
if (i>0){
System.out.println("添加成功!");
}
return "redirect:/allBook";
}
3.6.5 allBook.jsp
(同3.6.1)
3.7 模糊查询书(增加功能)
- allBook.jsp发送请求“/searchBook” -> BookController响应请求,并重定向 -> allBook.jsp展示所有书籍
- 此功能为之前底层没有涉及到的,故写好allBook.jsp -> dao层 -> service层 -> controller层
3.7.1 allBook.jsp
<form action="${pageContext.request.contextPath}/searchBook" method="get" class="form-inline">
<input type="text" class="form-control" name="bookName" placeholder="请输入搜索书籍名称">
<input type="submit" value="查询">
</form>
3.7.2 dao层
BookMapper接口
//模糊查询某本书
List<Books> queryBookByName(String bookName);
BookMapper.xml
<!--模糊查询book-->
<select id="queryBookByName" resultType="Books" parameterType="String">
SELECT * from ssmbuild.books where bookName like concat('%',#{bookName},'%') ;
</select>
3.7.3 service层
BookService接口
//模糊查询某本书
List<Books> queryBookByName(String bookName);
BookServiceImpl
//模糊查询某本书
public List<Books> queryBookByName(String bookName) {
return bookMapper.queryBookByName(bookName);
}
3.7.4 BookController
//模糊查询某本书
@RequestMapping("/searchBook")
public String searchBook(String bookName,Model model){
List<Books> list = bookService.queryBookByName(bookName);
model.addAttribute("list",list);
System.out.println(list);
return "allBook";
}