javascript
Spring、Spring MVC、MyBatis整合文件配置详解
?
前些天發(fā)現(xiàn)了一個(gè)巨牛的人工智能學(xué)習(xí)網(wǎng)站,通俗易懂,風(fēng)趣幽默,忍不住分享一下給大家。點(diǎn)擊跳轉(zhuǎn)到教程。
web.xml的配置 ?
web.xml應(yīng)該是整個(gè)項(xiàng)目最重要的配置文件了,不過(guò)servlet3.0中已經(jīng)支持注解配置方式了。在servlet3.0以前每個(gè)servlet必須要在web.xml中配置servlet及其映射關(guān)系。但是在spring框架中就不用了,因?yàn)镾pring中是依賴注入(Dependency Injection)的也叫控制反轉(zhuǎn)(Inversion of Control)。但是也要配置一個(gè)重要的servlet,就是前端控制器(DispatcherServlet)。配置方式與普通的servlet基本相似。
配置內(nèi)容如下:
<!-- 配置前端控制器 --> <servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><!-- ContextconfigLocation配置springmvc加載的配置文件適配器、處理映射器等--><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping><servlet-name>spring</servlet-name><!-- 1、.action訪問(wèn)以.action結(jié)尾的 由DispatcherServlet進(jìn)行解析2、/,所有訪問(wèn)都由DispatcherServlet進(jìn)行解析--><url-pattern>/</url-pattern> </servlet-mapping> <servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><!-- ContextconfigLocation配置springmvc加載的配置文件適配器、處理映射器等--><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping><servlet-name>spring</servlet-name><!-- 1、.action訪問(wèn)以.action結(jié)尾的 由DispatcherServlet進(jìn)行解析2、/,所有訪問(wèn)都由DispatcherServlet進(jìn)行解析--><url-pattern>/</url-pattern> </servlet-mapping>這里需要注意,springmvc.xml是spring配置文件,將在后面討論。在<servlet-mapping>中url如果是.action,前端控制器就只會(huì)攔截以.action結(jié)尾的請(qǐng)求,并不會(huì)理會(huì)靜態(tài)的文件。對(duì)靜態(tài)頁(yè)面的控制就要通過(guò)其他的手段。以/作為url的話就會(huì)攔截所有的請(qǐng)求,包括靜態(tài)頁(yè)面的請(qǐng)求。這樣的話就可以攔截任何想要處理的請(qǐng)求,但是有一個(gè)問(wèn)題。如果攔截了所有的請(qǐng)求,如果不在攔截器中做出相應(yīng)的處理那么所有靜態(tài)的js、css以及頁(yè)面中用到的圖片就會(huì)訪問(wèn)不到造成頁(yè)面無(wú)法正常顯示。但這可以通過(guò)靜態(tài)資源的配置來(lái)解決這個(gè)問(wèn)題。后面會(huì)提到。
配置spring容器:
<context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/spring/applicationContext-*.xml</param-value></context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/spring/applicationContext-*.xml</param-value></context-param>其中applicationContext-*.xml包含3個(gè)配置文件,是springIoC容器的具體配置。后面會(huì)提到。
配置一個(gè)監(jiān)聽(tīng)器:
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>web.xml的完整配置是這樣的:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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"><display-name></display-name> <!-- 404錯(cuò)誤攔截 --><error-page><error-code>404</error-code><location>/error404.jsp</location></error-page><!-- 500錯(cuò)誤攔截 --><error-page><error-code>500</error-code><location>/error500.jsp</location></error-page><!-- 加載spring容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/spring/applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置前端控制器 --><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><!-- ContextconfigLocation配置springmvc加載的配置文件適配器、處理映射器等--><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/spring/springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>spring</servlet-name><!-- 1、.action訪問(wèn)以.action結(jié)尾的 由DispatcherServlet進(jìn)行解析2、/,所有訪問(wèn)都由DispatcherServlet進(jìn)行解析--><url-pattern>/</url-pattern></servlet-mapping><!-- 解決post亂碼問(wèn)題的過(guò)濾器 --><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></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>welcome.jsp</welcome-file></welcome-file-list> </web-app> <web-app version="3.0" 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"><display-name></display-name> <!-- 404錯(cuò)誤攔截 --><error-page><error-code>404</error-code><location>/error404.jsp</location></error-page><!-- 500錯(cuò)誤攔截 --><error-page><error-code>500</error-code><location>/error500.jsp</location></error-page><!-- 加載spring容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/spring/applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置前端控制器 --><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><!-- ContextconfigLocation配置springmvc加載的配置文件適配器、處理映射器等--><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/spring/springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>spring</servlet-name><!-- 1、.action訪問(wèn)以.action結(jié)尾的 由DispatcherServlet進(jìn)行解析2、/,所有訪問(wèn)都由DispatcherServlet進(jìn)行解析--><url-pattern>/</url-pattern></servlet-mapping><!-- 解決post亂碼問(wèn)題的過(guò)濾器 --><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></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>welcome.jsp</welcome-file></welcome-file-list> </web-app>看到配置文件中多了兩塊內(nèi)容,一個(gè)是error page是用來(lái)友好的處理錯(cuò)誤的,可以使用錯(cuò)誤代碼來(lái)區(qū)別并跳轉(zhuǎn)到相應(yīng)的處理頁(yè)面。這段配置代碼最好放到最前面,在前端控制器攔截之前處理。
還有一塊內(nèi)容是一個(gè)解決post亂碼問(wèn)題的過(guò)濾器,攔截post請(qǐng)求并編碼為utf8。
springmvc.xml的配置 ??
視圖解析器的配置:
<!-- 配置視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 使用前綴和后綴 --><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property> </bean><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 使用前綴和后綴 --><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property> </bean>在Controller中設(shè)置視圖名的時(shí)候會(huì)自動(dòng)加上前綴和后綴。
Controller的配置
自動(dòng)掃描方式,掃描包下面所有的Controller,可以使用注解來(lái)指定訪問(wèn)路徑。
<!-- 使用組件掃描的方式可以一次掃描多個(gè)Controller --> <context:component-scan base-package="com.wxisme.ssm.controller"> <context:component-scan base-package="com.wxisme.ssm.controller">也可以使用單個(gè)的配置方式,需要指定Controller的全限定名。
<span style="color:#000080"><bean <span style="color:#008080">name</span>=<span style="color:#dd1144">"/queryUser.action"</span> <span style="color:#008080">class</span>=<span style="color:#dd1144">"com.wxisme.ssm.controller.Controller1"</span>/></span>配置注解的處理器適配器和處理器映射器:
<!-- 注解的處理器適配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 注解的處理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 注解的處理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>也可以使用下面的簡(jiǎn)化配置:
<!-- 配置注解的處理器映射器和處理器適配器 --> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>配置攔截器,可以直接定義攔截所有請(qǐng)求,也可以自定義攔截路徑。
<mvc:interceptors><!-- 直接定義攔截所有請(qǐng)求 --><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean><!-- <mvc:interceptor>攔截所有路徑的請(qǐng)求 包括子路徑<mvc:mapping path="/**"/><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean></mvc:interceptor> --></mvc:interceptors><!-- 直接定義攔截所有請(qǐng)求 --><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean><!-- <mvc:interceptor>攔截所有路徑的請(qǐng)求 包括子路徑<mvc:mapping path="/**"/><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean></mvc:interceptor> --></mvc:interceptors>配置全局異常處理器
<!-- 定義全局異常處理器 --><!-- 只有一個(gè)全局異常處理器起作用 --><bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean><!-- 只有一個(gè)全局異常處理器起作用 --><bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean>配置文件上傳數(shù)據(jù)解析器,在上傳文件時(shí)需要配置。
<!--配置上傳文件數(shù)據(jù)解析器 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize"><value>9242880</value></property></bean><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize"><value>9242880</value></property></bean>還可以配置一些自定義的參數(shù)類型,以日期類型綁定為例。
<!-- 自定義參數(shù)類型綁定 --><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><list><!-- 日期類型綁定 --><bean class="com.wxisme.ssm.controller.converter.DateConverter"/></list></property></bean><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><list><!-- 日期類型綁定 --><bean class="com.wxisme.ssm.controller.converter.DateConverter"/></list></property></bean>上面提到過(guò)如果在配置前端控制器時(shí)攔截了所有的請(qǐng)求,不做特殊處理就會(huì)導(dǎo)致部分靜態(tài)資源無(wú)法使用。如果是這種情況就可以使用下面的配置來(lái)訪問(wèn)靜態(tài)資源文件。
<mvc:resources mapping="/images/**" location="/images/" /> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/imgdata/**" location="/imgdata/" /> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/imgdata/**" location="/imgdata/" />也可以使用默認(rèn),但是需要在web.xml中配置。
<!-- 訪問(wèn)靜態(tài)資源文件 --><!-- <mvc:default-servlet-handler/> 需要在web.xml中配置--><!-- <mvc:default-servlet-handler/> 需要在web.xml中配置-->完全可以不攔截所有路徑,大可避免這個(gè)問(wèn)題的發(fā)生。
完整的配置大概是這樣的,需要注意xml文件的命名空間,有時(shí)候會(huì)有影響的。
<?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" 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"><!-- 配置視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 使用前綴和后綴 --><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean><!-- 使用組件掃描的方式可以一次掃描多個(gè)Controller --><context:component-scan base-package="com.wxisme.ssm.controller"></context:component-scan><!-- 配置注解的處理器映射器和處理器適配器 --><mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven><!-- 自定義參數(shù)類型綁定 --><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><list><!-- 日期類型綁定 --><bean class="com.wxisme.ssm.controller.converter.DateConverter"/></list></property></bean><!-- 訪問(wèn)靜態(tài)資源文件 --><!-- <mvc:default-servlet-handler/> 需要在web.xml中配置--><mvc:resources mapping="/images/**" location="/images/" /><mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/js/**" location="/js/" /><mvc:resources mapping="/imgdata/**" location="/imgdata/" /><!-- 定義攔截器 --><mvc:interceptors><!-- 直接定義攔截所有請(qǐng)求 --><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean><!-- <mvc:interceptor>攔截所有路徑的請(qǐng)求 包括子路徑<mvc:mapping path="/**"/><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean></mvc:interceptor> --></mvc:interceptors><!--配置上傳文件數(shù)據(jù)解析器 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize"><value>9242880</value></property></bean><!-- 定義全局異常處理器 --><!-- 只有一個(gè)全局異常處理器起作用 --><bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean></beans> <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" 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"><!-- 配置視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 使用前綴和后綴 --><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean><!-- 使用組件掃描的方式可以一次掃描多個(gè)Controller --><context:component-scan base-package="com.wxisme.ssm.controller"></context:component-scan><!-- 配置注解的處理器映射器和處理器適配器 --><mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven><!-- 自定義參數(shù)類型綁定 --><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><list><!-- 日期類型綁定 --><bean class="com.wxisme.ssm.controller.converter.DateConverter"/></list></property></bean><!-- 訪問(wèn)靜態(tài)資源文件 --><!-- <mvc:default-servlet-handler/> 需要在web.xml中配置--><mvc:resources mapping="/images/**" location="/images/" /><mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/js/**" location="/js/" /><mvc:resources mapping="/imgdata/**" location="/imgdata/" /><!-- 定義攔截器 --><mvc:interceptors><!-- 直接定義攔截所有請(qǐng)求 --><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean><!-- <mvc:interceptor>攔截所有路徑的請(qǐng)求 包括子路徑<mvc:mapping path="/**"/><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean></mvc:interceptor> --></mvc:interceptors><!--配置上傳文件數(shù)據(jù)解析器 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize"><value>9242880</value></property></bean><!-- 定義全局異常處理器 --><!-- 只有一個(gè)全局異常處理器起作用 --><bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean></beans>applicationContext-*.xml的配置 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
applicationContext-*.xml包括三個(gè)配置文件,分別對(duì)應(yīng)數(shù)據(jù)層控制、業(yè)務(wù)邏輯service控制和事務(wù)的控制。
數(shù)據(jù)訪問(wèn)層的控制,applicationContext-dao.xml的配置:
配置加載數(shù)據(jù)連接資源文件的配置,把數(shù)據(jù)庫(kù)連接數(shù)據(jù)抽取到一個(gè)properties資源文件中方便管理。
配置為:
<!-- 加載數(shù)據(jù)庫(kù)連接的資源文件 --> <context:property-placeholder location="/WEB-INF/classes/jdbc.properties"/> <context:property-placeholder location="/WEB-INF/classes/jdbc.properties"/>其中jdbc.properties文件的內(nèi)容如下:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/database jdbc.username=root jdbc.password=1234//localhost:3306/database jdbc.username=root jdbc.password=1234配置數(shù)據(jù)庫(kù)連接池,這里使用的是dbcp,別忘了添加jar包!
<!-- 配置數(shù)據(jù)源 dbcp數(shù)據(jù)庫(kù)連接池 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"><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> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"><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>Spring和MyBatis整合配置,jar包由MyBatis提供。
配置sqlSessionFactory
<!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 數(shù)據(jù)庫(kù)連接池 --><property name="dataSource" ref="dataSource"/><!-- 加載Mybatis全局配置文件 --><property name="configLocation" value="/WEB-INF/classes/mybatis/SqlMapConfig.xml"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 數(shù)據(jù)庫(kù)連接池 --><property name="dataSource" ref="dataSource"/><!-- 加載Mybatis全局配置文件 --><property name="configLocation" value="/WEB-INF/classes/mybatis/SqlMapConfig.xml"/> </bean>SqlMapConfig.xml文件是MyBatis的配置文件,后面會(huì)提到。
配置Mapper掃描器,掃描mapper包下的所有mapper文件和類,要求mapper配置文件和類名需要一致。
<!-- 配置mapper掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 掃描包路徑,如果需要掃描多個(gè)包中間用半角逗號(hào)隔開(kāi) --><property name="basePackage" value="com.wxisme.ssm.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 掃描包路徑,如果需要掃描多個(gè)包中間用半角逗號(hào)隔開(kāi) --><property name="basePackage" value="com.wxisme.ssm.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>整個(gè)applicationContext-dao.xml配置文件應(yīng)該是這樣的:
<?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" 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"> <!-- 加載數(shù)據(jù)庫(kù)連接的資源文件 --> <context:property-placeholder location="/WEB-INF/classes/jdbc.properties"/> <!-- 配置數(shù)據(jù)源 dbcp數(shù)據(jù)庫(kù)連接池 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"><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> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 數(shù)據(jù)庫(kù)連接池 --><property name="dataSource" ref="dataSource"/><!-- 加載Mybatis全局配置文件 --><property name="configLocation" value="/WEB-INF/classes/mybatis/SqlMapConfig.xml"/> </bean> <!-- 配置mapper掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 掃描包路徑,如果需要掃描多個(gè)包中間用半角逗號(hào)隔開(kāi) --><property name="basePackage" value="com.wxisme.ssm.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans> <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" 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"> <!-- 加載數(shù)據(jù)庫(kù)連接的資源文件 --> <context:property-placeholder location="/WEB-INF/classes/jdbc.properties"/> <!-- 配置數(shù)據(jù)源 dbcp數(shù)據(jù)庫(kù)連接池 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"><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> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 數(shù)據(jù)庫(kù)連接池 --><property name="dataSource" ref="dataSource"/><!-- 加載Mybatis全局配置文件 --><property name="configLocation" value="/WEB-INF/classes/mybatis/SqlMapConfig.xml"/> </bean> <!-- 配置mapper掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 掃描包路徑,如果需要掃描多個(gè)包中間用半角逗號(hào)隔開(kāi) --><property name="basePackage" value="com.wxisme.ssm.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>業(yè)務(wù)邏輯控制,applicationContext-service.xml的配置:
這個(gè)文件里暫時(shí)只需要定義service的實(shí)現(xiàn)類即可。
<!-- 定義service --> <bean id="userService" class="com.wxisme.ssm.service.impl.UserServiceImpl"/> <bean id="userService" class="com.wxisme.ssm.service.impl.UserServiceImpl"/>事務(wù)控制,applicationContext-transaction.xml的配置
配置數(shù)據(jù)源,使用JDBC控制類。
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 配置數(shù)據(jù)源 --><property name="dataSource" ref="dataSource"/> </bean><!-- 配置數(shù)據(jù)源 --><property name="dataSource" ref="dataSource"/> </bean>配置通知,事務(wù)控制。
<!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 傳播行為 --><tx:method name="save*" propagation="REQUIRED"/><tx:method name="insert*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/><tx:method name="select*" propagation="SUPPORTS" read-only="true"/></tx:attributes> </tx:advice> <tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 傳播行為 --><tx:method name="save*" propagation="REQUIRED"/><tx:method name="insert*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/><tx:method name="select*" propagation="SUPPORTS" read-only="true"/></tx:attributes> </tx:advice>配置AOP切面
<!-- 配置aop --><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wxisme.ssm.service.impl.*.*(..))"/></aop:config><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wxisme.ssm.service.impl.*.*(..))"/></aop:config>整個(gè)事務(wù)控制的配置是這樣的:
<?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" 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"> <!-- 事務(wù)控制 對(duì)MyBatis操作數(shù)據(jù)庫(kù) spring使用JDBC事務(wù)控制類 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 配置數(shù)據(jù)源 --><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="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/><tx:method name="select*" propagation="SUPPORTS" read-only="true"/></tx:attributes></tx:advice><!-- 配置aop --><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wxisme.ssm.service.impl.*.*(..))"/></aop:config> </beans> <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" 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"> <!-- 事務(wù)控制 對(duì)MyBatis操作數(shù)據(jù)庫(kù) spring使用JDBC事務(wù)控制類 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 配置數(shù)據(jù)源 --><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="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/><tx:method name="select*" propagation="SUPPORTS" read-only="true"/></tx:attributes></tx:advice><!-- 配置aop --><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wxisme.ssm.service.impl.*.*(..))"/></aop:config> </beans>MyBatis的配置 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
SqlMapConfig.xml的配置 ???全局setting配置這里省略,數(shù)據(jù)庫(kù)連接池在spring整合文件中已經(jīng)配置,具體setting配置參考官方文檔。
別名的定義:
<typeAliases><!-- 批量定義別名 ,指定包名,自動(dòng)掃描包中的類,別名即為類名,首字母大小寫無(wú)所謂--><package name="com.wxisme.ssm.po"/> </typeAliases><!-- 批量定義別名 ,指定包名,自動(dòng)掃描包中的類,別名即為類名,首字母大小寫無(wú)所謂--><package name="com.wxisme.ssm.po"/> </typeAliases>mapper映射文件的配置:
<mappers><!-- 加載映射文件 --><!-- 這里也可以使用class來(lái)加載映射文件,前提是:使用mapper代理的方法,遵循規(guī)范,并且兩個(gè)文件必須同名且在同一目錄<mapper class="com.wxisme.mybatis0100.mapper.UserMapper"/>基于class加載,可以進(jìn)行批量加載--><!-- 通過(guò)掃描包的方式來(lái)進(jìn)行批量加載映射文件 --><package name="com.wxisme.ssm.mapper"/> </mappers> <!-- 加載映射文件 --><!-- 這里也可以使用class來(lái)加載映射文件,前提是:使用mapper代理的方法,遵循規(guī)范,并且兩個(gè)文件必須同名且在同一目錄<mapper class="com.wxisme.mybatis0100.mapper.UserMapper"/>基于class加載,可以進(jìn)行批量加載--><!-- 通過(guò)掃描包的方式來(lái)進(jìn)行批量加載映射文件 --><package name="com.wxisme.ssm.mapper"/> </mappers>整個(gè)文件的配置應(yīng)該是這樣的:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 將數(shù)據(jù)庫(kù)連接數(shù)據(jù)抽取到屬性文件中方便測(cè)試 --> <!-- <properties resource="/WEB-INF/classes/jdbc.properties"></properties> --> <!-- 別名的定義 --> <typeAliases><!-- 批量定義別名 ,指定包名,自動(dòng)掃描包中的類,別名即為類名,首字母大小寫無(wú)所謂--><package name="com.wxisme.ssm.po"/> </typeAliases> <!-- 數(shù)據(jù)庫(kù)連接用數(shù)據(jù)庫(kù)連接池 --> <mappers><!-- 通過(guò)掃描包的方式來(lái)進(jìn)行批量加載映射文件 --><package name="com.wxisme.ssm.mapper"/> </mappers> </configuration> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 將數(shù)據(jù)庫(kù)連接數(shù)據(jù)抽取到屬性文件中方便測(cè)試 --> <!-- <properties resource="/WEB-INF/classes/jdbc.properties"></properties> --> <!-- 別名的定義 --> <typeAliases><!-- 批量定義別名 ,指定包名,自動(dòng)掃描包中的類,別名即為類名,首字母大小寫無(wú)所謂--><package name="com.wxisme.ssm.po"/> </typeAliases> <!-- 數(shù)據(jù)庫(kù)連接用數(shù)據(jù)庫(kù)連接池 --> <mappers><!-- 通過(guò)掃描包的方式來(lái)進(jìn)行批量加載映射文件 --><package name="com.wxisme.ssm.mapper"/> </mappers> </configuration>具體mapper文件的配置,在使用mapper代理的方法時(shí),命名空間需要是對(duì)應(yīng)的Mapper類。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.wxisme.ssm.mapper.AlbumMapper" ></mapper> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.wxisme.ssm.mapper.AlbumMapper" ></mapper>?
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Spring、Spring MVC、MyBatis整合文件配置详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ASP.NET获取任意网页HTML代码
- 下一篇: JSP/Servlet中的汉字编码问题