SSM框架的学习,跟着狂神做了一个SSM的项目,也是一个小的框架.
1.准备工作
(1)数据库的准备
创建一个名为ssmbuild的数据库,在库中建一个名为books的表,表的内容如下:
(2)创建一个空的maven项目,添加WebAPP文件
(3)在pom.xml中添加依赖以及静态资源到处问题
<!--依赖-->
<dependencies>
<!-- 连接数据库依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- Mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- JSP依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- JSP依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- JSTL表达式依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!-- mybatis与Spring整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- Spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
</dependencies>
<!--静态资源文件导出-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
(4)IDEA连接数据库
(5)按照项目格式创建文件以及配置文件等
<!--ApplicationContext.xml配置文件-->
<?xml version="1.0" encoding="UTF8"?>
<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">
</beans>
<!--Mybatis-Config配置文件-->
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
<!--database.properties文件-->
jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
jdbc.username = root
jdbc.password = 123456
(6).创建lib包
因为创建的是空的Maven项目,需要手动创建lib包,将项目所需的所有jar包放入其中.具体如下:
2.编写MyBatis层
(1)根据数据库中的表,在pojo包中创建Books实体类
package com.Sky.Pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//这里使用注解自动添加setget方法和无参有参构造,toString等一些方法.
//可手动添加
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
(2)在Mapper包下,创建BookMapper接口以及BookMapper.xml文件来对数据库进行操作
package com.Sky.Mapper;
import com.Sky.Pojo.Books;
import java.util.List;
public interface BookMapper {
int InsertBook(Books books);
int DeleteBook(int Id);
int UpDateBook(Books books);
Books SelectBook(int Id);
List<Books> SelectAllBooks();
List<Books> SelectBookName(String bookName);
}
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.Sky.Mapper.BookMapper">
<insert id="InsertBook" parameterType="com.Sky.Pojo.Books">
insert into books(bookName,bookCounts,detail) values (#{bookName},#{bookCounts},#{detail})
</insert>
<delete id="DeleteBook" parameterType="int">
delete from books where bookID = #{Id}
</delete>
<update id="UpDateBook" parameterType="com.Sky.Pojo.Books">
update books set bookName = #{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookID = #{bookID}
</update>
<select id="SelectBook" resultType="com.Sky.Pojo.Books">
select * from books where bookID = #{Id}
</select>
<select id="SelectAllBooks" resultType="com.Sky.Pojo.Books">
select * from books
</select>
<select id="SelectBookName" resultType="com.Sky.Pojo.Books">
select * from books where bookName = #{bookName}
</select>
</mapper>
(3)在MyBatis配置文件中,添加BookMapper的映射
<mappers>
<mapper class="com.Sky.Mapper.BookMapper"/>
</mappers>
(4)在Service层中,编写调用Mapper层的接口以及实现类
package com.Sky.Srevice;
import com.Sky.Pojo.Books;
import java.util.List;
public interface BookService {
int InsertBook(Books books);
int DeleteBook(int Id);
int UpDateBook(Books books);
Books SelectBook(int Id);
List<Books> SelectAllBooks();
List<Books> SelectBookName(String bookName);
}
package com.Sky.Srevice;
import com.Sky.Mapper.BookMapper;
import com.Sky.Pojo.Books;
import java.util.List;
public class BookServiceImp implements BookService{
private BookMapper Mapper;
public void setMapper(BookMapper mapper) {
Mapper = mapper;
}
public int InsertBook(Books books) {
return Mapper.InsertBook(books);
}
public int DeleteBook(int Id) {
return Mapper.DeleteBook(Id);
}
public int UpDateBook(Books books) {
return Mapper.UpDateBook(books);
}
public Books SelectBook(int Id) {
return Mapper.SelectBook(Id);
}
public List<Books> SelectAllBooks() {
return Mapper.SelectAllBooks();
}
public List<Books> SelectBookName(String bookName) {
return Mapper.SelectBookName(bookName);
}
}
3.编写Spring层
(1)整合Spring和Mapper层
<?xml version="1.0" encoding="UTF8"?>
<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">
<!--关联数据库配置文件database.properties-->
<context:property-placeholder location="classpath:database.properties"/>
<!--数据库连接池,c3p0第三方的配置,也可使用Spring自带的-->
<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}"/>
</bean>
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name= "dataSource" ref="dataSource"/>
<!-- 绑定MyBatis配置文件 -->
<property name="configLocation" value="classpath:MyBatis-Config.xml"/>
</bean>
<!-- 配置DAO接口扫描包,动态的实现DAO接口注入到Spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入SqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 要扫描的包 -->
<property name="basePackage" value="com.Sky.Mapper"/>
</bean>
</beans>
(2)整合Spring和Service层
<?xml version="1.0" encoding="UTF8"?>
<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">
<!-- 扫描Service下的包 -->
<context:component-scan base-package="com.Sky.Service"/>
<!-- 将Service下的类注入到Spring中,也可使用注解 -->
<bean id="BookServiceImp" class="com.Sky.Service.BookServiceImp">
<property name="mapper" ref="bookMapper"/>
</bean>
<!-- 声明事务类 -->
<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
4.编写SpringMVC层
(1).配置Web.xml
<!--DispatcherServlet-->
<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-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
(2).配置Spring-MVC.xml
<?xml version="1.0" encoding="UTF8"?>
<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">
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 静态资源过滤 -->
<mvc:default-servlet-handler/>
<!-- 扫描包 -->
<context:component-scan base-package="com.Sky.Controller"/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀,一般为文件路径 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
(3)整合所有Spring文件到ApplicationContext.xml
<?xml version="1.0" encoding="UTF8"?>
<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-Services.xml"/>
<import resource="Spring-Mapper.xml"/>
<import resource="Spring-MVC.xml"/>
</beans>
5.添加书籍增删改查功能与对应的前端页面
(1)controller层
package com.Sky.Controller;
import com.Sky.Pojo.Books;
import com.Sky.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
@RequestMapping("/book")
public class BookController {
@Autowired
@Qualifier("BookServiceImp")
private BookService bookService;
@RequestMapping("/AllBooks")
public String BooksList(Model model){
List<Books> books = bookService.SelectAllBooks();
model.addAttribute("books",books);
return "AllBooks";
}
@RequestMapping("/AddBooksPage")
public String AddBooksPage(){
return "AddBooksPage";
}
@RequestMapping("/AddBook")
public String AddBook(Books book){
bookService.InsertBook(book);
return "redirect:/book/AllBooks";
}
@RequestMapping("/UpDateBookPage/{bookID}")
public String UpDateBookPage(@PathVariable("bookID") int id, Model model){
Books books = bookService.SelectBook(id);
model.addAttribute("UpDate",books);
return "UpDateBookPage";
}
@RequestMapping("/UpDateBook")
public String UpDateBook(Books books){
bookService.UpDateBook(books);
return "redirect:/book/AllBooks";
}
@RequestMapping("/DeleteBook/{bookID}")
public String DeleteBook(@PathVariable("bookID") int id){
bookService.DeleteBook(id);
return "redirect:/book/AllBooks";
}
@RequestMapping("/SelectPage")
public String SelectBookByName(String SelectBookName,Model model){
List<Books> books = bookService.SelectBookName(SelectBookName);
/*System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
for (Books book : books) {
System.out.println(book);
}*/
model.addAttribute("books",books);
return "SelectPage";
}
}
(2).初始化页面,index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
<style>
a{
text-decoration: none;/*文本描述,消除超链接下划线*/
color: black;
font-size: 18px;
}
h3{
width: 100px;
height: 38px;
margin: 100px auto;/*设置上下左右的外边距*/
text-align: center;
line-height: 38px;
background: aqua;
border-radius: 25px ;/*圆角边框*/
}
</style>
</head>
<body>
<h3>
<a href="${pageContext.request.contextPath}/book/AllBooks">书籍展示</a>
</h3>
</body>
</html>
(3)AllBooks.jsp,查询所有书籍页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false"%>
<html>
<head>
<title>书籍展示</title>
<style>
h1{
text-align: center;
}
</style>
<!-- 引入 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>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/book/AddBooksPage">新增</a>
</div>
<div class="col-md-4 column"></div>
<div class="col-md-4 column">
<form action="${pageContext.request.contextPath}/book/SelectPage" method="post" style="float: right" class="form-inline">
<input type="text" name="SelectBookName" class="form-control" placeholder="请输入要查询的书籍名">
<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>
<tr>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${requestScope.get('books')}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
<td>
<a href="${pageContext.request.contextPath}/book/UpDateBookPage/${book.bookID}">修改</a>
|
<a href="${pageContext.request.contextPath}/book/DeleteBook/${book.bookID}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</body>
</html>
(4)AddBooksPage.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>
<form action="${pageContext.request.contextPath}/book/AddBook" method="post">
<div class="form-group">
<label>书籍名称* :</label>
<input type="text" class="form-control" name="bookName">
</div>
<div class="form-group">
<label>书籍数量* :</label>
<input type="text" class="form-control" name="bookCounts" required>
</div>
<div class="form-group">
<label>书籍描述* :</label>
<input type="text" class="form-control" name="detail" required>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="添加" required>
</div>
</form>
</div>
</body>
</html>
(5)UpDateBookPage.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>
<form action="${pageContext.request.contextPath}/book/UpDateBook" method="post">
<input type="hidden" name="bookID" value="${UpDate.bookID}">
<div class="form-group">
<label>书籍名称* :</label>
<input type="text" class="form-control" name="bookName" value="${UpDate.bookName}">
</div>
<div class="form-group">
<label>书籍数量* :</label>
<input type="text" class="form-control" name="bookCounts" value="${UpDate.bookCounts}" required>
</div>
<div class="form-group">
<label>书籍描述* :</label>
<input type="text" class="form-control" name="detail" value="${UpDate.detail}" required>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="修改" required>
</div>
</form>
</div>
</body>
</html>
(6).SelectPage.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>
<style>
h1{
text-align: center;
}
</style>
<!-- 引入 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>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/book/AddBooksPage">新增</a>
</div>
<div class="col-md-4 column"></div>
<div class="col-md-4 column">
<form action="${pageContext.request.contextPath}/book/SelectPage" method="post" style="float: right" class="form-inline">
<input type="text" name="SelectBookName" class="form-control" placeholder="请输入要查询的书籍名">
<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>
<tr>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${requestScope.get('books')}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
<td>
<a href="${pageContext.request.contextPath}/book/UpDateBookPage/${book.bookID}">修改</a>
|
<a href="${pageContext.request.contextPath}/book/DeleteBook/${book.bookID}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</body>
</html>
将一切都配置好后,启动TomCat即可运行.
在过程中遇到的一部分BUG和其解决方式如下链接
BUG合集