目錄主題帖傳送門:Spring SpringMVC MyBatis 整合-重復的輪子造的不亦樂乎 - 簡書
1象颖、環(huán)境
Eclipse + JDK1.8 + Tomcat 9.x檐什,環(huán)境一定要統(tǒng)一赵讯,否則后患無窮斑响,很多bug都是環(huán)境不統(tǒng)一造成的傻谁。
2葫慎、SSM搭建
第一步:就是找Jar包岗钩,說實話胞谈,找的很辛苦尘盼,網(wǎng)上很多Maven和Gradle的,里面加載了很多廢包烦绳,Coder的潔癖告訴我要自己動手豐衣足食悔叽。
項目里的包如上圖
spring-XXX開頭的是Spring+SpringMVC常用包,按圖索驥找到所有包是從Spring官網(wǎng)下載的爵嗅,由于Spring改版娇澎,SpringBoot主推,沒了下載地址睹晒,github上下源碼是不可能下了趟庄,那么就用Index of libs-release-local/org/springframework/spring?這個地址試試
commons-XXX開頭的是Apache-commons項目的用于數(shù)據(jù)庫連接池、文件IO相關的伪很,在Apache官網(wǎng)找的鏈接地址下載Apache Commons – Apache Commons
mybatis-XXX開頭的包戚啥,是Mybatis官網(wǎng)mybatis – MyBatis 3 | 入門找到的github地址Releases · mybatis/mybatis-3 · GitHub
Jackson是用于JSON收發(fā)的,是通過maven下載的锉试,怎么下傳送門:Eclipse Maven下載依賴包 - 簡書
JSTL和Standard庫是JSP標準標簽庫猫十,刀耕火種沒什么不好,不是么,官網(wǎng)地址Index of /dist/jakarta/taglibs/standard/binaries
Mysql-connect-XXX是mysql官網(wǎng)的包拖云,mysql鏈接贷笛,各自根據(jù)對應的數(shù)據(jù)庫找驅動包
annotations-api這個庫其實是從tomcat9的包里拷貝出來的,在tomcat9-lib包里第一個宙项,它是我拷貝出來解決注解異常的乏苦,應用包的規(guī)則是先找應用本地的,找不到就找tomcat的尤筐。這個包實際不用copy出來汇荐,因為可以通過add lib到classPath下面解決異常,后面會講盆繁。
找到這些包掀淘,包的版本之間是有差異的,如果不想管這些差異和異常油昂,可以直接按照截圖中的jar包版本號按圖索驥繁疤,后面會省去很多麻煩。比如Spring5.0以上的包需要搭配Jackson2.6以上的包才行秕狰,否則報錯找不到jackson中的一個類,再比如mysql5.0驅動和8.0驅動在className上有個小差異躁染,會導致一個warning鸣哀。
框架整合不困難,困難的是如何選擇正確的版本吞彤。
懶得逐個找包的用完整的Jar包分享百度盤:SSMJars.zip_免費高速下載|百度網(wǎng)盤-分享無限制
第二步:建項目
Eclipse建一個Dynamic Web項目我衬,勾選生成web.xml
將如上找到的jar包拷貝到WebContent > WEB-INF > lib文件夾下,刷新項目
添加Server饰恕,在Eclipse > Servers添加Server-Tomcat9
打開項目properties > Java Build Path > Libraries > ClassPath下Add Library > Server Runtime選擇tomcat9 Add挠羔,如果這里看不到tomcat9,那就回到上面一句話添加Server?
上面做完埋嵌,其實annotations-api這個可以從你的項目lib中刪除了破加。刪不刪隨你,如果你開始就沒拷貝雹嗦,就更好了范舀。不刪的話tomcat版本不一致會導致報錯說annotation類重復。
第三步:文件目錄
結構都是自己建的了罪,我的如下:
WebContent > WEB-INF > web.xml
WebContent下其他可以建可以不建锭环,images放網(wǎng)站UI圖片切片,JavaScripts放自己的JS泊藕,lib放jquery等三方UI控件辅辩,stylesheets放CSS,upload用于存放上傳的文件,當然有條件可以用文件服務代替本地這種存放玫锋。
resources 是自己建的包蛾茉,里面放Spring、SpringMVC和數(shù)據(jù)庫配置文件景醇,我嫌麻煩所以數(shù)據(jù)庫配置直接寫Spring.xml里了臀稚,所以比那些格式化的項目少了一個properties文件,要不要無所謂三痰。
SRC下
com.sidi.controller 存放控制類
com.sidi.dao 存放Mybatis數(shù)據(jù)庫操作接口吧寺,因為用注解SQL方式,所以不需要MapperXML
com.sidi.entity?存放Mybatis數(shù)據(jù)庫實體對象散劫,由MybatisGenerator生成稚机,傳送門:Eclipse中使用MyBatis Generator - 簡書
com.sidi.service 服務接口
com.sidi.service.impl 服務接口實現(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修改建議:暫時無需修改
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的請改為com.mysql.jdbc.Driver
dataSource:里面的鏈接參數(shù)自己替換
context:component-scan base-package="com.sidi" 包名根據(jù)實際src目錄替換
sqlSessionFactory下的<property name="basePackage"?value="com.sidi.dao"?/> 根據(jù)實際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>
<!-- 默認的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實際目錄修改
靜態(tài)資源配置根據(jù)webContent目錄下結構修改,主要是加載靜態(tài)資源不攔截
定義視圖解析器我的跳轉是通過定義的包/WEB-INF/view/XXX.jsp文件完成各薇,其中suffix可以不寫兼容HTML,JSP,FreeMaker或者自定義的等格式
至此充蓝,SSM拉包搭建完畢,Add進Server跑起來纬乍,應該沒有任何報錯,這里不像其他教程寫Hello是因為裸卫,后面會寫仿贬,這里只保證SSM的Jar包完整,配置正確即可墓贿。數(shù)據(jù)庫連接沒有就在Spring.xml注釋配置茧泪,后面有數(shù)據(jù)庫再加。啟動Tomcat如下圖不報錯即可聋袋。