1. 配置pom.xml
导入依赖:Junit、数据库驱动、数据库连接池、Servlet-JSP、MyBatis、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> com.acai</ groupId>
< artifactId> SSM</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< properties>
< maven.compiler.source> 8</ maven.compiler.source>
< maven.compiler.target> 8</ maven.compiler.target>
</ properties>
< dependencies>
< 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>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.2.6</ version>
</ dependency>
< 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>
< 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>
< 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>
< dependency>
< groupId> org.aspectj</ groupId>
< artifactId> aspectjweaver</ artifactId>
< version> 1.8.3</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.18.20</ 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>
2. 连接数据库
(1) 连接MySQL
(2) 创建database、table
create database if not exists ssm;
use ssm;
create table if not exists book_info
(
book_id int(10) primary key auto_increment,
book_name varchar(100) not null,
book_count int(10) not null
) character set utf8;
3. 建立目录结构
src/main/java/com/acai/pojo
src/main/java/com/acai/dao
src/main/java/com/acai/service
src/main/java/com/acai/controller
web/WEB-INF/page
4. 创建配置文件
(1) 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>
< settings>
< setting name = " logImpl" value = " STDOUT_LOGGING" />
</ settings>
< typeAliases>
< package name = " com.acai.pojo" />
</ typeAliases>
</ configuration>
(2) 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
https://www.springframework.org/schema/beans/spring-beans.xsd" >
< import resource = " classpath:spring-dao.xml" />
< import resource = " classpath:spring-service.xml" />
< import resource = " classpath:spring-mvc.xml" />
</ beans>
(3) database.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=false&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
(4) spring-dao.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: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd" >
< context: property-placeholder location = " classpath:database.properties" />
< bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource" >
< property name = " driverClassName" value = " ${jdbc.driverClassName}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " configLocation" value = " classpath:mybatis-config.xml" />
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " sqlSessionFactoryBeanName" value = " sqlSessionFactory" />
< property name = " basePackage" value = " com.acai.dao" />
</ bean>
</ beans>
(5) spring-service.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: context= " http://www.springframework.org/schema/context"
xmlns: tx= " http://www.springframework.org/schema/tx"
xmlns: aop= " http://www.springframework.org/schema/aop"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd" >
< context: component-scan base-package = " com.acai.service" />
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< tx: advice id = " txAdvice" transaction-manager = " transactionManager" >
< tx: attributes>
< tx: method name = " *" propagation = " REQUIRED" />
</ tx: attributes>
</ tx: advice>
< aop: config>
< aop: pointcut id = " txPointCut" expression = " execution(* com.acai.dao.*.*(..))" />
< aop: advisor advice-ref = " txAdvice" pointcut-ref = " txPointCut" />
</ aop: config>
</ beans>
(6) 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
https://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" >
< mvc: annotation-driven/>
< mvc: default-servlet-handler/>
< context: component-scan base-package = " com.acai.controller" />
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /WEB-INF/page/" />
< property name = " suffix" value = " .jsp" />
</ bean>
</ beans>
5. 配置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" >
< servlet>
< servlet-name> springmvc</ servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< init-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:applicationContext.xml</ param-value>
</ init-param>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> springmvc</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
< 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-config>
< session-timeout> 15</ session-timeout>
</ session-config>
</ web-app>
6. 编写xxx类
package com. acai. pojo ;
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
private Integer id;
private String name;
private Integer count;
}
7. 编写xxxMapper接口
package com. acai. dao ;
import com. acai. pojo. Book ;
import org. apache. ibatis. annotations. Param ;
import java. util. List ;
public interface BookMapper {
int addBook ( Book book) ;
int deleteBookById ( @Param ( "bookId" ) int id) ;
int updateBook ( Book book) ;
Book queryBookById ( @Param ( "bookId" ) int id) ;
List < Book > queryBookByName ( @Param ( "bookName" ) String name) ;
List < Book > queryAllBook ( ) ;
}
8. 编写xxxMapper.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.acai.dao.BookMapper" >
< resultMap id = " BookMap" type = " Book" >
< id column = " book_id" property = " id" />
< id column = " book_name" property = " name" />
< id column = " book_count" property = " count" />
</ resultMap>
< insert id = " addBook" parameterType = " Book" >
insert into ssm.book_info (book_name, book_count)
values (#{name}, #{count})
</ insert>
< delete id = " deleteBookById" parameterType = " int" >
delete
from ssm.book_info
where book_id = #{bookId}
</ delete>
< update id = " updateBook" parameterType = " Book" >
update ssm.book_info
set book_name=#{name},
book_count=#{count}
where book_id = #{id}
</ update>
< select id = " queryBookById" resultMap = " BookMap" >
select *
from ssm.book_info
where book_id = #{bookId}
</ select>
< select id = " queryBookByName" resultMap = " BookMap" >
select *
from ssm.book_info
where book_name = #{bookName}
</ select>
< select id = " queryAllBook" resultMap = " BookMap" >
select *
from ssm.book_info
</ select>
</ mapper>
9. 绑定xxxMapper.xml
< mappers>
< mapper resource = " com/acai/dao/BookMapper.xml" />
</ mappers>
10. 编写xxxService接口
package com. acai. service ;
import com. acai. pojo. Book ;
import java. util. List ;
public interface BookService {
int addBook ( Book book) ;
int deleteBookById ( int id) ;
int updateBook ( Book book) ;
Book queryBookById ( int id) ;
List < Book > queryBookByName ( String name) ;
List < Book > queryAllBook ( ) ;
}
11. 编写xxxServiceImpl类
package com. acai. service ;
import com. acai. dao. BookMapper ;
import com. acai. pojo. Book ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import java. util. List ;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
public void setBookMapper ( BookMapper bookMapper) {
this . bookMapper = bookMapper;
}
@Override
public int addBook ( Book book) {
return bookMapper. addBook ( book) ;
}
@Override
public int deleteBookById ( int id) {
return bookMapper. deleteBookById ( id) ;
}
@Override
public int updateBook ( Book book) {
return bookMapper. updateBook ( book) ;
}
@Override
public Book queryBookById ( int id) {
return bookMapper. queryBookById ( id) ;
}
@Override
public List < Book > queryBookByName ( String name) {
return bookMapper. queryBookByName ( name) ;
}
@Override
public List < Book > queryAllBook ( ) {
return bookMapper. queryAllBook ( ) ;
}
}
12. 注入xxxServiceImpl类
在spring-service.xml注入到spring容器中
< bean id = " bookService" class = " com.acai.service.BookServiceImpl" >
< property name = " bookMapper" ref = " bookMapper" />
</ bean>
13. 编写xxxController类
package com. acai. controller ;
import com. acai. pojo. Book ;
import com. acai. 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 ( "bookService" )
private BookService bookService;
@RequestMapping ( "/toAddBook" )
public String toAddBook ( ) {
return "add_book" ;
}
@RequestMapping ( "/addBook" )
public String addBook ( Book book) {
bookService. addBook ( book) ;
return "redirect:/book/queryAllBook" ;
}
@RequestMapping ( "/deleteBook" )
public String deleteBook ( int id) {
bookService. deleteBookById ( id) ;
return "redirect:/book/queryAllBook" ;
}
@RequestMapping ( "/toUpdateBook" )
public String toUpdateBook ( int id, Model model) {
Book book = bookService. queryBookById ( id) ;
model. addAttribute ( "book" , book) ;
return "update_book" ;
}
@RequestMapping ( "/updateBook" )
public String updateBook ( Book book) {
bookService. updateBook ( book) ;
return "redirect:/book/queryAllBook" ;
}
@RequestMapping ( "/queryBookByName" )
public String queryBookByName ( String name, Model model) {
List < Book > books = bookService. queryBookByName ( name) ;
if ( books. size ( ) != 0 ) {
model. addAttribute ( "allBook" , books) ;
return "show_book" ;
} else {
return "redirect:/book/queryAllBook" ;
}
}
@RequestMapping ( "/queryAllBook" )
public String queryAllBook ( Model model) {
List < Book > books = bookService. queryAllBook ( ) ;
model. addAttribute ( "allBook" , books) ;
return "show_book" ;
}
}
14. 编写JSP页面
(1) show_book.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: acai
Date: 2021/8/16
Time: 11:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head>
< title> 图书管理系统</ title>
</ head>
< body>
< div align = " center" >
< h1>
图书列表
</ h1>
< table style = " width : 650px; height : 500px; text-align : center" border = " 1" >
< tr>
< td colspan = " 5" align = " right" >
< form action = " ${pageContext.request.contextPath}/book/queryBookByName" method = " post" >
< a href = " ${pageContext.request.contextPath}/book/toAddBook" > 新增图书</ a>
< input type = " text" name = " name" >
< input type = " submit" value = " 查询" >
</ form>
</ td>
</ tr>
< tr>
< th colspan = " 2" align = " left" >
名称
</ th>
< th align = " left" >
数量
</ th>
< th colspan = " 2" >
操作
</ th>
</ tr>
< c: forEach var = " book" items = " ${allBook}" >
< tr>
< td colspan = " 2" align = " left" >
${book.name}
</ td>
< td align = " left" >
${book.count}
</ td>
< td colspan = " 2" >
< a href = " ${pageContext.request.contextPath}/book/toUpdateBook?id=${book.id}" > 修改</ a>
< a href = " ${pageContext.request.contextPath}/book/deleteBook?id=${book.id}" > 删除</ a>
</ td>
</ tr>
</ c: forEach>
</ div>
</ body>
</ html>
(2) add_book.jsp
<%--
Created by IntelliJ IDEA.
User: acai
Date: 2021/8/17
Time: 15:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head>
< title> 图书管理系统</ title>
</ head>
< body>
< div align = " center" >
< h2>
新增图书
</ h2>
< form action = " ${pageContext.request.contextPath}/book/addBook" method = " post" >
< table style = " width : 300px; height : 200px; text-align : center" border = " 1" >
< tr>
< td>
名称
</ td>
< td>
< input type = " text" name = " name" required = " true" >
</ td>
</ tr>
< tr>
< td>
数量
</ td>
< td>
< input type = " text" name = " count" required = " true" >
</ td>
</ tr>
< tr>
< td colspan = " 2" >
< input type = " submit" value = " 提交" >
< button onclick = " window.location.href=' ${pageContext.request.contextPath}/book/queryAllBook' " >
返回
</ button>
</ td>
</ tr>
</ table>
</ form>
</ div>
</ body>
</ html>
(3) update_book.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
Created by IntelliJ IDEA.
User: acai
Date: 2021/8/17
Time: 15:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head>
< title> 图书管理系统</ title>
</ head>
< body>
< div align = " center" >
< h2>
修改图书
</ h2>
< form action = " ${pageContext.request.contextPath}/book/updateBook" method = " post" >
< input type = " hidden" name = " id" value = " ${book.id}" >
< table style = " width : 300px; height : 200px; text-align : center" border = " 1" >
< tr>
< td>
名称
</ td>
< td>
< input type = " text" name = " name" value = " ${book.name}" required = " true" >
</ td>
</ tr>
< tr>
< td>
数量
</ td>
< td>
< input type = " text" name = " count" value = " ${book.count}" required = " true" >
</ td>
</ tr>
< tr>
< td colspan = " 2" >
< input type = " submit" value = " 提交" >
< button onclick = " window.location.href=' ${pageContext.request.contextPath}/book/queryAllBook' " >
返回
</ button>
</ td>
</ tr>
</ table>
</ form>
</ div>
</ body>
</ html>