SSM三大框架隨着互聯網的發展,在非企業級應用開發領域其使用占比已經逐漸超過了原來的三駕馬車SSH,其清晰的模塊化,輕量級在追求效率的互聯網項目開發中更占優勢。
SSM框架項目一般基於Maven,通過Maven的jar包管理及其他工具,web項目的搭建和維護變得更加簡便。
相比於Eclipse需要配置集成Maven項目,MyEclipse安裝好后則自動集成進來了Maven,故本文將基於MyEclipse,簡單地介紹如何搭建一個基本的SSM框架項目,具體步驟見下文:
1、新建一個Maven項目


點擊next后,在catalog中選擇All Catalog,Filter中輸入webapp進行搜索過濾,隨后再結果中選擇maven-archetype-webapp,再點擊next:

(Ps:
此處若不需要通過Maven archetype來創建項目的話(Maven archetype是一個插件,用於構建自定義的組織結構的web項目),可直接跳過,進行一個簡單項目的創建;本例需要創建一個web項目,需選擇maven-archetype-webapp。

)
點擊next后如下,其中Artifact Id為項目名;

隨后進行項目的配置:
右鍵項目-Properties,出現以下界面,對編碼進行設置:

隨后對依賴環境進行配置,此處默認總是選擇1.5的JDK,可能是我的Myeclipse的Maven默認設置有點小問題。remove掉換成1.7的jdk(edit修改貌似總是有點小問題,還是remove掉重新來的穩妥。。):

隨后配置新的jdk:


配置完JDK后,再配置編譯器的版本:
(若改完BuildPath后直接點到Java Compiler編譯器設置窗口的話,會彈出以下彈窗,點擊Apply應用即可:

)

隨后再將Project Facets將項目特性配置好,其中Dynamic Web Module/Java/JavaScript/Maven Support for Java EE Projects都已經勾選,
需要將Dynamic Web Module版本選擇為3.0、Java選擇1.7,並勾選JSTL Libraries選擇最新版本1.2.2。
(Ps:此處是選擇各個特性的較新版本,若有其他版本需要,則需自行嘗試,檢查沖突性,同時Maven也會給出相關版本不兼容的提示)

(Ps:若是選擇構建簡單項目,則也直接按上面的勾選即可,但配置完后項目可能會報錯,見下:
配置完后項目如下所示,發現項目存在報錯:cannot detect Web Project version


錯誤報在pom.xml處,
(Project Object Model,項目對象模型。通過xml格式保存的pom.xml文件。
作用類似ant的build.xml文件,功能更強大。
該文件用於管理:源代碼、配置文件、開發者的信息和角色、問題追蹤系統、組織信息、項目授權、項目的url、項目的依賴關系等等。)
雙擊進去后,對其版本進行設置:

設置后報錯消除,到此為止SSM項目的創建基本完成。
比較奇怪的是,設置了版本后pom.xml的內容僅僅增加了一個插件maven-war-plugin,該插件用於將Maven項目打包成war包:

但設置完版本后如果將此插件刪除,報錯則不再出現了???比較疑惑這是怎么個情況。。。
)
2、SSM三大框架jar包導入:
在pom.xml中導入jar包,直接在<project>標簽下插入以下內容:
1 <!-- 導入的Jar包詳細配置及注釋 --> 2 <!-- 統一導入的SSM框架jar包的版本,避免版本沖突,方便后續jar包導入的版本管理及統一維護修改 --> 3 <properties> 4 <!-- spring版本號 --> 5 <spring.version>4.0.2.RELEASE</spring.version> 6 <!-- mybatis版本號 --> 7 <mybatis.version>3.2.6</mybatis.version> 8 <!-- log4j日志文件管理包版本 --> 9 <slf4j.version>1.7.7</slf4j.version> 10 <log4j.version>1.2.17</log4j.version> 11 </properties> 12 <dependencies> 13 <dependency> 14 <groupId>junit</groupId> 15 <artifactId>junit</artifactId> 16 <version>4.11</version> 17 <!-- 表示開發的時候引入,發布的時候不會加載此包 --> 18 <scope>test</scope> 19 </dependency> 20 <!-- spring核心包 --> 21 <dependency> 22 <groupId>org.springframework</groupId> 23 <artifactId>spring-core</artifactId> 24 <version>${spring.version}</version> 25 </dependency> 26 27 <dependency> 28 <groupId>org.springframework</groupId> 29 <artifactId>spring-web</artifactId> 30 <version>${spring.version}</version> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-oxm</artifactId> 35 <version>${spring.version}</version> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework</groupId> 39 <artifactId>spring-tx</artifactId> 40 <version>${spring.version}</version> 41 </dependency> 42 43 <dependency> 44 <groupId>org.springframework</groupId> 45 <artifactId>spring-jdbc</artifactId> 46 <version>${spring.version}</version> 47 </dependency> 48 49 <dependency> 50 <groupId>org.springframework</groupId> 51 <artifactId>spring-webmvc</artifactId> 52 <version>${spring.version}</version> 53 </dependency> 54 <dependency> 55 <groupId>org.springframework</groupId> 56 <artifactId>spring-aop</artifactId> 57 <version>${spring.version}</version> 58 </dependency> 59 60 <dependency> 61 <groupId>org.springframework</groupId> 62 <artifactId>spring-context-support</artifactId> 63 <version>${spring.version}</version> 64 </dependency> 65 66 <dependency> 67 <groupId>org.springframework</groupId> 68 <artifactId>spring-test</artifactId> 69 <version>${spring.version}</version> 70 </dependency> 71 <!-- mybatis核心包 --> 72 <dependency> 73 <groupId>org.mybatis</groupId> 74 <artifactId>mybatis</artifactId> 75 <version>${mybatis.version}</version> 76 </dependency> 77 <!-- mybatis/spring包 --> 78 <dependency> 79 <groupId>org.mybatis</groupId> 80 <artifactId>mybatis-spring</artifactId> 81 <version>1.2.2</version> 82 </dependency> 83 <!-- 導入java ee jar 包 --> 84 <dependency> 85 <groupId>javax</groupId> 86 <artifactId>javaee-api</artifactId> 87 <version>7.0</version> 88 </dependency> 89 <!-- 導入Mysql數據庫鏈接jar包 --> 90 <dependency> 91 <groupId>mysql</groupId> 92 <artifactId>mysql-connector-java</artifactId> 93 <version>5.1.30</version> 94 </dependency> 95 <!-- 導入dbcp的jar包,用來在applicationContext.xml中配置數據庫 --> 96 <dependency> 97 <groupId>commons-dbcp</groupId> 98 <artifactId>commons-dbcp</artifactId> 99 <version>1.2.2</version> 100 </dependency> 101 <!-- JSTL標簽類 --> 102 <dependency> 103 <groupId>jstl</groupId> 104 <artifactId>jstl</artifactId> 105 <version>1.2</version> 106 </dependency> 107 <!-- 日志文件管理包 --> 108 <!-- log start --> 109 <dependency> 110 <groupId>log4j</groupId> 111 <artifactId>log4j</artifactId> 112 <version>${log4j.version}</version> 113 </dependency> 114 115 116 <!-- 格式化對象,方便輸出日志 --> 117 <dependency> 118 <groupId>com.alibaba</groupId> 119 <artifactId>fastjson</artifactId> 120 <version>1.1.41</version> 121 </dependency> 122 123 124 <dependency> 125 <groupId>org.slf4j</groupId> 126 <artifactId>slf4j-api</artifactId> 127 <version>${slf4j.version}</version> 128 </dependency> 129 130 <dependency> 131 <groupId>org.slf4j</groupId> 132 <artifactId>slf4j-log4j12</artifactId> 133 <version>${slf4j.version}</version> 134 </dependency> 135 <!-- log end --> 136 <!-- 映入JSON --> 137 <dependency> 138 <groupId>org.codehaus.jackson</groupId> 139 <artifactId>jackson-mapper-asl</artifactId> 140 <version>1.9.13</version> 141 </dependency> 142 <!-- 上傳組件包 --> 143 <dependency> 144 <groupId>commons-fileupload</groupId> 145 <artifactId>commons-fileupload</artifactId> 146 <version>1.3.1</version> 147 </dependency> 148 <dependency> 149 <groupId>commons-io</groupId> 150 <artifactId>commons-io</artifactId> 151 <version>2.4</version> 152 </dependency> 153 <dependency> 154 <groupId>commons-codec</groupId> 155 <artifactId>commons-codec</artifactId> 156 <version>1.9</version> 157 </dependency> 158 </dependencies>
如果你精確地知道你要導入的jar包的包名及版本,那么就可以才用上面的方式導入依賴包。但是如果記不太清也不要緊,Maven提供了依賴jar包搜索的功能,點擊pom.xml的Dependencies可進入jar包管理界面如下:

其中關於pom.xml的介紹:
Overview:顯示maven項目的一些基本信息.
Dependencies:添加jar包的頁面,很重要!
Plugins:添加maven插件的頁面.比如tomcat-maven-plugin等.
Reporting:從沒用過,無視~
Dependency Hierarchy:用於顯示jar包的依賴關系.沒事的時候可以看看jar包的依賴關系. Effective POM:顯示maven的編譯路徑,plugin之類的.也可以無視.
pom.xml:導入jar包的信息,可以在其中進行修改.重要
Dependency Graph:依賴圖
接下來對Spring及Mybatis進行整合,在src/main/resource/下新建spring-mybatis.xml(數據庫側配置),並將以下配置內容寫入:
(其中<context:property-placeholder location="classpath:jdbc.properties"/><!-- 加載配置文件 -->是一種加載存放着數據庫相關配置信息的properties文件方式,
除了此種,還可以在<bean>標簽中定義,定義后可以在xml中引用或者在java代碼中使用注解方式實現注入(使用@Value標簽)。具體可見一篇介紹博客:http://blog.csdn.net/eson_15/article/details/51365707
)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 10 <context:property-placeholder location="classpath:jdbc.properties"/><!-- 加載配置文件 --> 11 12 <bean id="dataSource" class="${dataSource}" 13 destroy-method="close"> 14 <property name="driverClassName" value="${driverClassName}" /> 15 <property name="url" value="${url}" /> 16 <property name="username" value="${user}" /> 17 <property name="password" value="${password}" /> 18 <!-- 初始化連接大小 --> 19 <property name="initialSize" value="${initialSize}"></property> 20 <!-- 連接池最大數量 --> 21 <property name="maxActive" value="${maxActive}"></property> 22 <!-- 連接池最大空閑 --> 23 <property name="maxIdle" value="${maxIdle}"></property> 24 <!-- 連接池最小空閑 --> 25 <property name="minIdle" value="${minIdle}"></property> 26 <!-- 獲取連接最大等待時間 --> 27 <property name="maxWait" value="${maxWait}"></property> 28 </bean> 29 <context:annotation-config /> <!-- 此行語句使得resource autowired 等四個注解可以使用 --> 30 31 <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> 32 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 33 <property name="dataSource" ref="dataSource" /> 34 <!-- 自動掃描mapping.xml文件 --> 35 <property name="mapperLocations" value="classpath:mapper/*.xml"></property> 36 </bean> 37 <!-- DAO接口所在包名,Spring會自動查找其下的類 --> 38 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 39 <property name="basePackage" value="cn.job.dao" /> 40 <!-- 41 <property name="sqlSessionFactory" value="sqlSessionFactoryBean"/> 42 這句話可寫可不寫,因為spring會去自動查找並注入 43 --> 44 </bean> 45 46 <!-- 配置事務管理器 --> 47 <bean id="txManager" 48 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 49 <property name="dataSource" ref="dataSource" /> 50 </bean> 51 52 <!-- 事務掃描開始(開啟@Tranctional) --> 53 <tx:annotation-driven transaction-manager="txManager" /> 54 55 </beans>
隨后在web.xml中配置容器啟動內容:
(spring-*.xml代表讀取所有以spring-開頭的xml文件,
classpath就是代表 /WEB-INF /classes/ 這個路徑,
classpath 和 classpath* 區別:
classpath:只會到你的class路徑中查找找文件;
classpath*:不僅包含class路徑,還包括jar文件中(class路徑)進行查找 ---- 夠深入的吧
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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_2_5.xsd" version="2.5"> 3 <welcome-file-list> 4 <welcome-file>index.jsp</welcome-file> 5 </welcome-file-list> 6 <!-- 啟動spring容器 --> 7 <servlet> 8 <servlet-name>springmvc</servlet-name> 9 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 10 <init-param> 11 <param-name>contextConfigLocation</param-name> 12 <param-value>classpath:spring-*.xml</param-value> 13 </init-param> 14 <!-- 啟動順序優先 --> 15 <load-on-startup>1</load-on-startup> 16 </servlet> 17 <!-- 對請求后綴進行篩選 --> 18 <servlet-mapping> 19 <servlet-name>springmvc</servlet-name> 20 <url-pattern>*.e</url-pattern> 21 </servlet-mapping> 22 </web-app>
配置視圖層(展示層)的spring-web.xml文件:
(<mvc:annotation-driven/>相當於注冊了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter兩個bean,配置一些messageconverter。即解決了@Controller注解的使用前提配置。
<context:annotation-config/>是對包進行掃描,實現注釋驅動Bean定義,同時將bean自動注入容器中使用。即解決了@Controller標識的類的bean的注入和使用。)
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 注解掃描和驅動配置 --> <context:component-scan base-package="cn.job.action"/> <mvc:annotation-driven/> </beans>
最后配置業務層spring-service.xml,與視圖層類似,僅僅是掃了@Controller注解驅動部分:
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <context:component-scan base-package="cn.job.services"/> </beans>
配置完后,接下來就可以寫代碼了,從dao層開始,寫數據庫側接口:
1 package cn.job.dao; 2 3 import org.springframework.stereotype.Repository; 4 5 @Repository("testDao") 6 public interface testDao { 7 8 String getSysdate(); 9 10 void testPG(); 11 }
對應地寫sql映射文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="cn.job.dao.testDao"> <select id="getSysdate" resultType="String"> select now() </select> <update id="testPG"> drop table if exists mid_ydyh,mid_ydsyxz,mid_ydbyxz,mid_ydsyxzC,mid_ydsyxzR; select a.* into mid_ydsyxz from WID_CHN_PRD_SERV_MKT_MON a where a.acct_month=201702 and a.NEW_FLAG='T' and a.product_id=208511296; </update> </mapper>
每一個接口中的方法都必須在mapper.xml中的<mapper>標簽中對應着有一個id唯一的bean,在bean標簽內寫着對應的sql語句,同時每一個接口最好1對1地有一個mapper.xml映射文件:

最后,根據想寫的demo把剩余的action、services、index.jsp寫好,結構即如下:

成功!


對SSM框架的掌握僅僅停留在應用層面,十分慚愧,后續將以此博客為起點,逐漸學習SSM框架中經典的實現原理及方式!
每天進步一點點!
