首页 > 编程语言 > 详细

Spring SpringMvc 3.0 + MyBatis 整合

时间:2015-01-05 02:05:54      阅读:341      评论:0      收藏:0      [点我收藏+]

一、使用的jar包就不详细讲解了,下载了Mybatis 和 Spring 的jar包基本上都添加上去了、

一图概括:(这是我使用的ar包,有些不是Mybatis 和 Spring 的 ) bubuko.com,布布扣

?

二、 web.xml配置文件

  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <web-app?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  3. ????xmlns="http://java.sun.com/xml/ns/javaee"?xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"??
  4. ????xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"??
  5. ????id="WebApp_ID"?version="2.5">??
  6. ????<display-name>WeShare</display-name>??
  7. ????<welcome-file-list>??
  8. ????????<welcome-file>/jumper.html</welcome-file>??
  9. ????</welcome-file-list>??
  10. ????<!--?加载spring容器配置?-->??
  11. ????<listener>??
  12. ????????<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>??
  13. ????</listener>??
  14. ????<!--?设置Spring容器加载配置文件路径?(主要配置都在这里面)?-->??
  15. ????<context-param>??
  16. ????????<param-name>contextConfigLocation</param-name>??
  17. ????????<param-value>/WEB-INF/applicationContext.xml??
  18. ??</param-value>??
  19. ????</context-param>??
  20. ??
  21. ????<!--?配置Spring核心控制器?-->??
  22. ????<servlet>??
  23. ????????<servlet-name>web</servlet-name>??
  24. ????????<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>??
  25. ????????<!--?不指定?<init-param>?会自动找web.xml相同路径下?web-servlet.xml文件?-->??
  26. ????????<!--???
  27. ????????<init-param>???
  28. ????????????<param-name>contextConfigLocation</param-name>???
  29. ????????????<param-value>/WEB-INF/dispatcher.xml</param-value>???
  30. ????????</init-param>???
  31. ????????????-->??
  32. ????????<load-on-startup>1</load-on-startup>??
  33. ????</servlet>??
  34. ????<servlet-mapping>??
  35. ????????<servlet-name>web</servlet-name>??
  36. ????????<url-pattern>*.do</url-pattern>??
  37. ????</servlet-mapping>??
  38. ????<servlet-mapping>??
  39. ????????<servlet-name>web</servlet-name>??
  40. ????????<url-pattern>*.action</url-pattern>??
  41. ????</servlet-mapping>??
  42. ??
  43. ??
  44. ????<!--?解决工程编码过滤器?-->??
  45. ????<filter>??
  46. ????????<filter-name>characterEncodingFilter</filter-name>??
  47. ????????<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>??
  48. ????????<init-param>??
  49. ????????????<param-name>encoding</param-name>??
  50. ????????????<param-value>UTF-8</param-value>??
  51. ????????</init-param>??
  52. ????</filter>??
  53. ????<filter-mapping>??
  54. ????????<filter-name>characterEncodingFilter</filter-name>??
  55. ????????<url-pattern>/*</url-pattern>??
  56. ????</filter-mapping>??
  57. ??
  58. ??<!--?dwr?添加配置??-->??
  59. ??<servlet>??
  60. ????????<servlet-name>dwr-invoker</servlet-name>??
  61. ????????<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>??
  62. ????????<init-param>??
  63. ????????????<description></description>??
  64. ????????????<param-name>debug</param-name>??
  65. ????????????<param-value>false</param-value>??
  66. ????????</init-param>??
  67. ????</servlet>??
  68. ??<servlet-mapping>??
  69. ???????<servlet-name>dwr-invoker</servlet-name>??
  70. ???????<url-pattern>/dwr/*</url-pattern>??
  71. ????</servlet-mapping>??
  72. ??
  73. ??
  74. ????<error-page>??
  75. ????????<exception-type>java.lang.Throwable</exception-type>??
  76. ????????<location>/common/jsp/error.jsp</location>??
  77. ????</error-page>??
  78. ????<error-page>??
  79. ????????<error-code>403</error-code>??
  80. ????????<location>/common/jsp/error403.jsp</location>??
  81. ????</error-page>??
  82. ????<error-page>??
  83. ????????<error-code>404</error-code>??
  84. ????????<location>/common/jsp/error404.jsp</location>??
  85. ????</error-page>??
  86. ??
  87. </web-app>??
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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>WeShare</display-name>
	<welcome-file-list>
		<welcome-file>/jumper.html</welcome-file>
	</welcome-file-list>
	<!-- 加载spring容器配置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 设置Spring容器加载配置文件路径 (主要配置都在这里面) -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml
  </param-value>
	</context-param>

	<!-- 配置Spring核心控制器 -->
	<servlet>
		<servlet-name>web</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 不指定 <init-param> 会自动找web.xml相同路径下 web-servlet.xml文件 -->
		<!-- 
		<init-param> 
			<param-name>contextConfigLocation</param-name> 
			<param-value>/WEB-INF/dispatcher.xml</param-value> 
		</init-param> 
			-->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>web</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>web</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>


	<!-- 解决工程编码过滤器 -->
	<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>

  <!-- dwr 添加配置  -->
  <servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
		<init-param>
			<description></description>
			<param-name>debug</param-name>
			<param-value>false</param-value>
		</init-param>
	</servlet>
  <servlet-mapping>
       <servlet-name>dwr-invoker</servlet-name>
       <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>


	<error-page>
		<exception-type>java.lang.Throwable</exception-type>
		<location>/common/jsp/error.jsp</location>
	</error-page>
	<error-page>
		<error-code>403</error-code>
		<location>/common/jsp/error403.jsp</location>
	</error-page>
	<error-page>
		<error-code>404</error-code>
		<location>/common/jsp/error404.jsp</location>
	</error-page>

</web-app>


三、 <!-- 配置Spring核心控制器 -->
?<servlet>
??<servlet-name>web</servlet-name>
??<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
??<!-- 不指定 <init-param> 会自动找web.xml相同路径下 web-servlet.xml文件 -->
??<!--
??<init-param>
???<param-name>contextConfigLocation</param-name>
???<param-value>/WEB-INF/dispatcher.xml</param-value>
??</init-param>
???-->
??<load-on-startup>1</load-on-startup>
?</servlet>? 这个我使用的是默认的 web-servlet.xml文件. 如下:

  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. ??
  3. <beans?xmlns="http://www.springframework.org/schema/beans"??
  4. ???????xmlns:tx="http://www.springframework.org/schema/tx"??
  5. ???????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  6. ???????xmlns:context="http://www.springframework.org/schema/context"??
  7. ???????xmlns:aop="http://www.springframework.org/schema/aop"??
  8. ???????xmlns:p="http://www.springframework.org/schema/p"???
  9. ???????xsi:schemaLocation="http://www.springframework.org/schema/beans???
  10. ???????http://www.springframework.org/schema/beans/spring-beans.xsd??
  11. ???????http://www.springframework.org/schema/tx???
  12. ???????http://www.springframework.org/schema/tx/spring-tx.xsd??
  13. ???????http://www.springframework.org/schema/context???
  14. ???????http://www.springframework.org/schema/context/spring-context.xsd??
  15. ???????http://www.springframework.org/schema/aop??
  16. ???????http://www.springframework.org/schema/aop/spring-aop.xsd">??
  17. ??????
  18. ????<!--?启动Spring?MVC的注解功能,完成请求和注解POJO的映射?-->??
  19. ????<bean?class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>??
  20. ??????
  21. ????<!--?对模型视图名称的解析,即在模型视图名称添加前后缀?-->??
  22. ????<bean?class="org.springframework.web.servlet.view.InternalResourceViewResolver"?>??
  23. ????????<property?name="prefix"?value="/"></property>??
  24. ????????<property?name="suffix"?value=".jsp"></property>??
  25. ????</bean>??
  26. ????????????
  27. ????<context:component-scan?base-package="com.weshare.**.web"/>??
  28. ?????<!--?/hrtiaoxin/src/java/com/guohualife/???hr/pfc/???web/controller/HrDeptMarkController.java?-->??
  29. </beans>??
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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:p="http://www.springframework.org/schema/p" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
 	
    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    	<property name="prefix" value="/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
          
    <context:component-scan base-package="com.weshare.**.web"/>
     <!-- /hrtiaoxin/src/java/com/guohualife/   hr/pfc/   web/controller/HrDeptMarkController.java -->
</beans>


?

四: applicationContext.xml 文件:

  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ????xmlns:tx="http://www.springframework.org/schema/tx"???
  4. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  5. ????xmlns:context="http://www.springframework.org/schema/context"??
  6. ????xmlns:aop="http://www.springframework.org/schema/aop"???
  7. ????xmlns:p="http://www.springframework.org/schema/p"??
  8. ????xsi:schemaLocation="http://www.springframework.org/schema/beans???
  9. ???????http://www.springframework.org/schema/beans/spring-beans.xsd??
  10. ???????http://www.springframework.org/schema/tx???
  11. ???????http://www.springframework.org/schema/tx/spring-tx.xsd??
  12. ???????http://www.springframework.org/schema/context???
  13. ???????http://www.springframework.org/schema/context/spring-context.xsd??
  14. ???????http://www.springframework.org/schema/aop??
  15. ???????http://www.springframework.org/schema/aop/spring-aop.xsd">??
  16. ??
  17. ??
  18. <!--?properties配置文件?-->??
  19. ????<bean?id="config"??
  20. ????????class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">??
  21. ????????<!--?是否忽略不可解析的?-->??
  22. ????????<property?name="ignoreUnresolvablePlaceholders"?value="true"?/>??
  23. ????????<!--?多个locations,?单个location?<value>?-->??
  24. ????????<property?name="locations">??
  25. ????????????<list>??
  26. ????????????????<value>/WEB-INF/config/config.properties</value>??
  27. ????????????????<value>/WEB-INF/config/urlAddress.properties</value>??
  28. ?????????????<!--???
  29. ????????????????<value>/WEB-INF/platform/config/config.properties</value>??
  30. ????????????????<value>/WEB-INF/config/config.properties</value>??
  31. ????????????????<value>/WEB-INF/hr/config/config.properties</value>??
  32. ?????????????????-->??
  33. ????????????</list>??
  34. ????????</property>??
  35. ????</bean>??
  36. ??????
  37. ????<!--?文件上传?-->??
  38. ????<bean?id="multipartResolver"?class="org.springframework.web.multipart.commons.CommonsMultipartResolver"???p:defaultEncoding="UTF-8"?></bean>??
  39. ??????
  40. ????<!--?加载?其他xml文件?-->??
  41. ????<import?resource="/config/aC-common.xml"?/>??
  42. ????<import?resource="/config/aC-interceptor.xml"?/>??
  43. ????<import?resource="/config/aC-properties.xml"?/>??
  44. ????<import?resource="/config/aC-quartz-config.xml"?/>??
  45. ??
  46. </beans>??
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">


<!-- properties配置文件 -->
	<bean id="config"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!-- 是否忽略不可解析的 -->
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<!-- 多个locations, 单个location <value> -->
		<property name="locations">
			<list>
				<value>/WEB-INF/config/config.properties</value>
				<value>/WEB-INF/config/urlAddress.properties</value>
			 <!-- 
				<value>/WEB-INF/platform/config/config.properties</value>
				<value>/WEB-INF/config/config.properties</value>
				<value>/WEB-INF/hr/config/config.properties</value>
				 -->
			</list>
		</property>
	</bean>
	
	<!-- 文件上传 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"   p:defaultEncoding="UTF-8" ></bean>
	
	<!-- 加载 其他xml文件 -->
	<import resource="/config/aC-common.xml" />
	<import resource="/config/aC-interceptor.xml" />
	<import resource="/config/aC-properties.xml" />
	<import resource="/config/aC-quartz-config.xml" />

</beans>



?

包含的其他4个xml文件:

4.1 : aC-common.xml

  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ????xmlns:tx="http://www.springframework.org/schema/tx"???
  4. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  5. ????xmlns:context="http://www.springframework.org/schema/context"??
  6. ????xmlns:aop="http://www.springframework.org/schema/aop"???
  7. ????xmlns:p="http://www.springframework.org/schema/p"??
  8. ????xsi:schemaLocation="http://www.springframework.org/schema/beans???
  9. ???????http://www.springframework.org/schema/beans/spring-beans.xsd??
  10. ???????http://www.springframework.org/schema/tx???
  11. ???????http://www.springframework.org/schema/tx/spring-tx.xsd??
  12. ???????http://www.springframework.org/schema/context???
  13. ???????http://www.springframework.org/schema/context/spring-context.xsd??
  14. ???????http://www.springframework.org/schema/aop??
  15. ???????http://www.springframework.org/schema/aop/spring-aop.xsd">??
  16. ??
  17. ????<context:component-scan?base-package="com.weshare.*.api.dao"></context:component-scan>??
  18. ????<context:component-scan?base-package="com.weshare.*.api.service"></context:component-scan>??
  19. ????<context:component-scan?base-package="com.weshare.common.utils"></context:component-scan>??
  20. ??????
  21. ??
  22. ????<bean?id="weshareDataSource"?class="org.apache.commons.dbcp.BasicDataSource"?destroy-method="close">??
  23. ????????<property?name="driverClassName"?value="${common.db.driver}"?/>??
  24. ????????<property?name="url"?value="${common.db.url}"?/>??
  25. ????????<property?name="username"?value="${common.db.username}"?/>??
  26. ????????<property?name="password"?value="${common.db.password}"?/>??
  27. ????????<!--?最大连接数据库连接数?-->??
  28. ????????<property?name="maxActive"?value="500"?/>??
  29. ????????<!--?最大等待连接中的数量???0标识没有限制?-->??
  30. ????????<property?name="maxIdle"?value="10"?/>??
  31. ????????<!--?最大等待毫秒数??超时报错?-->??
  32. ????????<property?name="maxWait"?value="500"?/>??
  33. ????????<property?name="defaultAutoCommit"?value="true"?/>??
  34. ????????<!--?是否自我中断?-->??
  35. ????????<property?name="removeAbandoned"?value="true"?/>??
  36. ????????<property?name="removeAbandonedTimeout"?value="60"?/>??
  37. ????</bean>??
  38. ??????
  39. ????<bean?id="weshareSessionFactory"?class="org.mybatis.spring.SqlSessionFactoryBean"?>??
  40. ????????<property?name="dataSource">??
  41. ????????????<ref?bean="weshareDataSource"?/>??
  42. ????????</property>??
  43. ????????<!--?MyBatis?的?XML?配置文件路径?-->??
  44. ????????<property?name="configLocation"?value="/WEB-INF/config/mybatisSqlMapConfig.xml"?/>??
  45. ????????<!--?扫描自动生成的xml文件?--><!--?Mybatis?XML映射文件?-->??
  46. ??????????
  47. ????????<property?name="mapperLocations"??>??
  48. ????????????<list><!--?Mybatis?XML映射文件?-->??
  49. ????????????????<value>classpath*:com/weshare/common/generated/xml/*.xml</value>??
  50. ????????????????<!--?扫描自己写的xml文件-->???
  51. ????????????????<value>classpath*:com/weshare/*/api/xml/*.xml</value>??
  52. ????????????</list>?????
  53. ????????</property>??
  54. ??????????
  55. ????</bean>??
  56. ??????
  57. ????<bean?id="weshareSqlSessionTemplate"?class="org.mybatis.spring.SqlSessionTemplate">??
  58. ????????<constructor-arg?index="0"?ref="weshareSessionFactory"></constructor-arg>??
  59. ????</bean>??
  60. ??????
  61. ????<!--?注册单个??mybatisGenerator??自动生成的?接口文件-->??
  62. ????<!--???
  63. ????<bean?id="TestMapper"?class="org.mybatis.spring.mapper.MapperFactoryBean">????
  64. ????????<property?name="mapperInterface"?value="com.weshare.common.generated.dao.TestMapper"?/>??
  65. ????????<property?name="sqlSessionTemplate"?ref="weshareSqlSessionTemplate"?></property>????
  66. ????</bean>??
  67. ?????-->??
  68. ?????<!--?扫描mybatisGenerator?自动生成的??所有接口-->??
  69. ????<bean?class="org.mybatis.spring.mapper.MapperScannerConfigurer"?>??
  70. ????????<property?name="basePackage"?value="com.weshare.common.generated.dao"?></property>??
  71. ????</bean>??
  72. ??????
  73. ??????
  74. ??????
  75. ????<!--?数据库事务策略-->??
  76. ????<bean?id="weshareTransactionManager"?class="org.springframework.jdbc.datasource.DataSourceTransactionManager">??
  77. ????????<property?name="dataSource">??
  78. ????????????<ref?bean="weshareDataSource"?/>??
  79. ????????</property>??
  80. ????</bean>??
  81. ??????
  82. ????<tx:advice?id="weshareTxAdvice"?transaction-manager="weshareTransactionManager">??
  83. ????????<tx:attributes>??
  84. ????????<!--???
  85. ????????????<tx:method?name="save*"?propagation="REQUIRED"?/>??
  86. ????????????<tx:method?name="ins*"?propagation="REQUIRED"?/>??
  87. ????????????<tx:method?name="del*"?propagation="REQUIRED"?/>??
  88. ????????????<tx:method?name="update*"?propagation="REQUIRED"?/>??
  89. ????????????<tx:method?name="find*"?read-only="true"?/>??
  90. ????????????<tx:method?name="get*"?read-only="true"?/>??
  91. ????????????<tx:method?name="select*"?read-only="true"?/>??
  92. ?????????????-->??
  93. ????????????<tx:method?name="*"?propagation="REQUIRED"?/>??
  94. ????????</tx:attributes>??
  95. ????</tx:advice>??
  96. ??
  97. ????<aop:config?proxy-target-class="true">??
  98. ????????<aop:advisor?pointcut="execution(?*?com.weshare.*.api.service.*.*(..))"?advice-ref="weshareTxAdvice"?/>??
  99. ??????????
  100. ????</aop:config>??
  101. ??????
  102. ??
  103. </beans>??
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

	<context:component-scan base-package="com.weshare.*.api.dao"></context:component-scan>
	<context:component-scan base-package="com.weshare.*.api.service"></context:component-scan>
	<context:component-scan base-package="com.weshare.common.utils"></context:component-scan>
	

	<bean id="weshareDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${common.db.driver}" />
		<property name="url" value="${common.db.url}" />
		<property name="username" value="${common.db.username}" />
		<property name="password" value="${common.db.password}" />
		<!-- 最大连接数据库连接数 -->
		<property name="maxActive" value="500" />
		<!-- 最大等待连接中的数量   0标识没有限制 -->
		<property name="maxIdle" value="10" />
		<!-- 最大等待毫秒数  超时报错 -->
		<property name="maxWait" value="500" />
		<property name="defaultAutoCommit" value="true" />
		<!-- 是否自我中断 -->
		<property name="removeAbandoned" value="true" />
		<property name="removeAbandonedTimeout" value="60" />
	</bean>
	
	<bean id="weshareSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
		<property name="dataSource">
            <ref bean="weshareDataSource" />
        </property>
        <!-- MyBatis 的 XML 配置文件路径 -->
        <property name="configLocation" value="/WEB-INF/config/mybatisSqlMapConfig.xml" />
		<!-- 扫描自动生成的xml文件 --><!-- Mybatis XML映射文件 -->
		
        <property name="mapperLocations"  >
        	<list><!-- Mybatis XML映射文件 -->
        		<value>classpath*:com/weshare/common/generated/xml/*.xml</value>
        		<!-- 扫描自己写的xml文件--> 
        		<value>classpath*:com/weshare/*/api/xml/*.xml</value>
        	</list>	
        </property>
        
    </bean>
    
    <bean id="weshareSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    	<constructor-arg index="0" ref="weshareSessionFactory"></constructor-arg>
    </bean>
	
	<!-- 注册单个  mybatisGenerator  自动生成的 接口文件-->
	<!-- 
	<bean id="TestMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
        <property name="mapperInterface" value="com.weshare.common.generated.dao.TestMapper" />
        <property name="sqlSessionTemplate" ref="weshareSqlSessionTemplate" ></property>  
    </bean>
     -->
     <!-- 扫描mybatisGenerator 自动生成的  所有接口-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
		<property name="basePackage" value="com.weshare.common.generated.dao" ></property>
	</bean>
	
	
	
	<!-- 数据库事务策略-->
	<bean id="weshareTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="weshareDataSource" />
        </property>
    </bean>
	
	<tx:advice id="weshareTxAdvice" transaction-manager="weshareTransactionManager">
		<tx:attributes>
		<!-- 
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="ins*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="select*" read-only="true" />
			 -->
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

	<aop:config proxy-target-class="true">
		<aop:advisor pointcut="execution( * com.weshare.*.api.service.*.*(..))" advice-ref="weshareTxAdvice" />
		
	</aop:config>
	

</beans>

?

4.2 aC-interceptor.xml

这里拦截器只是拦截到controller , 具体拦截到action‘,后面会有写到, 这里的配置只是参考。

  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ????xmlns:tx="http://www.springframework.org/schema/tx"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  4. ????xmlns:context="http://www.springframework.org/schema/context"??
  5. ????xmlns:aop="http://www.springframework.org/schema/aop"?xmlns:p="http://www.springframework.org/schema/p"??
  6. ????xmlns:mvc="http://www.springframework.org/schema/mvc"??
  7. ????xsi:schemaLocation="http://www.springframework.org/schema/beans???
  8. ???????http://www.springframework.org/schema/beans/spring-beans.xsd??
  9. ???????http://www.springframework.org/schema/tx???
  10. ???????http://www.springframework.org/schema/tx/spring-tx.xsd??
  11. ???????http://www.springframework.org/schema/context???
  12. ???????http://www.springframework.org/schema/context/spring-context.xsd??
  13. ???????http://www.springframework.org/schema/aop??
  14. ???????http://www.springframework.org/schema/aop/spring-aop.xsd??
  15. ???????http://www.springframework.org/schema/mvc??
  16. ???????http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">??
  17. ??
  18. ??
  19. ??
  20. ??
  21. ?<mvc:interceptors>????
  22. ????????<mvc:interceptor>????
  23. ????????????<!--设置拦截的路径??mvc:mapping指定到哪个action?,??用mappingURL匹配方法-->????
  24. ????????????<mvc:mapping?path="/dynamic/dynamic.do"?/>????
  25. ????????????<bean?class="com.weshare.common.web.LoginInterceptorController">???
  26. ????????????????<property?name="mappingURL"?value="^.*checklogin$"?/>??
  27. ????????????</bean>????
  28. ????????</mvc:interceptor>????
  29. ????</mvc:interceptors>????
  30. ???
  31. ???
  32. ???
  33. ???
  34. ??
  35. </beans>??
  36. ?????????
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx" 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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">




 <mvc:interceptors>  
        <mvc:interceptor>  
            <!--设置拦截的路径  mvc:mapping指定到哪个action ,  用mappingURL匹配方法-->  
            <mvc:mapping path="/dynamic/dynamic.do" />  
            <bean class="com.weshare.common.web.LoginInterceptorController"> 
             	<property name="mappingURL" value="^.*checklogin$" />
            </bean>  
        </mvc:interceptor>  
    </mvc:interceptors>  
 
 
 
 

</beans>
       


4.3 aC-properties.xml

这个xml作用是在启动项目的时候给org.springframework.beans.factory.config.PropertiesFactoryBean? 赋值,这样在代码中可以使用下面方法获得这些值

  1. @Resource??
  2. ????private?Properties?imageUrlProperties;??
@Resource
	private Properties imageUrlProperties;

?

  1. imageUrlProperties.getProperty("dynamicUrl")??
imageUrlProperties.getProperty("dynamicUrl")

?

  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ????xmlns:tx="http://www.springframework.org/schema/tx"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  4. ????xmlns:context="http://www.springframework.org/schema/context"??
  5. ????xmlns:aop="http://www.springframework.org/schema/aop"?xmlns:p="http://www.springframework.org/schema/p"??
  6. ????xsi:schemaLocation="http://www.springframework.org/schema/beans???
  7. ???????http://www.springframework.org/schema/beans/spring-beans.xsd??
  8. ???????http://www.springframework.org/schema/tx???
  9. ???????http://www.springframework.org/schema/tx/spring-tx.xsd??
  10. ???????http://www.springframework.org/schema/context???
  11. ???????http://www.springframework.org/schema/context/spring-context.xsd??
  12. ???????http://www.springframework.org/schema/aop??
  13. ???????http://www.springframework.org/schema/aop/spring-aop.xsd">??
  14. ??
  15. ??
  16. ????<!--?platform?properties?-->??
  17. ????<bean?id="imageUrlProperties"??
  18. ????????class="org.springframework.beans.factory.config.PropertiesFactoryBean">??
  19. ????????<property?name="singleton"?value="true"?/>??
  20. ????????<property?name="properties">??
  21. ????????????<props>??
  22. ????????????????<prop?key="aliyuming">${aliyuming}</prop>??
  23. ????????????????<prop?key="ACCESS_ID">${ACCESS_ID}</prop>??
  24. ????????????????<prop?key="ACCESS_KEY">${ACCESS_KEY}</prop>??
  25. ????????????????<prop?key="bucketDynamicAndHeadimages">${bucketDynamicAndHeadimages}</prop>??
  26. ????????????????<prop?key="faceurl.pre">${faceurl.pre}</prop>??
  27. ????????????????<prop?key="imagesUrl.pre">${imagesUrl.pre}</prop>??
  28. ????????????????<prop?key="dynamicUrl">${dynamicUrl}</prop>??
  29. ????????????????<prop?key="headUrl">${headUrl}</prop>??
  30. ????????????????<prop?key="edge.dynamic">${edge.dynamic}</prop>??
  31. ????????????????<prop?key="edge.small">${edge.small}</prop>??
  32. ????????????????<prop?key="edge.middle">${edge.middle}</prop>??
  33. ????????????????<prop?key="edge.big">${edge.big}</prop>??
  34. ????????????</props>??
  35. ????????</property>??
  36. ????</bean>??
  37. ??
  38. ??
  39. </beans>??
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx" 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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">


	<!-- platform properties -->
	<bean id="imageUrlProperties"
		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="singleton" value="true" />
		<property name="properties">
			<props>
				<prop key="aliyuming">${aliyuming}</prop>
				<prop key="ACCESS_ID">${ACCESS_ID}</prop>
				<prop key="ACCESS_KEY">${ACCESS_KEY}</prop>
				<prop key="bucketDynamicAndHeadimages">${bucketDynamicAndHeadimages}</prop>
				<prop key="faceurl.pre">${faceurl.pre}</prop>
				<prop key="imagesUrl.pre">${imagesUrl.pre}</prop>
				<prop key="dynamicUrl">${dynamicUrl}</prop>
				<prop key="headUrl">${headUrl}</prop>
				<prop key="edge.dynamic">${edge.dynamic}</prop>
				<prop key="edge.small">${edge.small}</prop>
				<prop key="edge.middle">${edge.middle}</prop>
				<prop key="edge.big">${edge.big}</prop>
			</props>
		</property>
	</bean>


</beans>


4aC-quartz-cofig.xml, 这个是批处理定时任务的xml配置方法,? 在这里我并没有使用, 我使用的总是注解的方式, 后面会讲到。

  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans??
  3. ????xsi:schemaLocation="?http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.1.xsd?http://www.springframework.org/schema/fex?http://www.springframework.org/schema/fex/spring-fex-1.5.xsd?http://www.springframework.org/schema/task?http://www.springframework.org/schema/task/spring-task-3.0.xsd?http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.1.xsd"??
  4. ????xmlns:p="http://www.springframework.org/schema/p"?xmlns:context="http://www.springframework.org/schema/context"??
  5. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:task="http://www.springframework.org/schema/task"??
  6. ????xmlns="http://www.springframework.org/schema/beans">??
  7. ????<!--?下面是使用注解配置的方法?-->??
  8. ????<context:component-scan?base-package="com.weshare.*.api.batch"?/>??
  9. ????<task:executor?pool-size="5"?id="executor"?/>??
  10. ????<task:scheduler?pool-size="10"?id="scheduler"?/>??
  11. ????<task:annotation-driven?scheduler="scheduler"????executor="executor"?/>??
  12. ??????????
  13. ??????????
  14. ??????????
  15. ??????????
  16. ??????????
  17. ????<!--?下面是?使用xml配置的方法?-->??
  18. ??
  19. ????<!--?<bean?id="personBatch"?class="com.weshare.person.api.batch.PersonBatch"???
  20. ????????/>?-->??
  21. ??
  22. ????<!--?启动触发器的配置开始?-->??
  23. ??
  24. ????<!--?<bean?name="startQuertz"?lazy-init="false"?autowire="no"?class="org.springframework.scheduling.quartz.SchedulerFactoryBean">???
  25. ????????<property?name="triggers">?<list>?<ref?bean="myJobTrigger"?/>?</list>?</property>???
  26. ????????</bean>?-->??
  27. ??
  28. ????<!--?启动触发器的配置结束?-->??
  29. ??
  30. ????<!--?quartz-2.x的配置?-->??
  31. ??
  32. ????<!--?<bean?id="myJobTrigger"?class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">???
  33. ????????<property?name="jobDetail">?<ref?bean="myJobDetail"?/>?</property>?<property???
  34. ????????name="cronExpression">?<value>0/1?*?*?*?*??</value>?</property>?</bean>?-->??
  35. ??
  36. ????<!--?调度的配置结束?-->??
  37. ??
  38. ????<!--?job的配置开始?-->??
  39. ??
  40. ????<!--?<bean?id="myJobDetail"?class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">???
  41. ????????<property?name="targetObject">?<ref?bean="personBatch"?/>?</property>?<property???
  42. ????????name="targetMethod">?<value>testMethod</value>?</property>?</bean>?-->??
  43. ??
  44. ????<!--?job的配置结束?-->??
  45. </beans>??
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/fex http://www.springframework.org/schema/fex/spring-fex-1.5.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
	xmlns="http://www.springframework.org/schema/beans">
	<!-- 下面是使用注解配置的方法 -->
	<context:component-scan base-package="com.weshare.*.api.batch" />
	<task:executor pool-size="5" id="executor" />
	<task:scheduler pool-size="10" id="scheduler" />
	<task:annotation-driven scheduler="scheduler"	executor="executor" />
		
		
		
		
		
	<!-- 下面是 使用xml配置的方法 -->

	<!-- <bean id="personBatch" class="com.weshare.person.api.batch.PersonBatch" 
		/> -->

	<!-- 启动触发器的配置开始 -->

	<!-- <bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
		<property name="triggers"> <list> <ref bean="myJobTrigger" /> </list> </property> 
		</bean> -->

	<!-- 启动触发器的配置结束 -->

	<!-- quartz-2.x的配置 -->

	<!-- <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 
		<property name="jobDetail"> <ref bean="myJobDetail" /> </property> <property 
		name="cronExpression"> <value>0/1 * * * * ?</value> </property> </bean> -->

	<!-- 调度的配置结束 -->

	<!-- job的配置开始 -->

	<!-- <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
		<property name="targetObject"> <ref bean="personBatch" /> </property> <property 
		name="targetMethod"> <value>testMethod</value> </property> </bean> -->

	<!-- job的配置结束 -->
</beans>


?

五:注解方式

下面deleteDynamic方法的 调用地址为: localhost:8080/xx工程名/dynamic/admin.do?action=deleteDynamic

?

?

?

源码:获取

?

?

?

Spring SpringMvc 3.0 + MyBatis 整合

原文:http://qq-24871163.iteye.com/blog/2172454

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!