你的位置:首页 > ASP.net教程

[ASP.net教程]Spring SpringMvc + MyBatis 整合


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

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

 

二、 web.[html] view plaincopy在CODE上查看代码片派生到我的代码片 


  1. <?version="1.0" encoding="UTF-8"?>  
  2. <web-app "http://www.w3.org/2001/
  3.     "http://java.sun.com/"http://java.sun.com/
  4.     xsi:schemaLocation="http://java.sun.com/
  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.
  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.
  26.         <!--   
  27.         <init-param>   
  28.             <param-name>contextConfigLocation</param-name>   
  29.             <param-value>/WEB-INF/dispatcher.</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>  


三、 <!-- 配置Spring核心控制器 -->
 <servlet>
  <servlet-name>web</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 不指定 <init-param> 会自动找web.  <!-- 
  <init-param> 
   <param-name>contextConfigLocation</param-name> 
   <param-value>/WEB-INF/dispatcher.  </init-param> 
   -->
  <load-on-startup>1</load-on-startup>
 </servlet>  这个我使用的是默认的 web-servlet.[html] view plaincopy在CODE上查看代码片派生到我的代码片 


  1. <?version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans "http://www.springframework.org/schema/beans"  
  4.        "http://www.springframework.org/schema/tx"  
  5.        "http://www.w3.org/2001/
  6.        "http://www.springframework.org/schema/context"  
  7.        "http://www.springframework.org/schema/aop"  
  8.        "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>  


 

四: applicationContext.[html] view plaincopy在CODE上查看代码片派生到我的代码片 


  1. <?version="1.0" encoding="UTF-8"?>  
  2. <beans "http://www.springframework.org/schema/beans"  
  3.     "http://www.springframework.org/schema/tx"   
  4.     "http://www.w3.org/2001/
  5.     "http://www.springframework.org/schema/context"  
  6.     "http://www.springframework.org/schema/aop"   
  7.     "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.     <!-- 加载 其他
  41.     <import resource="/config/aC-common./>  
  42.     <import resource="/config/aC-interceptor./>  
  43.     <import resource="/config/aC-properties./>  
  44.     <import resource="/config/aC-quartz-config./>  
  45.   
  46. </beans>  


 

包含的其他4个

4.1 : aC-common.[html] view plaincopy在CODE上查看代码片派生到我的代码片 


  1. <?version="1.0" encoding="UTF-8"?>  
  2. <beans "http://www.springframework.org/schema/beans"  
  3.     "http://www.springframework.org/schema/tx"   
  4.     "http://www.w3.org/2001/
  5.     "http://www.springframework.org/schema/context"  
  6.     "http://www.springframework.org/schema/aop"   
  7.     "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 的 
  44.         <property name="configLocation" value="/WEB-INF/config/mybatisSqlMapConfig./>  
  45.         <!-- 扫描自动生成的<!-- Mybatis 
  46.           
  47.         <property name="mapperLocations"  >  
  48.             <list><!-- Mybatis 
  49.                 <value>classpath*:com/weshare/common/generated/</value>  
  50.                 <!-- 扫描自己写的
  51.                 <value>classpath*:com/weshare/*/api/</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>  

 

4.2 aC-interceptor.

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

[html] view plaincopy在CODE上查看代码片派生到我的代码片 


  1. <?version="1.0" encoding="UTF-8"?>  
  2. <beans "http://www.springframework.org/schema/beans"  
  3.     "http://www.springframework.org/schema/tx" "http://www.w3.org/2001/
  4.     "http://www.springframework.org/schema/context"  
  5.     "http://www.springframework.org/schema/aop" "http://www.springframework.org/schema/p"  
  6.     "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.          


4.3 aC-properties.

这个[java] view plaincopy在CODE上查看代码片派生到我的代码片 


  1. @Resource  
  2.     private Properties imageUrlProperties;  

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片 


  1. imageUrlProperties.getProperty("dynamicUrl")  

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片 


  1. <?version="1.0" encoding="UTF-8"?>  
  2. <beans "http://www.springframework.org/schema/beans"  
  3.     "http://www.springframework.org/schema/tx" "http://www.w3.org/2001/
  4.     "http://www.springframework.org/schema/context"  
  5.     "http://www.springframework.org/schema/aop" "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>  


4aC-quartz-cofig.[html] view plaincopy在CODE上查看代码片派生到我的代码片 


  1. <?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.     "http://www.springframework.org/schema/p" "http://www.springframework.org/schema/context"  
  5.     "http://www.w3.org/2001/"http://www.springframework.org/schema/task"  
  6.     "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.     <!-- 下面是 使用
  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>  


 

五:注解方式

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

 

 

整理的有点乱,【源码地址获取】,希望可以帮助到大家