Spring、Spring MVC、MyBatis整合文件配置詳解

?

前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。

web.xml的配置 ?

web.xml應該是整個項目最重要的配置文件了,不過servlet3.0中已經支持注解配置方式了。在servlet3.0以前每個servlet必須要在web.xml中配置servlet及其映射關系。但是在spring框架中就不用了,因為Spring中是依賴注入(Dependency Injection)的也叫控制反轉(Inversion of Control)。但是也要配置一個重要的servlet,就是前端控制器(DispatcherServlet)。配置方式與普通的servlet基本相似。

配置內容如下:

<!-- 配置前端控制器 -->
<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訪問以.action結尾的  由DispatcherServlet進行解析2、/,所有訪問都由DispatcherServlet進行解析--><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訪問以.action結尾的  由DispatcherServlet進行解析2、/,所有訪問都由DispatcherServlet進行解析--><url-pattern>/</url-pattern>
</servlet-mapping>

這里需要注意,springmvc.xml是spring配置文件,將在后面討論。在<servlet-mapping>中url如果是.action,前端控制器就只會攔截以.action結尾的請求,并不會理會靜態的文件。對靜態頁面的控制就要通過其他的手段。以/作為url的話就會攔截所有的請求,包括靜態頁面的請求。這樣的話就可以攔截任何想要處理的請求,但是有一個問題。如果攔截了所有的請求,如果不在攔截器中做出相應的處理那么所有靜態的js、css以及頁面中用到的圖片就會訪問不到造成頁面無法正常顯示。但這可以通過靜態資源的配置來解決這個問題。后面會提到。

配置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個配置文件,是springIoC容器的具體配置。后面會提到。

配置一個監聽器:

<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錯誤攔截 --><error-page><error-code>404</error-code><location>/error404.jsp</location></error-page><!-- 500錯誤攔截 --><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訪問以.action結尾的  由DispatcherServlet進行解析2、/,所有訪問都由DispatcherServlet進行解析--><url-pattern>/</url-pattern></servlet-mapping><!-- 解決post亂碼問題的過濾器 --><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錯誤攔截 --><error-page><error-code>404</error-code><location>/error404.jsp</location></error-page><!-- 500錯誤攔截 --><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訪問以.action結尾的  由DispatcherServlet進行解析2、/,所有訪問都由DispatcherServlet進行解析--><url-pattern>/</url-pattern></servlet-mapping><!-- 解決post亂碼問題的過濾器 --><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>

看到配置文件中多了兩塊內容,一個是error page是用來友好的處理錯誤的,可以使用錯誤代碼來區別并跳轉到相應的處理頁面。這段配置代碼最好放到最前面,在前端控制器攔截之前處理。

還有一塊內容是一個解決post亂碼問題的過濾器,攔截post請求并編碼為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中設置視圖名的時候會自動加上前綴和后綴。

Controller的配置

自動掃描方式,掃描包下面所有的Controller,可以使用注解來指定訪問路徑。

<!-- 使用組件掃描的方式可以一次掃描多個Controller -->
<context:component-scan base-package="com.wxisme.ssm.controller">
<context:component-scan base-package="com.wxisme.ssm.controller">

也可以使用單個的配置方式,需要指定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"/>

也可以使用下面的簡化配置:

<!-- 配置注解的處理器映射器和處理器適配器 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

配置攔截器,可以直接定義攔截所有請求,也可以自定義攔截路徑。

<mvc:interceptors><!-- 直接定義攔截所有請求 --><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean><!-- <mvc:interceptor>攔截所有路徑的請求   包括子路徑<mvc:mapping path="/**"/><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean></mvc:interceptor> --></mvc:interceptors><!-- 直接定義攔截所有請求 --><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean><!-- <mvc:interceptor>攔截所有路徑的請求   包括子路徑<mvc:mapping path="/**"/><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean></mvc:interceptor> --></mvc:interceptors>

配置全局異常處理器

<!-- 定義全局異常處理器 --><!-- 只有一個全局異常處理器起作用 --><bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean><!-- 只有一個全局異常處理器起作用 --><bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean>

配置文件上傳數據解析器,在上傳文件時需要配置。

<!--配置上傳文件數據解析器  --><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>

還可以配置一些自定義的參數類型,以日期類型綁定為例。

<!-- 自定義參數類型綁定 --><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>

上面提到過如果在配置前端控制器時攔截了所有的請求,不做特殊處理就會導致部分靜態資源無法使用。如果是這種情況就可以使用下面的配置來訪問靜態資源文件。

<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/" />

也可以使用默認,但是需要在web.xml中配置。

<!-- 訪問靜態資源文件 --><!-- <mvc:default-servlet-handler/> 需要在web.xml中配置--><!-- <mvc:default-servlet-handler/> 需要在web.xml中配置-->

完全可以不攔截所有路徑,大可避免這個問題的發生。

完整的配置大概是這樣的,需要注意xml文件的命名空間,有時候會有影響的。

<?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><!-- 使用組件掃描的方式可以一次掃描多個Controller --><context:component-scan base-package="com.wxisme.ssm.controller"></context:component-scan><!-- 配置注解的處理器映射器和處理器適配器 --><mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven><!-- 自定義參數類型綁定 --><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><list><!-- 日期類型綁定 --><bean class="com.wxisme.ssm.controller.converter.DateConverter"/></list></property></bean><!-- 訪問靜態資源文件 --><!-- <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><!-- 直接定義攔截所有請求 --><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean><!-- <mvc:interceptor>攔截所有路徑的請求   包括子路徑<mvc:mapping path="/**"/><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean></mvc:interceptor> --></mvc:interceptors><!--配置上傳文件數據解析器  --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize"><value>9242880</value></property></bean><!-- 定義全局異常處理器 --><!-- 只有一個全局異常處理器起作用 --><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><!-- 使用組件掃描的方式可以一次掃描多個Controller --><context:component-scan base-package="com.wxisme.ssm.controller"></context:component-scan><!-- 配置注解的處理器映射器和處理器適配器 --><mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven><!-- 自定義參數類型綁定 --><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><list><!-- 日期類型綁定 --><bean class="com.wxisme.ssm.controller.converter.DateConverter"/></list></property></bean><!-- 訪問靜態資源文件 --><!-- <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><!-- 直接定義攔截所有請求 --><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean><!-- <mvc:interceptor>攔截所有路徑的請求   包括子路徑<mvc:mapping path="/**"/><bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean></mvc:interceptor> --></mvc:interceptors><!--配置上傳文件數據解析器  --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize"><value>9242880</value></property></bean><!-- 定義全局異常處理器 --><!-- 只有一個全局異常處理器起作用 --><bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean></beans>

applicationContext-*.xml的配置 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

applicationContext-*.xml包括三個配置文件,分別對應數據層控制、業務邏輯service控制和事務的控制。

數據訪問層的控制,applicationContext-dao.xml的配置:

配置加載數據連接資源文件的配置,把數據庫連接數據抽取到一個properties資源文件中方便管理。

配置為:

<!-- 加載數據庫連接的資源文件 -->
<context:property-placeholder location="/WEB-INF/classes/jdbc.properties"/>
<context:property-placeholder location="/WEB-INF/classes/jdbc.properties"/>

其中jdbc.properties文件的內容如下:

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

配置數據庫連接池,這里使用的是dbcp,別忘了添加jar包!

<!-- 配置數據源   dbcp數據庫連接池 -->
<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"><!-- 數據庫連接池 --><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"><!-- 數據庫連接池 --><property name="dataSource" ref="dataSource"/><!-- 加載Mybatis全局配置文件 --><property name="configLocation" value="/WEB-INF/classes/mybatis/SqlMapConfig.xml"/>
</bean>

SqlMapConfig.xml文件是MyBatis的配置文件,后面會提到。

配置Mapper掃描器,掃描mapper包下的所有mapper文件和類,要求mapper配置文件和類名需要一致。

<!-- 配置mapper掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 掃描包路徑,如果需要掃描多個包中間用半角逗號隔開 --><property name="basePackage" value="com.wxisme.ssm.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 掃描包路徑,如果需要掃描多個包中間用半角逗號隔開 --><property name="basePackage" value="com.wxisme.ssm.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

整個applicationContext-dao.xml配置文件應該是這樣的:

<?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">
<!-- 加載數據庫連接的資源文件 -->
<context:property-placeholder location="/WEB-INF/classes/jdbc.properties"/>
<!-- 配置數據源   dbcp數據庫連接池 -->
<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"><!-- 數據庫連接池 --><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"><!-- 掃描包路徑,如果需要掃描多個包中間用半角逗號隔開 --><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">
<!-- 加載數據庫連接的資源文件 -->
<context:property-placeholder location="/WEB-INF/classes/jdbc.properties"/>
<!-- 配置數據源   dbcp數據庫連接池 -->
<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"><!-- 數據庫連接池 --><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"><!-- 掃描包路徑,如果需要掃描多個包中間用半角逗號隔開 --><property name="basePackage" value="com.wxisme.ssm.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>

業務邏輯控制,applicationContext-service.xml的配置:

這個文件里暫時只需要定義service的實現類即可。

<!-- 定義service -->
<bean id="userService" class="com.wxisme.ssm.service.impl.UserServiceImpl"/>
<bean id="userService" class="com.wxisme.ssm.service.impl.UserServiceImpl"/>

事務控制,applicationContext-transaction.xml的配置

配置數據源,使用JDBC控制類。

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 配置數據源 --><property name="dataSource" ref="dataSource"/>
</bean><!-- 配置數據源 --><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>

<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>

整個事務控制的配置是這樣的:

<?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">
<!-- 事務控制  對MyBatis操作數據庫  spring使用JDBC事務控制類 -->
<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="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">
<!-- 事務控制  對MyBatis操作數據庫  spring使用JDBC事務控制類 -->
<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="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配置這里省略,數據庫連接池在spring整合文件中已經配置,具體setting配置參考官方文檔。

別名的定義:

<typeAliases><!-- 批量定義別名 ,指定包名,自動掃描包中的類,別名即為類名,首字母大小寫無所謂--><package name="com.wxisme.ssm.po"/>
</typeAliases><!-- 批量定義別名 ,指定包名,自動掃描包中的類,別名即為類名,首字母大小寫無所謂--><package name="com.wxisme.ssm.po"/>
</typeAliases>

mapper映射文件的配置:

<mappers><!-- 加載映射文件 --><!-- 這里也可以使用class來加載映射文件,前提是:使用mapper代理的方法,遵循規范,并且兩個文件必須同名且在同一目錄<mapper class="com.wxisme.mybatis0100.mapper.UserMapper"/>基于class加載,可以進行批量加載--><!-- 通過掃描包的方式來進行批量加載映射文件 --><package name="com.wxisme.ssm.mapper"/>
</mappers>
<!-- 加載映射文件 --><!-- 這里也可以使用class來加載映射文件,前提是:使用mapper代理的方法,遵循規范,并且兩個文件必須同名且在同一目錄<mapper class="com.wxisme.mybatis0100.mapper.UserMapper"/>基于class加載,可以進行批量加載--><!-- 通過掃描包的方式來進行批量加載映射文件 --><package name="com.wxisme.ssm.mapper"/>
</mappers>

整個文件的配置應該是這樣的:

<?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>
<!-- 將數據庫連接數據抽取到屬性文件中方便測試 -->
<!-- <properties resource="/WEB-INF/classes/jdbc.properties"></properties> -->
<!-- 別名的定義 -->
<typeAliases><!-- 批量定義別名 ,指定包名,自動掃描包中的類,別名即為類名,首字母大小寫無所謂--><package name="com.wxisme.ssm.po"/>
</typeAliases>
<!-- 數據庫連接用數據庫連接池 -->
<mappers><!-- 通過掃描包的方式來進行批量加載映射文件 --><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>
<!-- 將數據庫連接數據抽取到屬性文件中方便測試 -->
<!-- <properties resource="/WEB-INF/classes/jdbc.properties"></properties> -->
<!-- 別名的定義 -->
<typeAliases><!-- 批量定義別名 ,指定包名,自動掃描包中的類,別名即為類名,首字母大小寫無所謂--><package name="com.wxisme.ssm.po"/>
</typeAliases>
<!-- 數據庫連接用數據庫連接池 -->
<mappers><!-- 通過掃描包的方式來進行批量加載映射文件 --><package name="com.wxisme.ssm.mapper"/>
</mappers>
</configuration>

具體mapper文件的配置,在使用mapper代理的方法時,命名空間需要是對應的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>

?

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/452217.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/452217.shtml
英文地址,請注明出處:http://en.pswp.cn/news/452217.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

19.C++-(=)賦值操作符、初步編寫智能指針

()賦值操作符 編譯器為每個類默認重載了()賦值操作符默認的()賦值操作符僅完成淺拷貝默認的賦值操作符和默認的拷貝構造函數有相同的存在意義()賦值操作符注意事項 首先要判斷兩個操作數是否相等 返回值一定是 return *this; 返回類型是Type&型,避免連續使用后,出現bug 比如…

windows mysqldump 不成功 1049 1064 報錯

1064 路徑不對&#xff0c;需要cd選到mysql bin目錄下 1049 在cmd里面不需要分號 以下是正確的 E:\phpStudy\PHPTutorial\MySQL\bin>mysqldump -uroot -proot db >db.sql 轉載于:https://www.cnblogs.com/JANCHAN/p/9227388.html

學成在線--14.使用RabbitMQ完成頁面發布

文章目錄一.技術方案二.頁面發布——消費方1.需求分析2.創建Cms Client工程1&#xff09;創建maven工程2&#xff09;配置文件3&#xff09;啟動類3.RabbitmqConfig配置類4.定義消息格式5.PageDao1&#xff09;使用CmsPageRepository 查詢頁面信息2&#xff09;使用CmsSiteRepo…

對象模型中類與類間的關系

類與類之間通常有關聯、聚集、泛化(繼承)、依賴和細化4種關系 1.關聯 關聯表示兩個類的對象之間存在某種語義上的聯系。 (1) 普通關聯 只要在類與類之間存在連接關系就可以用普通關聯表示。普通關聯的圖示符號是連接兩個類之間的直線&#xff0c;如下圖所示。關聯…

記憶講師石偉華微信公眾號2017所有文章匯總(待更新)

17-10-24-不勝光榮的記憶 17-10-26-每日一個超長英文單詞&#xff08;2&#xff09; 17-10-27-每日一個超長英文單詞&#xff08;3&#xff09; 17-10-28-每日一個超長英文單詞&#xff08;4&#xff09; 轉載于:https://www.cnblogs.com/bakblog/p/9228096.html

Log4J日志配置詳解

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 一、Log4j簡介 Log4j有三個主要的組件&#xff1a;Loggers(記錄器)&#xff0c;Appenders (輸出源)和Layouts(布局)。這里可簡單理解為日…

中文編碼雜談

編碼問題的例子 在windows自帶的notepad&#xff08;記事本&#xff09;程序中輸入“聯通”兩個字&#xff0c;保存后再次打開&#xff0c;會發現“聯通”不見了&#xff0c;代之以“”的亂碼。這是windows平臺上典型的中文編碼問題。即文件保存的時候是按照ANSI編碼&#xff…

Java NIO (十四)NIO 和 IO 的區別和適用場景分析

在研究Java NIO和IO API時&#xff0c;很快就會想到一個問題&#xff1a; 什么時候應該使用IO&#xff0c;什么時候應該使用NIO&#xff1f; 在本文中&#xff0c;我將嘗試闡明Java NIO和IO之間的區別&#xff0c;它們的用例以及它們如何影響代碼的設計。 ###Java NIO和IO之間的…

面向對象三種模型之間的關系

功能模型指明了系統應該“做什么”&#xff1b;動態模型明確規定了什么時候(即在何種狀態下接受了什么事件的觸發)做&#xff1b;對象模型則定義了做事情的實體。在面向對象方法學中&#xff0c;對象模型是最基本最重要的&#xff0c;它為其他兩種模型奠定了基礎&#xff0c;人…

android node

pkg install nodejs-current轉載于:https://www.cnblogs.com/insight0912/p/9231342.html

springmvc 中@Controller和@RestController的區別

1.Controller, RestController的共同點 都是用來表示Spring某個類的是否可以接收HTTP請求 2.Controller, RestController的不同點 Controller標識一個Spring類是Spring MVC controller處理器 RestController&#xff1a; a convenience annotation that does nothing more …

easyUI 日期控件修改...

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 個人覺得easyUI挺好用的。 它的中文文檔地址&#xff1a; http://www.zi-han.net/case/easyui/ 日期本來效果是這樣的&#xff1a; 改…

面向對象分析的三個模型與5個層次

在面向對象分析中&#xff0c;主要由對象模型、動態模型和功能模型組成。對象模型是最基本、最重要、最核心的。 面向對象建模得到的模型包含系統的3個要素&#xff0c;即靜態結構(對象模型)、交互次序(動態模型)和數據變換(功能模型)。解決的問題不同&#xff0c;這3個子模型…

學成在線--15.課程計劃查詢

文章目錄一.需求分析二.頁面原型1.tree組件介紹2.webstorm配置jsx三.API接口1.數據模型2.自定義模型類3.接口定義四.sql語句五.服務器端1.Dao1&#xff09;Mapper接口2&#xff09;Mapper映射文件2.Service3.Controller4.測試六.前端1.Api方法2.Api調用1&#xff09;定義查詢課…

團隊作業-項目答辯

1. 王書磊 1600802063 http://www.cnblogs.com/wsl-1117/ 劉令斌 1600802017 http://www.cnblogs.com/liulingbin/ 許浩然 1600802066 https://www.cnblogs.com/xuhaoran1/ 成明龍 1600802038 http://www.cnblogs.com/CMLCML/ 2這是我們的效果圖. 3.&#xff08;1&#xff09;修…

Java構造和解析Json數據的兩種方法詳解一

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 在www.json.org上公布了很多JAVA下的json構造和解析工具&#xff0c;其中org.json和json-lib比較簡單&#xff0c;兩者使用上差不多但還是…

面向對象方法開發的方法

面向對象分析首要的工作&#xff0c;是建立問題域的對象模型。 這個模型描述了現實世界中的“類與對象”以及它們之間的關系&#xff0c;表示了目標系統的靜態數據結構。靜態數據結構對應用細節依賴較少&#xff0c;比較容易確定。因此&#xff0c;用面向對象方法開發絕大多數…

程序員編程需要多少個小時?

Michael Arrington曾發表一篇博文說&#xff0c;創業者必須加倍的努力工作&#xff0c;甚至不惜趴在辦公桌上睡覺&#xff0c;這樣才能成功。對此&#xff0c;我并不贊同其觀點&#xff0c;我看了很多評論都是關于這樣工作會適得其反&#xff0c;不但沒有獲得成功&#xff0c;相…

事務以及@Transcational注解

文章目錄1.事務的概念2.事務的四個特性3.關于Transcational注解的理解4.使用場景5.舉例6.編程式事務管理7.相關知識1.事務的概念 事務&#xff0c;是指作為單個邏輯工作單元執行的一系列操作&#xff0c;結果只有成功和失敗兩種&#xff0c;要么全部成功(全部提交)&#xff0c…

提高代碼復用率

由于現在的互聯網企業業務比較繁忙&#xff0c;導致產品狗不停地提需求&#xff0c;還總是改來改去&#xff0c;最后留給程序猿的時間少之又少。程序猿也不是吃素的&#xff0c;干脆直接copy一下代碼隨便搞一下實現功能就行&#xff0c;也談不上所謂的精心設計了。這樣的確是縮…