最近在寫畢設過程當中,從新梳理了一遍SSM框架,特此記錄一下。css
附上源碼:https://gitee.com/niceyoo/jeenotes-ssmhtml
在寫代碼以前咱們先了解一下這三個框架分別是幹什麼的? java
SpringMVC:它用於web層,至關於controller(等價於傳統的servlet和struts的action),用來處理用戶請求。舉個例子,用戶在地址欄輸入http://網站域名/login,那麼springmvc就會攔截到這個請求,而且調用controller層中相應的方法,(中間可能包含驗證用戶名和密碼的業務邏輯,以及查詢數據庫操做,但這些都不是springmvc的職責),最終把結果返回給用戶,而且返回相應的頁面(固然也能夠只返回json/xml等格式數據)。springmvc就是作前面和後面過程的活,與用戶打交道!!mysql
Spring:太強大了,以致於我沒法用一個詞或一句話來歸納它。但與咱們平時開發接觸最多的估計就是IOC容器,它能夠裝載bean(也就是咱們java中的類,固然也包括service dao裏面的),有了這個機制,咱們就不用在每次使用這個類的時候爲它初始化,不多看到關鍵字new。另外spring的aop,事務管理等等都是咱們常常用到的。git
MyBatis:若是你問我它跟鼎鼎大名的Hibernate有什麼區別?我只想說,他更符合個人需求。第一,它能自由控制sql,這會讓有數據庫經驗的人(固然不是說我啦~捂臉~)編寫的代碼能搞提高數據庫訪問的效率。第二,它可使用xml的方式來組織管理咱們的sql,由於通常程序出錯不少狀況下是sql出錯,別人接手代碼後能快速找到出錯地方,甚至能夠優化原來寫的sql。web
IDE: Eclipse Mars2 spring
Jdk: 1.7sql
數據庫: MySQL數據庫
注:本例演示採用的開發工具是Eclipse,不要讓開發工具限制了你的學習,按照本身的須要來建立就好,用什麼工具就按照什麼步驟來建立。apache
新建Dynamic Web Project
File->New->Other->Web->Dynamic Web Project
如下是在Package Explorer 視圖下的完整目錄結構,具體內容看圖:
jar包最後會提供下載地址。
一、Dao層:
Mybatis的配置文件:mybatis-config.xml
不須要配置任何內容,須要有文件頭。文件必須存在。
spring-dao.xml:mybatis整合spring,經過由spring建立數據庫鏈接池,spring管理SqlSessionFactory、mapper代理對象。須要mybatis和spring的整合包。
二、Service層:
spring-service.xml:全部的service實現類都放到spring容器中管理。並由spring管理事務。
三、表現層:
Springmvc框架,由springmvc管理controller。
Springmvc的三大組件。
首先來張圖,有圖有真相,打馬賽克部分暫時用不到,在src下建立resources文件夾... 以下圖所示依次建立。
mybatis——mybatis配置
spring——spring+springmvc+spring和mybatis配置
jdbc.properties——數據庫配置文件
log4j.properties——log日誌
補充:spring包下的文件配置有個知識點須要注意一下,由於spring的配置太多,在這裏分爲spring-dao、spring-service、spring-mvc(至關於spring-web,自己springmvc就是起到web層的做用)三層,一般咱們在別人的項目裏看到的就只有spring-context、spring-mvc,其實你能夠理解成:sprig-context = spring-dao + spring-service ,我拆分開只是爲了更加直觀,不管是拆成幾個,自己內容是不會變的,只要最後在web.xml把文件配置進去可以實例化便可,無須糾結。
以下xml順序不分排名,按照上方截圖依次提供
一、mybatis-config(mybatis配置)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 6 <!-- 別名 --> 7 <!-- 起別名後,不用在mappring resultType 填寫全類名 --> 8 <typeAliases> 9 <package name="com.jeenotes.ssm.pojo"/> 10 </typeAliases> 11 12 </configuration>
二、spring-dao(持久層,spring和mybatis的結合)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 10 11 <!-- 配置 讀取properties文件 jdbc.properties --> 12 <context:property-placeholder location="classpath:resources/jdbc.properties" /> 13 14 <!-- 配置 數據源 --> 15 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> 16 <property name="driverClassName" value="${jdbc.driver}" /> 17 <property name="url" value="${jdbc.url}" /> 18 <property name="username" value="${jdbc.username}" /> 19 <property name="password" value="${jdbc.password}" /> 20 </bean> 21 22 <!-- 配置SqlSessionFactory --> 23 <bean class="org.mybatis.spring.SqlSessionFactoryBean"> 24 <!-- 設置MyBatis核心配置文件 --> 25 <property name="configLocation" value="classpath:resources/mybatis/mybatis-config.xml" /> 26 <!-- 設置數據源 --> 27 <property name="dataSource" ref="dataSource" /> 28 <!-- 它表示咱們的Mapper文件存放的位置,當咱們的Mapper文件跟對應的Mapper接口處於同一位置的時候能夠不用指定該屬性的值。 --> 29 <property name="mapperLocations" value="classpath:/mappings/**/*.xml" /> 30 <!-- 那麼在Mapper文件裏面就能夠直接寫對應的類名 而不用寫全路徑名了 --> 31 <!-- 跟mybatis中<typeAliases>做用同樣 --> 32 <!-- <property name="typeAliasesPackage" value="com.jeenotes.ssm.pojo"/> --> 33 </bean> 34 35 <!-- 配置Mapper掃描 --> 36 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 37 <!-- 設置Mapper掃描包 --> 38 <property name="basePackage" value="com.jeenotes.ssm.dao" /> 39 </bean> 40 41 </beans>
三、spring-service(配置service掃描)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 10 11 <!-- 配置Service掃描 --> 12 <context:component-scan base-package="com.jeenotes.ssm.service" /> 13 </beans>
四、spring-mvc
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:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 9 <!-- 配置Controller掃描 --> 10 <context:component-scan base-package="com.jeenotes.ssm.controller" /> 11 12 <!-- 配置註解驅動 --> 13 <mvc:annotation-driven /> 14 15 <!-- 對靜態資源放行 --> 16 <mvc:resources location="/css/" mapping="/css/**"/> 17 <mvc:resources location="/js/" mapping="/js/**"/> 18 <mvc:resources location="/fonts/" mapping="/fonts/**"/> 19 <mvc:resources location="/frame/" mapping="/frame/**"/> 20 <mvc:resources location="/images/" mapping="/images/**"/> 21 <mvc:resources location="/style/" mapping="/style/**"/> 22 <!-- 配置視圖解析器 --> 23 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 24 <!-- 前綴 --> 25 <property name="prefix" value="/WEB-INF/jsp/" /> 26 <!-- 後綴 --> 27 <property name="suffix" value=".jsp" /> 28 </bean> 29 </beans> 30
五、jdbc.properties
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8 3 jdbc.username=root 4 jdbc.password=****
六、log4j.properties
1 # Global logging configuration 2 log4j.rootLogger=DEBUG, stdout 3 # Console output... 4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
到這裏,其實整個框架的搭建已經基本了,畢竟整點就是上邊這堆配置文件嘛,接下來,就是測試一下了。
是時候讓你看一下部分馬賽克了,參照以下架構可自行建立包
com.jeenotes.ssm:存放control、dao、pojo、service
隨便建立一個control,可參考以下:
@Controller @RequestMapping(value = "user") public class LoginControl { @RequestMapping("login") public String dologin() { return "index"; } }
而後再建立一個index.jsp,jsp文件最好放在WEB-INF目錄下,至於爲什麼,自行百度。
在這我就直接放在外面了,其實儘管不去請求url,項目運行一樣會進入index.jsp,緣由就在於web.xml配置文件中這段配置,
welcome-file-list是一個配置在web.xml中的一個歡迎頁,用於當用戶在url中輸入工程名稱或者輸入web容器url(如http://localhost:8080/jeenotes-ssm/)時直接跳轉的頁面.
<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>
程序運行後,在瀏覽器訪問http://localhost:8080/jeenotes-ssm/user/login 一樣能夠訪問到index.jsp證實測試成功。
如上只是對web層的一次請求測試,接下來就是對整個ssm環境進行測試了,在這咱們經過一個例子繼續。
待補充
SSM(Spring+SpringMVC+Mybatis)框架環境搭建(應用實例)(二)
項目地址:https://gitee.com/niceyoo/jeenotes-ssm
本文地址:http://www.cnblogs.com/niceyoo/articles/8729379.html
我建立了一個java相關的公衆號,用來記錄本身的學習之路,感興趣的小夥伴能夠關注微信公衆號:niceyoo
給你們安利款《微信小程序》,副業賺點外快~