搭建ssm框架項目基本原理和主要的配置文件小結
1.springmvc是spring框架的一個模塊,springmvc和spring無需通過中間整合層進行整合。springmvc是一個基於mvc的web框架。mvc的思想大家已經很熟悉了,簡稱“Model-View-Controller”。
下面先簡單介紹下我對spring-mvc的理解。
上面這張圖大概說明了springmvc的運行過程,看起來可能有點雲裏霧裏的,總結起來就是下面這些:
-
客戶端發起請求到前端控制器(DispatcherServlet).
-
前端控制器請求HandlerMappering 查找Handler,可以根據xml配置、註解進行查找。
-
DispatcherServlet將請求提交到Controller;
-
Controller調用業務邏輯處理後,返回ModelAndView;
-
DispatcherServlet查詢一個或多個ViewResoler視圖解析器,找到ModelAndView指定的視圖;
-
視圖負責將結果顯示到客戶端。
這裏稍微解釋下常用的幾個組件名稱和作用。
-
前端控制器(DispatcherServlet):用於接收請求,響應結果
-
處理器映射器(HandlerMapping):根據請求的url查找Handler(三大核心組件之一)
-
處理器適配器(HandlerAdapter):按照特定的規則去執行Handler(三大核心組件之一)
-
處理器(Handler):編寫Handler時按照HandlerAdapter的要求去做,這樣適配器才可以去正確執行Handler
-
視圖解析器(View resolver):進行視圖解析(三大核心組件之一)
-
視圖(View):包括jsp、pdf等
在了解了上面的基礎原理後下面來講下幾個主要的配置文件。項目開發前所需要的jar包提前要導入項目工程裏面去。
有幾個主要的配置文件,先了解下每個配置文件的作用。
1. web.xml:當服務啟動時首先會去加載web.xml這個資源文件,裏面包括了對前端控制器、亂碼問題等配置。
2.applicatonContext.xml : 一般配置數據源,事物,註解 等。
在這裏我使用的是applicatonContext-*.xml的形式將DAO層、Service層、Transaction
分別為applicatonContext-dao.xml、applicatonContext-service.xml、applicatonContext-transaction.xml
分開配置時,需要在web.xml中配置上下文位置
3.springmvc.xml: 裏面配置的是控制層的 ,如視圖解析器靜態資源, mvc 文件上傳,攔截器等。
4.SqlMapConfig.xml: 該配置文件為MyBatis的配置文件,裏面無需配置,一切交給spring管理,但是xml文件基礎配置要有。
以下是配置文件代碼:
web.xml
[html] view plain copy print?
- <?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_2_5.xsd"
- id="WebApp_ID" version="2.5">
- <display-name>ssm_boot_crm</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>
- <!-- 上下文的位置 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext-*.xml</param-value>
- </context-param>
- <!-- Spring的監聽器 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- POST提交過濾器 UTF-8 -->
- <filter>
- <filter-name>encoding</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>encoding</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <!-- 前端控制器 -->
- <servlet>
- <servlet-name>crm</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <!-- 此處不配置 默認找 /WEB-INF/[servlet-name]-servlet.xml -->
- <param-value>classpath:springmvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>crm</servlet-name>
- <!-- 1:*.do *.action 攔截以.do結尾的請求 (不攔截 jsp png jpg .js .css)
- 2:/ 攔截所有請求 (不攔截.jsp) 建議使用此種 方式 (攔截 .js.css .png) (放行靜態資源)
- 3:/* 攔截所有請求(包括.jsp) 此種方式 不建議使用 -->
- <url-pattern>*.action</url-pattern>
- </servlet-mapping>
- </web-app>
applicationContext-dao.xml
[html] view plain copy print?
- <?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:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- 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
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-4.0.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <!-- srping框架 配置文件 用於管理數據庫連接池 -->
- <!-- 配置 讀取properties文件 db.properties -->
- <context:property-placeholder location="classpath:db.properties" />
- <!-- 配置 數據源 用於連接數據庫 -->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <!-- 數據庫驅動 -->
- <property name="driverClassName" value="${jdbc.driver}" />
- <!-- 連接地址 -->
- <property name="url" value="${jdbc.url}" />
- <!-- 用戶名 -->
- <property name="username" value="${jdbc.username}" />
- <!-- 密碼 -->
- <property name="password" value="${jdbc.password}" />
- </bean>
- <!-- 配置 Mybatis的工廠 -->
- <bean class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 綁定數據源 -->
- <property name="dataSource" ref="dataSource" />
- <!-- 配置Mybatis的核心 配置文件所在位置 -->
- <property name="configLocation" value="classpath:SqlMapConfig.xml" />
- <!-- 配置pojo別名 -->
- <property name="typeAliasesPackage" value="com.company.ssm.crm.pojo" />
- </bean>
- <!-- 配置 1:原始Dao開發 接口實現類 Mapper.xml 三個
- 2:接口開發 接口 不寫實現類 Mapper.xml 二個 (UserDao、ProductDao
- 、BrandDao。。。。。。。)
- 3:接口開發、並支持掃描 cn.itcast.core.dao(UserDao。。。。。) 寫在此包下即可被掃描到
- -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.company.ssm.crm.dao" />
- </bean>
- </beans>
applicationContext-service.xml
[html] view plain copy print?
- <?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:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- 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
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-4.0.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <!-- 配置掃描包 掃描 @Service spring代理管理業務層 -->
- <context:component-scan base-package="com.company.ssm.crm.service" />
- </beans>
applicationContext-transaction.xml
[html] view plain copy print?
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- 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
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
- <!-- spring 事務管理器 -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <!-- 數據源 -->
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 通知 -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <!-- 傳播行為 -->
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="create*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
- <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
- <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
- </tx:attributes>
- </tx:advice>
- <!-- AOP 切面 -->
- <aop:config>
- <aop:advisor advice-ref="txAdvice"
- pointcut="execution(* cn.itcast.core.service.*.*(..))" />
- </aop:config>
- </beans>
springmvc.xml
[html] view plain copy print?
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- 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
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-4.0.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <!-- 加載屬性文件 -->
- <context:property-placeholder location="classpath:resource.properties" />
- <!-- 配置掃描 器 -->
- <context:component-scan base-package="com.company.ssm.crm.controller" />
- <!-- 配置處理器映射器 適配器 -->
- <mvc:annotation-driven />
- <!-- 配置視圖解釋器 jsp -->
- <bean id="jspViewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
SqlMapConfig.xml
[html] view plain copy print?
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- 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
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-4.0.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <!-- 加載屬性文件 -->
- <context:property-placeholder location="classpath:resource.properties" />
- <!-- 配置掃描 器 -->
- <context:component-scan base-package="com.company.ssm.crm.controller" />
- <!-- 配置處理器映射器 適配器 -->
- <mvc:annotation-driven />
- <!-- 配置視圖解釋器 jsp -->
- <bean id="jspViewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
db.properties
[html] view plain copy print?
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8
- jdbc.username=root
- jdbc.password=root
以上就是主要的配置文件的配置,特別註意的是在web.xml中默認加載的資源文件再WEB_INF目錄下,如果的你xml不在的話就要寫清楚的文件路徑,例如寫在src目錄下面就要寫classpath,之前開發過程中忽略了這一點,所以啟動一直報錯找不到資源文件。
主要參考出處
並結合自己的SSM搭建,小結SSM環境搭建,以便記憶,交流和學習,有什麽不妥的地方,歡迎指正、交流
搭建ssm框架項目基本原理和主要的配置文件小結