使用SSM框架做了幾個小項目了,感覺還不錯是時候總結一下了。先總結一下SSM整合的文件配置。其實具體的用法最好還是看官方文檔。
Spring:http://spring.io/docs
MyBatis:http://mybatis.github.io/mybatis-3/
基本的組織結構和用法就不說了,前面的博客和官方文檔上都非常的全面。jar包可以使用Maven來組織管理。來看配置文件。
web.xml的配置
web.xml應該是整個項目最重要的配置文件了,不過servlet3.0中已經支持註解配置方式了。在servlet3.0以前每個servlet必須要在web.xml中配置servlet及其映射關係。但是在spring框架中就不用了,因為Spring中是依賴注入(Dependency Injection)的也叫控制反轉(Inversion of Control)。但是也要配置一個重要的servlet,就是前端控制器(DispatcherServlet)。配置方式與普通的servlet基本相似。
配置內容如下:
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
WEB-INF/classes/spring/springmvc.xml
spring
/
這裡需要注意,springmvc.xml是spring配置文件,將在後面討論。在
配置spring容器:
contextConfigLocation
WEB-INF/classes/spring/applicationContext-*.xml
其中applicationContext-*.xml包含3個配置文件,是springIoC容器的具體配置。後面會提到。
配置一個監聽器:
org.springframework.web.context.ContextLoaderListener
web.xml的完整配置是這樣的:
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
404
/error404.jsp
500
/error500.jsp
contextConfigLocation
WEB-INF/classes/spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
WEB-INF/classes/spring/springmvc.xml
spring
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
welcome.jsp
看到配置文件中多了兩塊內容,一個是error page是用來友好的處理錯誤的,可以使用錯誤代碼來區別並跳轉到相應的處理頁面。這段配置代碼最好放到最前面,在前端控制器攔截之前處理。
還有一塊內容是一個解決post亂碼問題的過濾器,攔截post請求並編碼為utf8。
springmvc.xml的配置
視圖解析器的配置:
在Controller中設置視圖名的時候會自動加上前綴和後綴。
Controller的配置
自動掃描方式,掃描包下面所有的Controller,可以使用註解來指定訪問路徑。
也可以使用單個的配置方式,需要指定Controller的全限定名。
配置註解的處理器適配器和處理器映射器:
也可以使用下面的簡化配置:
配置攔截器,可以直接定義攔截所有請求,也可以自定義攔截路徑。
配置全局異常處理器
配置文件上傳數據解析器,在上傳文件時需要配置。
9242880
還可以配置一些自定義的參數類型,以日期類型綁定為例。
上面提到過如果在配置前端控制器時攔截了所有的請求,不做特殊處理就會導致部分靜態資源無法使用。如果是這種情況就可以使用下面的配置來訪問靜態資源文件。
也可以使用默認,但是需要在web.xml中配置。
完全可以不攔截所有路徑,大可避免這個問題的發生。
完整的配置大概是這樣的,需要注意xml文件的命名空間,有時候會有影響的。
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
9242880
applicationContext-*.xml的配置
applicationContext-*.xml包括三個配置文件,分別對應數據層控制、業務邏輯service控制和事務的控制。
數據訪問層的控制,applicationContext-dao.xml的配置:
配置加載數據連接資源文件的配置,把數據庫連接數據抽取到一個properties資源文件中方便管理。
配置為:
其中jdbc.properties文件的內容如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database
jdbc.username=root
jdbc.password=1234
配置數據庫連接池,這裡使用的是dbcp,別忘了添加jar包!
Spring和MyBatis整合配置,jar包由MyBatis提供。
配置sqlSessionFactory
SqlMapConfig.xml文件是MyBatis的配置文件,後面會提到。
配置Mapper掃描器,掃描mapper包下的所有mapper文件和類,要求mapper配置文件和類名需要一致。
整個applicationContext-dao.xml配置文件應該是這樣的:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
業務邏輯控制,applicationContext-service.xml的配置:
這個文件裡暫時只需要定義service的實現類即可。
事務控制,applicationContext-transaction.xml的配置
配置數據源,使用JDBC控制類。
配置通知,事務控制。
配置AOP切面
整個事務控制的配置是這樣的:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
MyBatis的配置
SqlMapConfig.xml的配置 全局setting配置這裡省略,數據庫連接池在spring整合文件中已經配置,具體setting配置參考官方文檔。
別名的定義:
mapper映射文件的配置:
整個文件的配置應該是這樣的:
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
具體mapper文件的配置,在使用mapper代理的方法時,命名空間需要是對應的Mapper類。
以上只是對SSM框架簡單使用時的配置文件,如果需要深入使用或者需要理解其內部機理需要參考官方文檔和其源代碼。
閱讀更多 java面試筆試 的文章