目錄主題帖傳送門(mén):Spring SpringMVC MyBatis 整合-重復(fù)的輪子造的不亦樂(lè)乎 - 簡(jiǎn)書(shū)
1、環(huán)境
Eclipse + JDK1.8 + Tomcat 9.x,環(huán)境一定要統(tǒng)一,否則后患無(wú)窮,很多bug都是環(huán)境不統(tǒng)一造成的。
2、SSM搭建
第一步:就是找Jar包,說(shuō)實(shí)話,找的很辛苦,網(wǎng)上很多Maven和Gradle的,里面加載了很多廢包,Coder的潔癖告訴我要自己動(dòng)手豐衣足食。

項(xiàng)目里的包如上圖
spring-XXX開(kāi)頭的是Spring+SpringMVC常用包,按圖索驥找到所有包是從Spring官網(wǎng)下載的,由于Spring改版,SpringBoot主推,沒(méi)了下載地址,github上下源碼是不可能下了,那么就用Index of libs-release-local/org/springframework/spring?這個(gè)地址試試
commons-XXX開(kāi)頭的是Apache-commons項(xiàng)目的用于數(shù)據(jù)庫(kù)連接池、文件IO相關(guān)的,在Apache官網(wǎng)找的鏈接地址下載Apache Commons – Apache Commons
mybatis-XXX開(kāi)頭的包,是Mybatis官網(wǎng)mybatis – MyBatis 3 | 入門(mén)找到的github地址Releases · mybatis/mybatis-3 · GitHub
Jackson是用于JSON收發(fā)的,是通過(guò)maven下載的,怎么下傳送門(mén):Eclipse Maven下載依賴包 - 簡(jiǎn)書(shū)
JSTL和Standard庫(kù)是JSP標(biāo)準(zhǔn)標(biāo)簽庫(kù),刀耕火種沒(méi)什么不好,不是么,官網(wǎng)地址Index of /dist/jakarta/taglibs/standard/binaries
Mysql-connect-XXX是mysql官網(wǎng)的包,mysql鏈接,各自根據(jù)對(duì)應(yīng)的數(shù)據(jù)庫(kù)找驅(qū)動(dòng)包
annotations-api這個(gè)庫(kù)其實(shí)是從tomcat9的包里拷貝出來(lái)的,在tomcat9-lib包里第一個(gè),它是我拷貝出來(lái)解決注解異常的,應(yīng)用包的規(guī)則是先找應(yīng)用本地的,找不到就找tomcat的。這個(gè)包實(shí)際不用copy出來(lái),因?yàn)榭梢酝ㄟ^(guò)add lib到classPath下面解決異常,后面會(huì)講。
找到這些包,包的版本之間是有差異的,如果不想管這些差異和異常,可以直接按照截圖中的jar包版本號(hào)按圖索驥,后面會(huì)省去很多麻煩。比如Spring5.0以上的包需要搭配Jackson2.6以上的包才行,否則報(bào)錯(cuò)找不到j(luò)ackson中的一個(gè)類,再比如mysql5.0驅(qū)動(dòng)和8.0驅(qū)動(dòng)在className上有個(gè)小差異,會(huì)導(dǎo)致一個(gè)warning。
框架整合不困難,困難的是如何選擇正確的版本。
懶得逐個(gè)找包的用完整的Jar包分享百度盤(pán):SSMJars.zip_免費(fèi)高速下載|百度網(wǎng)盤(pán)-分享無(wú)限制
第二步:建項(xiàng)目
Eclipse建一個(gè)Dynamic Web項(xiàng)目,勾選生成web.xml
將如上找到的jar包拷貝到WebContent > WEB-INF > lib文件夾下,刷新項(xiàng)目

添加Server,在Eclipse > Servers添加Server-Tomcat9

打開(kāi)項(xiàng)目properties > Java Build Path > Libraries > ClassPath下Add Library > Server Runtime選擇tomcat9 Add,如果這里看不到tomcat9,那就回到上面一句話添加Server?

上面做完,其實(shí)annotations-api這個(gè)可以從你的項(xiàng)目lib中刪除了。刪不刪隨你,如果你開(kāi)始就沒(méi)拷貝,就更好了。不刪的話tomcat版本不一致會(huì)導(dǎo)致報(bào)錯(cuò)說(shuō)annotation類重復(fù)。
第三步:文件目錄
結(jié)構(gòu)都是自己建的,我的如下:

WebContent > WEB-INF > web.xml
WebContent下其他可以建可以不建,images放網(wǎng)站UI圖片切片,JavaScripts放自己的JS,lib放jquery等三方UI控件,stylesheets放CSS,upload用于存放上傳的文件,當(dāng)然有條件可以用文件服務(wù)代替本地這種存放。
resources 是自己建的包,里面放Spring、SpringMVC和數(shù)據(jù)庫(kù)配置文件,我嫌麻煩所以數(shù)據(jù)庫(kù)配置直接寫(xiě)Spring.xml里了,所以比那些格式化的項(xiàng)目少了一個(gè)properties文件,要不要無(wú)所謂。
SRC下
com.sidi.controller 存放控制類
com.sidi.dao 存放Mybatis數(shù)據(jù)庫(kù)操作接口,因?yàn)橛米⒔釹QL方式,所以不需要MapperXML
com.sidi.entity?存放Mybatis數(shù)據(jù)庫(kù)實(shí)體對(duì)象,由MybatisGenerator生成,傳送門(mén):Eclipse中使用MyBatis Generator - 簡(jiǎn)書(shū)
com.sidi.service 服務(wù)接口
com.sidi.service.impl 服務(wù)接口實(shí)現(xiàn)類
com.sidi.util 工具類
第四步:配置文件
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/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SIDI</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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*:/spring-mvc.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>
</web-app>
web.xml修改建議:暫時(shí)無(wú)需修改
Spring.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans? ?
? ? http://www.springframework.org/schema/beans/spring-beans-4.2.xsd? ?
? ? http://www.springframework.org/schema/tx? ?
? ? http://www.springframework.org/schema/tx/spring-tx-4.2.xsd? ?
? ? http://www.springframework.org/schema/context? ?
? ? http://www.springframework.org/schema/context/spring-context-4.2.xsd?
? ? http://www.springframework.org/schema/aop
? ? http://www.springframework.org/schema/aop/spring-aop-4.2.xsd?
? ? http://www.springframework.org/schema/mvc? ?
? ? http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
? ? http://www.springframework.org/schema/task
? ? http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<context:component-scan base-package="com.sidi">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/testDB?characterEncoding=utf-8" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
? ? ? ? <property name="sqlSessionFactory" ref="sqlSessionFactory" />
? ? ? ? <property name="basePackage" value="com.sidi.dao" />
? ? </bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Spring.xml修改建議:
driverClassName:我用的connect是8.0 所以是com.mysql.cj.jdbc.Driver,如果5.0的請(qǐng)改為com.mysql.jdbc.Driver
dataSource:里面的鏈接參數(shù)自己替換
context:component-scan base-package="com.sidi" 包名根據(jù)實(shí)際src目錄替換
sqlSessionFactory下的<property name="basePackage"?value="com.sidi.dao"?/> 根據(jù)實(shí)際src中dao目錄替換
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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans? ?
? ? http://www.springframework.org/schema/beans/spring-beans-4.2.xsd? ?
? ? http://www.springframework.org/schema/tx? ?
? ? http://www.springframework.org/schema/tx/spring-tx-4.2.xsd? ?
? ? http://www.springframework.org/schema/context? ?
? ? http://www.springframework.org/schema/context/spring-context-4.2.xsd?
? ? http://www.springframework.org/schema/aop
? ? http://www.springframework.org/schema/aop/spring-aop-4.2.xsd?
? ? http://www.springframework.org/schema/mvc? ?
? ? http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
? ? http://www.springframework.org/schema/task
? ? http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<!-- Spring MVC配置 -->
? ? <context:annotation-config />
<!-- 掃描注解 -->
? ? <context:component-scan base-package="com.sidi"? use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 默認(rèn)的mvc注解映射的支持 -->
? ? <mvc:annotation-driven />
<!-- 靜態(tài)資源配置 -->
? ? <mvc:resources mapping="/stylesheets/**" location="/stylesheets/" />?
? ? <mvc:resources mapping="/images/**" location="/images/" />
? ? <mvc:resources mapping="/lib/**" location="/lib/" />
? ? <mvc:resources mapping="/javascripts/**" location="/javascripts/" />
? ? <mvc:resources mapping="/upload/**" location="/upload/" />
<!-- 定義視圖解析器? -->
? ? <bean id="viewResolver"?
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 文件上傳解析器? -->
? ? <bean id="multipartResolver"
? ? ? ? class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
? ? ? ? <property name="maxUploadSize" value="-1"/>
? ? ? ? <property name="defaultEncoding" value="UTF-8" />
? ? </bean>
</beans>
Spring-MVC.xml修改建議:
掃描注解包名根據(jù)src實(shí)際目錄修改
靜態(tài)資源配置根據(jù)webContent目錄下結(jié)構(gòu)修改,主要是加載靜態(tài)資源不攔截
定義視圖解析器我的跳轉(zhuǎn)是通過(guò)定義的包/WEB-INF/view/XXX.jsp文件完成,其中suffix可以不寫(xiě)兼容HTML,JSP,FreeMaker或者自定義的等格式
至此,SSM拉包搭建完畢,Add進(jìn)Server跑起來(lái),應(yīng)該沒(méi)有任何報(bào)錯(cuò),這里不像其他教程寫(xiě)Hello是因?yàn)椋竺鏁?huì)寫(xiě),這里只保證SSM的Jar包完整,配置正確即可。數(shù)據(jù)庫(kù)連接沒(méi)有就在Spring.xml注釋配置,后面有數(shù)據(jù)庫(kù)再加。啟動(dòng)Tomcat如下圖不報(bào)錯(cuò)即可。

