SSM整合时配置文件的编写
編寫web.xml
1.配置項目初始化時啟動Spring容器
傳遞參數,spring配置文件的位置
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- Bootstraps the root web application context before servlet initialization --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>其中applicationContext.xml為spring配置文件,位置在src/main/resource目錄下。
2.編寫SpringMVC前段控制器,攔截所有請求
方式一:
在param中通過location指定springMVC配置文件的位置
<servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>location</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>方式二:
去掉init-param,名字統一改為dispatcherServlet,然后在與web.xml同級目錄下新建dispatcherServlet-servlet.xml
其中dispatcherServlet要與上面的servlet-name一致。?
<servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- <init-param><param-name>contextConfigLocation</param-name><param-value>location</param-value></init-param> --><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>3.編寫springMVC帶的字符編碼過濾器,字符編碼器要放在所有過濾器之前
<!-- 字符編碼過濾器,一定要放在所有過濾器之前 --><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><init-param><param-name>forceRequestEncoding</param-name><param-value>true</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>4.配置Rest風格的URL的過濾器
<!-- 使用Rest風格的URI,將頁面普通的post請求轉為指定的delete或者put請求 --><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>HttpPutFormContentFilter</filter-name><filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class></filter><filter-mapping><filter-name>HttpPutFormContentFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>5.完整的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_2_5.xsd"id="WebApp_ID" version="2.5"><!--1、啟動Spring的容器? --><!-- needed for ContextLoaderListener --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- Bootstraps the root web application context before servlet initialization --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--2、springmvc的前端控制器,攔截所有請求? --><!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- <init-param><param-name>contextConfigLocation</param-name><param-value>location</param-value></init-param> --><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 3、字符編碼過濾器,一定要放在所有過濾器之前 --><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><init-param><param-name>forceRequestEncoding</param-name><param-value>true</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 4、使用Rest風格的URI,將頁面普通的post請求轉為指定的delete或者put請求 --><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>HttpPutFormContentFilter</filter-name><filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class></filter><filter-mapping><filter-name>HttpPutFormContentFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>編寫springMVC的配置文件
在上面方式二新建的dispatcherServlet-servlet.xml中進行配置
主要配置網站的跳轉邏輯的控制、配置視圖解析器,方便頁面返回等。
所以我們先在項目下新建相應的mvc結構的包。
1.配置掃描組件
<!--SpringMVC的配置文件,包含網站跳轉邏輯的控制,配置? --><context:component-scan base-package="com.badao" use-default-filters="false"><!--只掃描控制器。? --><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>SpringMVC默認是掃描所有的,所以為了讓它只掃描控制器,要加上use-default-filters="false"
2.配置視圖解析器
<!--配置視圖解析器,方便頁面返回? --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"></property><property name="suffix" value=".jsp"></property></bean>在WEB-INF目錄下新建views目錄,用來存放jsp頁面,配置好前綴與后綴,就能解析視圖,返回相應頁面。
3.SpringMVC的兩個標準配置
?<!--兩個標準配置? --><!-- 將springmvc不能處理的請求交給tomcat,這樣動態與靜態資源就都能訪問了 --><mvc:default-servlet-handler/><!-- 能支持springmvc更高級的一些功能,JSR303校驗,快捷的ajax...映射動態請求 --><mvc:annotation-driven/>4.完整配置springMVC配置文件代碼
<?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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!--SpringMVC的配置文件,包含網站跳轉邏輯的控制,配置? --><context:component-scan base-package="com.badao" use-default-filters="false"><!--只掃描控制器。? --><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!--配置視圖解析器,方便頁面返回? --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"></property><property name="suffix" value=".jsp"></property></bean><!--兩個標準配置? --><!-- 將springmvc不能處理的請求交給tomcat,這樣動態與靜態資源就都能訪問了 --><mvc:default-servlet-handler/><!-- 能支持springmvc更高級的一些功能,JSR303校驗,快捷的ajax...映射動態請求 --><mvc:annotation-driven/></beans>編寫spring的配置文件
在src/main/resource目錄下新建applicationContext.xml
這里主要配置和業務邏輯有關的,比如數據源、事務控制等。
1.配置數據源
<context:property-placeholder location="classpath:dbconfig.properties" /><bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="driverClass" value="${jdbc.driverClass}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean>其中location="classpath:dbconfig.properties" 就是屬性文件存放的位置。
通過<context:property-placeholder location="classpath:dbconfig.properties" />就可以將屬性文件引入。
2.新建屬性文件dbconfig.properties
在resource下新建dbconfig.properties文件,用來存取數據源的相關屬性,在applicationContext.xml中就可以通過
${}來獲取屬性。
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud jdbc.driverClass=com.mysql.jdbc.Driver jdbc.user=root jdbc.password=123456這里ssm_crud是建立的mysql的數據庫的名字
root與123456是對應自己的連接數據庫的用戶名和密碼
3.配置掃描業務邏輯組件
<context:component-scan base-package="com.badao"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan>4.配置和Mybatis整合
<!--================== 配置和MyBatis的整合=============== --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 指定mybatis全局配置文件的位置 --><property name="configLocation" value="classpath:mybatis-config.xml"></property><property name="dataSource" ref="pooledDataSource"></property><!-- 指定mybatis,mapper文件的位置 --><property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean>所以在resource下新建mapper目錄用來存放mapper文件,在上面已經聲明位置。
5.配置掃描器
將mybatis接口的實現加入到ioc容器中
<!-- 配置掃描器,將mybatis接口的實現加入到ioc容器中 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--掃描所有dao接口的實現,加入到ioc容器中 --><property name="basePackage" value="com.badao.ssm.dao"></property></bean>6.配置事務控制
<!-- ===============事務控制的配置 ================--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--控制數據源? --><property name="dataSource" ref="pooledDataSource"></property></bean><!--開啟基于注解的事務,使用xml配置形式的事務(必要主要的都是使用配置式)? --><aop:config><!-- 切入點表達式 此包下的所有類的所有方法都切入事務--><aop:pointcut expression="execution(* com.badao.ssm.service..*(..))" id="txPoint"/><!-- 配置事務增強 --><aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/></aop:config><!--配置事務增強,事務如何切入? --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 所有方法都是事務方法 --><tx:method name="*"/><!--以get開始的所有方法? --><tx:method name="get*" read-only="true"/></tx:attributes></tx:advice>7.完整的spring的配置文件
<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><context:component-scan base-package="com.badao"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan><!-- Spring的配置文件,這里主要配置和業務邏輯有關的 --><!--=================== 數據源,事務控制,xxx ================--><context:property-placeholder location="classpath:dbconfig.properties" /><bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="driverClass" value="${jdbc.driverClass}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean><!--================== 配置和MyBatis的整合=============== --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 指定mybatis全局配置文件的位置 --><property name="configLocation" value="classpath:mybatis-config.xml"></property><property name="dataSource" ref="pooledDataSource"></property><!-- 指定mybatis,mapper文件的位置 --><property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean><!-- 配置掃描器,將mybatis接口的實現加入到ioc容器中 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--掃描所有dao接口的實現,加入到ioc容器中 --><property name="basePackage" value="com.badao.ssm.dao"></property></bean><!-- 配置一個可以執行批量的sqlSession --><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg><constructor-arg name="executorType" value="BATCH"></constructor-arg></bean><!--=============================================? --><!-- ===============事務控制的配置 ================--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--控制數據源? --><property name="dataSource" ref="pooledDataSource"></property></bean><!--開啟基于注解的事務,使用xml配置形式的事務(必要主要的都是使用配置式)? --><aop:config><!-- 切入點表達式 此包下的所有類的所有方法都切入事務--><aop:pointcut expression="execution(* com.badao.ssm.service..*(..))" id="txPoint"/><!-- 配置事務增強 --><aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/></aop:config><!--配置事務增強,事務如何切入? --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 所有方法都是事務方法 --><tx:method name="*"/><!--以get開始的所有方法? --><tx:method name="get*" read-only="true"/></tx:attributes></tx:advice></beans>編寫MyBatis的配置文件
在resource下新建mybatis-config.xml,作為全局配置文件
1.開啟駝峰命名規則
<settings><setting name="mapUnderscoreToCamelCase" value="true"/></settings>2.開啟別名
<typeAliases><package name="com.badao.ssm.bean"/></typeAliases>3.完整mybatis-config.xml文件代碼
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><settings><setting name="mapUnderscoreToCamelCase" value="true"/></settings><typeAliases><package name="com.badao.ssm.bean"/></typeAliases></configuration>代碼下載
本階段示例代碼下載
https://download.csdn.net/download/badao_liumang_qizhi/10857091
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的SSM整合时配置文件的编写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快速入门SSM整合配置建立第一个SSM项
- 下一篇: XCode提交app时提示SDK Ver