文章目錄 概述 準備 實現 1.搭建數據庫 2.實現pojo層 3.實現dao層 4.實現service層 5.配置Spring整合dao層 6.配置Spring整合service層 7.配置Spring整合Spring MVC 8.配置Spring整合文件 9.實現controller層 10.實現view層 11.運行前檢查 運行 后記
概述
通過對數據庫中一張表的CRUD,將相應的操作結果渲染到頁面上。 如果想查看Spring Boot架構的同等規模的項目,可參考此篇博客Spring Boot項目實例——簡易版商城管理系統 由于廣大讀者要求源碼,作者通過這篇博客還原了項目(當然有一些隱藏的坑),然后將該項目上傳到了Gitee,在末尾的后記部分有源碼地址,讀者可參考。
準備
環境:
IDEA MySQL 5.7.19 Tomcat 9 Maven 3.6 要求:
掌握MySQL數據庫 掌握Spring, 掌握SpringMVC 掌握MyBatis知識 掌握簡單的前端知識
實現
1.搭建數據庫
創建一個存放書籍數據的數據庫(ssmbuild),并在該數據庫中建立一個名為books的表,代碼如下:
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' , 5 , '從進門到進牢' ) ;
寫好數據庫的配置文件database.properties,該文件可以放在resources目錄下,代碼如下:
jdbc
. driver
= com
. mysql
. jdbc
. Driver
jdbc
. url
= jdbc:mysql:
jdbc
. username
= root
jdbc
. password
= 123456
在Maven中導入數據庫連接所需要的驅動包(mysql-connector),代碼如下:
< dependency> < groupId> mysql
</ groupId> < artifactId> mysql-connector-java
</ artifactId> < version> 5.1.47
</ version> </ dependency>
2.實現pojo層
在項目的java目錄下新建pojo包,因為數據庫中只有一個表,所以只需要編寫一個實體類Book即可,代碼如下所示:
package pojo
; public class Book { private int bookID
; private String bookName
; private int bookCounts
; private String detail
; public Book ( ) { } public Book ( int bookID
, String bookName
, int bookCounts
, String detail
) { this . bookID
= bookID
; this . bookName
= bookName
; this . bookCounts
= bookCounts
; this . detail
= detail
; } public int getBookID ( ) { return bookID
; } public void setBookID ( int bookID
) { this . bookID
= bookID
; } public String
getBookName ( ) { return bookName
; } public void setBookName ( String bookName
) { this . bookName
= bookName
; } public int getBookCounts ( ) { return bookCounts
; } public void setBookCounts ( int bookCounts
) { this . bookCounts
= bookCounts
; } public String
getDetail ( ) { return detail
; } public void setDetail ( String detail
) { this . detail
= detail
; } @Override public String
toString ( ) { return "Book{" + "bookID=" + bookID
+ ", bookName='" + bookName
+ '\'' + ", bookCounts=" + bookCounts
+ ", detail='" + detail
+ '\'' + '}' ; }
}
3.實現dao層
在項目的java目錄下新建dao包,編寫dao層接口BookDao即可,代碼如下所示:
package dao
; import org
. apache
. ibatis
. annotations
. Param
;
import pojo
. Book
; import java
. util
. List
; public interface BookDao { int addBook ( Book books
) ; int deleteBookByID ( @Param ( "bookID" ) int id
) ; int updateBook ( Book books
) ; Book
queryBookByID ( @Param ( "bookID" ) int id
) ; List
< Book> queryAllBook ( ) ; }
在Maven中導入Mybatis所需要的驅動包(mybatis),代碼如下:
< dependency> < groupId> org.mybatis
</ groupId> < artifactId> mybatis
</ artifactId> < version> 3.4.6
</ version> </ dependency>
在dao包下編寫對應的映射文件BookMapper.xml,代碼如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> < mapper namespace = " dao.BookDao" > < insert id = " addBook" parameterType = " Book" > insert into ssmbuild.books(bookID , bookName, bookCounts, detail)values (#{bookID},#{bookName},#{bookCounts},#{detail})
</ insert> < delete id = " deleteBookByID" parameterType = " int" > delete from ssmbuild.books where bookID = #{bookID}
</ delete> < update id = " updateBook" parameterType = " Book" > update ssmbuild.booksset bookName = #{bookName} , bookCounts = #{bookCounts} ,detail = #{detail}where bookID = #{bookID}
</ update> < select id = " queryBookByID" resultType = " Book" > select * from ssmbuild.books where bookID = #{bookID}
</ select> < select id = " queryAllBook" resultType = " Book" > select * from ssmbuild.books
</ select> </ mapper>
在resources目錄下編寫Mybatis的配置文件Mybatis-config.xml,用于配置別名,以及注冊映射文件,代碼如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
< configuration> < typeAliases> < package name = " pojo" /> </ typeAliases> < mappers> < mapper resource = " dao/BookMapper.xml" > </ mapper> </ mappers> </ configuration>
4.實現service層
在項目的java目錄下新建service包,編寫service層接口BookService,代碼如下所示:
package service
; import org
. apache
. ibatis
. annotations
. Param
;
import pojo
. Book
; import java
. util
. List
; public interface BookService { int addBook ( Book books
) ; int deleteBookByID ( @Param ( "bookID" ) int id
) ; int updateBook ( Book books
) ; Book
queryBookByID ( @Param ( "bookID" ) int id
) ; List
< Book> queryAllBook ( ) ; }
在service包下編寫對應的實現類BookServiceImpl,代碼如下:
package service
; import dao
. BookDao
;
import pojo
. Book
; import java
. util
. List
; public class BookServiceImpl implements BookService { private BookDao bookDao
; public void setBookDao ( BookDao bookDao
) { this . bookDao
= bookDao
; } @Override public int addBook ( Book books
) { return bookDao
. addBook ( books
) ; } @Override public int deleteBookByID ( int id
) { return bookDao
. deleteBookByID ( id
) ; } @Override public int updateBook ( Book books
) { return bookDao
. updateBook ( books
) ; } @Override public Book
queryBookByID ( int id
) { return bookDao
. queryBookByID ( id
) ; } @Override public List
< Book> queryAllBook ( ) { return bookDao
. queryAllBook ( ) ; }
}
5.配置Spring整合dao層
在本例中使用c3p0連接池,需要在Maven導入相關的包,代碼如下:
< dependency> < groupId> com.mchange
</ groupId> < artifactId> c3p0
</ artifactId> < version> 0.9.5.2
</ version> </ dependency> < dependency> < groupId> org.mybatis
</ groupId> < artifactId> mybatis-spring
</ artifactId> < version> 1.3.1
</ version> </ dependency> < dependency> < groupId> org.springframework
</ groupId> < artifactId> spring-jdbc
</ artifactId> < version> 4.3.24.RELEASE
</ version> </ dependency> < dependency> < groupId> org.springframework
</ groupId> < artifactId> spring-webmvc
</ artifactId> < version> 4.3.24.RELEASE
</ version> </ dependency>
在resources目錄下新建spring文件夾,并在該文件夾中新建一個名為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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd" > < context: property-placeholder location = " classpath:database.properties" /> < 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> < 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 = " dao" /> </ bean> < context: component-scan base-package = " dao" /> </ beans>
6.配置Spring整合service層
在resources/spring文件夾中新建一個名為spring-service.xml的配置文件,用于讓Spring整合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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd" > < context: component-scan base-package = " service" /> < bean id = " bookServiceImpl" class = " service.BookServiceImpl" > < property name = " bookDao" ref = " bookDao" /> </ bean> < bean class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = " dataSource" ref = " dataSource" /> </ bean> </ beans>
7.配置Spring整合Spring MVC
修改webapp目錄下的web.xml文件,代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
< web-app xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xmlns = " http://java.sun.com/xml/ns/javaee" xsi: schemaLocation= " http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = " WebApp_ID" version = " 3.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> </ web-app>
在resources/spring文件夾中新建一個名為spring-mvc.xml的配置文件,用于讓Spring整合servlet層,代碼如下:
<?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: mvc= " http://www.springframework.org/schema/mvc" xsi: schemaLocation= " http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd" > < context: component-scan base-package = " controller" /> < mvc: default-servlet-handler/> < mvc: annotation-driven/> < bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name = " prefix" value = " /WEB-INF/jsp/" /> < property name = " suffix" value = " .jsp" /> </ bean> </ beans>
在目錄中新建一個名為filter的包,在包中新建名為GenericEncodingFilter的類,用于字符編碼過濾,代碼如下:
package filter
; import javax
. servlet
. *
;
import javax
. servlet
. http
. HttpServletRequest
;
import javax
. servlet
. http
. HttpServletRequestWrapper
;
import javax
. servlet
. http
. HttpServletResponse
;
import java
. io
. IOException
;
import java
. io
. UnsupportedEncodingException
;
import java
. util
. Map
;
public class GenericEncodingFilter implements Filter { @Override public void destroy ( ) { } @Override public void doFilter ( ServletRequest request
, ServletResponse response
, FilterChain chain
) throws IOException
, ServletException
{ HttpServletResponse myResponse
= ( HttpServletResponse
) response
; myResponse
. setContentType ( "text/html;charset=UTF-8" ) ; HttpServletRequest httpServletRequest
= ( HttpServletRequest
) request
; HttpServletRequest myrequest
= new MyRequest ( httpServletRequest
) ; chain
. doFilter ( myrequest
, response
) ; } @Override public void init ( FilterConfig filterConfig
) throws ServletException
{ } }
class MyRequest extends HttpServletRequestWrapper { private HttpServletRequest request
; private boolean hasEncode
; public MyRequest ( HttpServletRequest request
) { super ( request
) ; this . request
= request
; } @Override public Map
getParameterMap ( ) { String method
= request
. getMethod ( ) ; if ( method
. equalsIgnoreCase ( "post" ) ) { try { request
. setCharacterEncoding ( "utf-8" ) ; return request
. getParameterMap ( ) ; } catch ( UnsupportedEncodingException e
) { e
. printStackTrace ( ) ; } } else if ( method
. equalsIgnoreCase ( "get" ) ) { Map
< String
, String
[ ] > parameterMap
= request
. getParameterMap ( ) ; if ( ! hasEncode
) { for ( String parameterName
: parameterMap
. keySet ( ) ) { String
[ ] values
= parameterMap
. get ( parameterName
) ; if ( values
!= null
) { for ( int i
= 0 ; i
< values
. length
; i
++ ) { try { values
[ i
] = new String ( values
[ i
] . getBytes ( "ISO-8859-1" ) , "utf-8" ) ; } catch ( UnsupportedEncodingException e
) { e
. printStackTrace ( ) ; } } } } hasEncode
= true ; } return parameterMap
; } return super . getParameterMap ( ) ; } @Override public String
getParameter ( String name
) { Map
< String
, String
[ ] > parameterMap
= getParameterMap ( ) ; String
[ ] values
= parameterMap
. get ( name
) ; if ( values
== null
) { return null
; } return values
[ 0 ] ; } @Override public String
[ ] getParameterValues ( String name
) { Map
< String
, String
[ ] > parameterMap
= getParameterMap ( ) ; String
[ ] values
= parameterMap
. get ( name
) ; return values
; }
}
8.配置Spring整合文件
在resources中新建一個名為applicationContext.xml的配置文件,用于整合上面的spring-dao.xml、spring-service.xml、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" xsi: schemaLocation= " http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd" > < import resource = " classpath:spring/spring-dao.xml" /> < import resource = " classpath:spring/spring-service.xml" /> < import resource = " classpath:spring/spring-mvc.xml" /> </ beans>
在Maven導入jsp和servlet的包,代碼如下:
< dependency> < groupId> javax.servlet
</ groupId> < artifactId> javax.servlet-api
</ artifactId> < version> 3.0.1
</ version> < scope> provided
</ scope> </ dependency> < dependency> < groupId> javax.servlet.jsp
</ groupId> < artifactId> jsp-api
</ artifactId> < version> 2.2
</ version> < scope> provided
</ scope> </ dependency> < dependency> < groupId> javax.servlet
</ groupId> < artifactId> jstl
</ artifactId> < version> 1.2
</ version> </ dependency>
9.實現controller層
在項目的java目錄下新建controller包,編寫控制器BookController,代碼如下所示:
package controller
; import org
. apache
. ibatis
. annotations
. Param
;
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 pojo
. Book
;
import service
. BookService
; import java
. util
. List
; @Controller
@RequestMapping ( "/book" )
public class BookController { @Autowired @Qualifier ( "bookServiceImpl" ) private BookService bookService
; @RequestMapping ( "/allBook" ) public String
list ( Model model
) { List
< Book> list
= bookService
. queryAllBook ( ) ; model
. addAttribute ( "list" , list
) ; return "allBook" ; } @RequestMapping ( "/toAddBook" ) public String
toAddBookPage ( ) { return "addBook" ; } @RequestMapping ( "/addBook" ) public String
addBook ( Book book
) { bookService
. addBook ( book
) ; return "redirect:/book/allBook" ; } @RequestMapping ( "/toUpdateBook" ) public String
toUpdateBook ( int id
, Model model
) { Book book
= bookService
. queryBookByID ( id
) ; model
. addAttribute ( "book" , book
) ; return "updateBook" ; } @RequestMapping ( "/updateBook" ) public String
updateBook ( Book book
, Model model
) { bookService
. updateBook ( book
) ; Book book1
= bookService
. queryBookByID ( book
. getBookID ( ) ) ; model
. addAttribute ( "books" , book1
) ; return "redirect:/book/allBook" ; } @RequestMapping ( "/del/{bookID}" ) public String
delete ( @PathVariable ( "bookID" ) int id
) { bookService
. deleteBookByID ( id
) ; return "redirect:/book/allBook" ; }
}
10.實現view層
編寫主頁index.jsp,用于進入查看所有圖書的頁面,代碼如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE HTML>
< html>
< head> < title> 首頁
</ title> < style type = " text/css" > a { text-decoration : none; color : black; font-size : 18px; } h3 { width : 180px; height : 38px; margin : 100px auto; text-align : center; line-height : 38px; background : deepskyblue; border-radius : 4px; } </ style>
</ head>
< body> < h3> < a href = " ${pageContext.request.contextPath}/book/allBook" > 點擊進入列表頁
</ a>
</ h3>
</ body>
</ html>
在WEB-INF目錄下新建jsp目錄,并在該目錄下編寫allBook.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" > < 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 class = " row" > < div class = " col-md-4 column" > < a class = " btn btn-primary" href = " ${pageContext.request.contextPath}/book/toAddBook" > 新增
</ a> </ 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> </ 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}/book/toUpdateBook?id=${book.getBookID()}" > 更改
</ a> |
< a href = " ${pageContext.request.contextPath}/book/del/${book.getBookID()}" > 刪除
</ a> </ td> </ tr> </ c: forEach> </ tbody> </ table> </ div> </ div>
</ div>
在WEB-INF/jsp目錄下編寫addBook.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" > < 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" > 書籍名稱:
< input type = " text" name = " bookName" > < br> < br> < br> 書籍數量:
< input type = " text" name = " bookCounts" > < br> < br> < br> 書籍詳情:
< input type = " text" name = " detail" > < br> < br> < br> < input type = " submit" value = " 添加" > </ form> </ div>
在WEB-INF/jsp目錄下編寫updateBook.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" > < 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 = " ${book.getBookID()}" /> 書籍名稱:
< input type = " text" name = " bookName" value = " ${book.getBookName()}" /> 書籍數量:
< input type = " text" name = " bookCounts" value = " ${book.getBookCounts()}" /> 書籍詳情:
< input type = " text" name = " detail" value = " ${book.getDetail() }" /> < input type = " submit" value = " 提交" /> </ form> </ div>
11.運行前檢查
Maven的包導入應如下所示(注意添加build標簽中的resources標簽,該標簽用于讓Maven導入相關資源):
<?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.moxitao
</ groupId> < artifactId> SSMbuild-test
</ artifactId> < version> 1.0-SNAPSHOT
</ version> < packaging> war
</ packaging> < name> SSMbuild-test Maven Webapp
</ name> < url> http://www.example.com
</ url> < properties> < project.build.sourceEncoding> UTF-8
</ project.build.sourceEncoding> < maven.compiler.source> 1.7
</ maven.compiler.source> < maven.compiler.target> 1.7
</ maven.compiler.target> </ properties> < dependencies> < dependency> < groupId> junit
</ groupId> < artifactId> junit
</ artifactId> < version> 4.11
</ version> < scope> test
</ scope> </ dependency> < dependency> < groupId> mysql
</ groupId> < artifactId> mysql-connector-java
</ artifactId> < version> 5.1.47
</ version> </ dependency> < dependency> < groupId> org.mybatis
</ groupId> < artifactId> mybatis
</ artifactId> < version> 3.4.6
</ version> </ dependency> < dependency> < groupId> org.mybatis
</ groupId> < artifactId> mybatis-spring
</ artifactId> < version> 1.3.1
</ version> </ dependency> < dependency> < groupId> com.mchange
</ groupId> < artifactId> c3p0
</ artifactId> < version> 0.9.5.2
</ version> </ dependency> < dependency> < groupId> org.springframework
</ groupId> < artifactId> spring-webmvc
</ artifactId> < version> 4.3.24.RELEASE
</ version> </ dependency> < dependency> < groupId> org.springframework
</ groupId> < artifactId> spring-jdbc
</ artifactId> < version> 4.3.24.RELEASE
</ version> </ dependency> < dependency> < groupId> javax.servlet
</ groupId> < artifactId> javax.servlet-api
</ artifactId> < version> 3.0.1
</ 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> </ dependencies> < build> < finalName> SSMbuild-test
</ finalName> < pluginManagement> < plugins> < plugin> < artifactId> maven-clean-plugin
</ artifactId> < version> 3.1.0
</ version> </ plugin> < plugin> < artifactId> maven-resources-plugin
</ artifactId> < version> 3.0.2
</ version> </ plugin> < plugin> < artifactId> maven-compiler-plugin
</ artifactId> < version> 3.8.0
</ version> </ plugin> < plugin> < artifactId> maven-surefire-plugin
</ artifactId> < version> 2.22.1
</ version> </ plugin> < plugin> < artifactId> maven-war-plugin
</ artifactId> < version> 3.2.2
</ version> </ plugin> < plugin> < artifactId> maven-install-plugin
</ artifactId> < version> 2.5.2
</ version> </ plugin> < plugin> < artifactId> maven-deploy-plugin
</ artifactId> < version> 2.8.2
</ version> </ plugin> </ plugins> </ pluginManagement> < resources> < resource> < directory> src/main/java
</ directory> < includes> < include> **/*.xml
</ include> </ includes> < filtering> false
</ filtering> </ resource> < resource> < directory> src/main/resources
</ directory> < includes> < include> **/*.xml
</ include> < include> **/*.properties
</ include> </ includes> < filtering> false
</ filtering> </ resource> </ resources> </ build>
</ project>
項目目錄如圖所示:
運行
配置好Tomcat運行環境后,即可開始運行,結果圖如下:
首頁(index.jsp) 顯示全部書籍(allBook.jsp) 增加書籍頁面(addBook.jsp) 修改書籍頁面(updateBook.jsp)
后記
盡管現在的Java Web項目的開發多用Spring Boot進行搭建,但是使用相對原生的SSM(Spring + Spring MVC + Mybatis)進行開發,不僅能讓我們對于Web項目的搭建流程更加熟悉,還會對我們在日后的學習和工作中大有裨益 項目的源碼地址:SSM版本 簡易管理系統,由于該博客之前有一些細節錯誤尚未修正,這里在搭建項目的時候進行了部分修改。對按照該博客搭建項目后遇到坑的讀者表示抱歉,感謝您的瀏覽!
總結
以上是生活随笔 為你收集整理的SSM项目实例——简易版图书管理系统 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。