拉Jar包方式SSM框架搭建

目錄主題帖傳送門: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如下圖不報錯即可聋袋。


下一篇:Spring注解開發(fā)MVC基本范例 - 簡書

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末队伟,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子幽勒,更是在濱河造成了極大的恐慌嗜侮,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件啥容,死亡現(xiàn)場離奇詭異棘钞,居然都是意外死亡,警方通過查閱死者的電腦和手機干毅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門宜猜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人硝逢,你說我怎么就攤上這事姨拥∩鸷恚” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵叫乌,是天一觀的道長柴罐。 經(jīng)常有香客問我,道長憨奸,這世上最難降的妖魔是什么革屠? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮排宰,結果婚禮上似芝,老公的妹妹穿的比我還像新娘。我一直安慰自己板甘,他們只是感情好党瓮,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著盐类,像睡著了一般寞奸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上在跳,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天枪萄,我揣著相機與錄音,去河邊找鬼猫妙。 笑死瓷翻,一個胖子當著我的面吹牛,可吹牛的內容都是我干的吐咳。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼元践,長吁一口氣:“原來是場噩夢啊……” “哼韭脊!你這毒婦竟也來了?” 一聲冷哼從身側響起单旁,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤沪羔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后象浑,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蔫饰,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年愉豺,在試婚紗的時候發(fā)現(xiàn)自己被綠了篓吁。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡蚪拦,死狀恐怖杖剪,靈堂內的尸體忽然破棺而出冻押,到底是詐尸還是另有隱情,我是刑警寧澤盛嘿,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布洛巢,位于F島的核電站,受9級特大地震影響次兆,放射性物質發(fā)生泄漏稿茉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一芥炭、第九天 我趴在偏房一處隱蔽的房頂上張望漓库。 院中可真熱鬧,春花似錦蚤认、人聲如沸米苹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蘸嘶。三九已至,卻和暖如春陪汽,著一層夾襖步出監(jiān)牢的瞬間训唱,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工挚冤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留况增,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓训挡,卻偏偏與公主長得像澳骤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子澜薄,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內容