Spring SpringMvc + MyBatis 整合

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

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

二、 web.xml配置文件

[html] view plaincopy

  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. <!-- 配置Spring核心控制器 -->
  21. <servlet>
  22. <servlet-name>web</servlet-name>
  23. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  24. <!-- 不指定 <init-param> 会自动找web.xml相同路径下 web-servlet.xml文件 -->
  25. <!--
  26. <init-param>
  27. <param-name>contextConfigLocation</param-name>
  28. <param-value>/WEB-INF/dispatcher.xml</param-value>
  29. </init-param>
  30. -->
  31. <load-on-startup>1</load-on-startup>
  32. </servlet>
  33. <servlet-mapping>
  34. <servlet-name>web</servlet-name>
  35. <url-pattern>*.do</url-pattern>
  36. </servlet-mapping>
  37. <servlet-mapping>
  38. <servlet-name>web</servlet-name>
  39. <url-pattern>*.action</url-pattern>
  40. </servlet-mapping>
  41. <!-- 解决工程编码过滤器 -->
  42. <filter>
  43. <filter-name>characterEncodingFilter</filter-name>
  44. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  45. <init-param>
  46. <param-name>encoding</param-name>
  47. <param-value>UTF-8</param-value>
  48. </init-param>
  49. </filter>
  50. <filter-mapping>
  51. <filter-name>characterEncodingFilter</filter-name>
  52. <url-pattern>/*</url-pattern>
  53. </filter-mapping>
  54. <!-- dwr 添加配置  -->
  55. <servlet>
  56. <servlet-name>dwr-invoker</servlet-name>
  57. <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  58. <init-param>
  59. <description></description>
  60. <param-name>debug</param-name>
  61. <param-value>false</param-value>
  62. </init-param>
  63. </servlet>
  64. <servlet-mapping>
  65. <servlet-name>dwr-invoker</servlet-name>
  66. <url-pattern>/dwr/*</url-pattern>
  67. </servlet-mapping>
  68. <error-page>
  69. <exception-type>java.lang.Throwable</exception-type>
  70. <location>/common/jsp/error.jsp</location>
  71. </error-page>
  72. <error-page>
  73. <error-code>403</error-code>
  74. <location>/common/jsp/error403.jsp</location>
  75. </error-page>
  76. <error-page>
  77. <error-code>404</error-code>
  78. <location>/common/jsp/error404.jsp</location>
  79. </error-page>
  80. </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文件. 如下:

[html] view plaincopy

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

四: applicationContext.xml 文件:

[html] view plaincopy

  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. <!-- properties配置文件 -->
  17. <bean id="config"
  18. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  19. <!-- 是否忽略不可解析的 -->
  20. <property name="ignoreUnresolvablePlaceholders" value="true" />
  21. <!-- 多个locations, 单个location <value> -->
  22. <property name="locations">
  23. <list>
  24. <value>/WEB-INF/config/config.properties</value>
  25. <value>/WEB-INF/config/urlAddress.properties</value>
  26. <!--
  27. <value>/WEB-INF/platform/config/config.properties</value>
  28. <value>/WEB-INF/config/config.properties</value>
  29. <value>/WEB-INF/hr/config/config.properties</value>
  30. -->
  31. </list>
  32. </property>
  33. </bean>
  34. <!-- 文件上传 -->
  35. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"   p:defaultEncoding="UTF-8" ></bean>
  36. <!-- 加载 其他xml文件 -->
  37. <import resource="/config/aC-common.xml" />
  38. <import resource="/config/aC-interceptor.xml" />
  39. <import resource="/config/aC-properties.xml" />
  40. <import resource="/config/aC-quartz-config.xml" />
  41. </beans>

包含的其他4个xml文件:

4.1 : aC-common.xml

[html] view plaincopy

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

4.2 aC-interceptor.xml

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

[html] view plaincopy

  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. <mvc:interceptors>
  18. <mvc:interceptor>
  19. <!--设置拦截的路径  mvc:mapping指定到哪个action ,  用mappingURL匹配方法-->
  20. <mvc:mapping path="/dynamic/dynamic.do" />
  21. <bean class="com.weshare.common.web.LoginInterceptorController">
  22. <property name="mappingURL" value="^.*checklogin$" />
  23. </bean>
  24. </mvc:interceptor>
  25. </mvc:interceptors>
  26. </beans>

4.3 aC-properties.xml

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

[java] view plaincopy

  1. @Resource
  2. private Properties imageUrlProperties;

[java] view plaincopy

  1. imageUrlProperties.getProperty("dynamicUrl")

[html] view plaincopy

  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. <!-- platform properties -->
  15. <bean id="imageUrlProperties"
  16. class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  17. <property name="singleton" value="true" />
  18. <property name="properties">
  19. <props>
  20. <prop key="aliyuming">${aliyuming}</prop>
  21. <prop key="ACCESS_ID">${ACCESS_ID}</prop>
  22. <prop key="ACCESS_KEY">${ACCESS_KEY}</prop>
  23. <prop key="bucketDynamicAndHeadimages">${bucketDynamicAndHeadimages}</prop>
  24. <prop key="faceurl.pre">${faceurl.pre}</prop>
  25. <prop key="imagesUrl.pre">${imagesUrl.pre}</prop>
  26. <prop key="dynamicUrl">${dynamicUrl}</prop>
  27. <prop key="headUrl">${headUrl}</prop>
  28. <prop key="edge.dynamic">${edge.dynamic}</prop>
  29. <prop key="edge.small">${edge.small}</prop>
  30. <prop key="edge.middle">${edge.middle}</prop>
  31. <prop key="edge.big">${edge.big}</prop>
  32. </props>
  33. </property>
  34. </bean>
  35. </beans>

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

[html] view plaincopy

  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. <!-- 下面是 使用xml配置的方法 -->
  13. <!-- <bean id="personBatch" class="com.weshare.person.api.batch.PersonBatch"
  14. /> -->
  15. <!-- 启动触发器的配置开始 -->
  16. <!-- <bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  17. <property name="triggers"> <list> <ref bean="myJobTrigger" /> </list> </property>
  18. </bean> -->
  19. <!-- 启动触发器的配置结束 -->
  20. <!-- quartz-2.x的配置 -->
  21. <!-- <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
  22. <property name="jobDetail"> <ref bean="myJobDetail" /> </property> <property
  23. name="cronExpression"> <value>0/1 * * * * ?</value> </property> </bean> -->
  24. <!-- 调度的配置结束 -->
  25. <!-- job的配置开始 -->
  26. <!-- <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  27. <property name="targetObject"> <ref bean="personBatch" /> </property> <property
  28. name="targetMethod"> <value>testMethod</value> </property> </bean> -->
  29. <!-- job的配置结束 -->
  30. </beans>

五:注解方式

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

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

时间: 2024-10-20 06:12:15

Spring SpringMvc + MyBatis 整合的相关文章

Spring+SpringMVC +MyBatis整合配置文件案例66666

Spring+SpringMVC +MyBatis整合配置文件案例 标签: springspringmvcmybatismvcjava 2017-04-13 19:12 228人阅读 评论(1) 收藏 举报 分类: java_javaSE(2) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Spring+SpringMVC +MyBatis整合配置文件案例 针对spring/SpringMVC/MyBatis三个框架的整合有很多的方式,经过最近的学习我来总结一下其配置文

ssm之spring+springmvc+mybatis整合初探

1.基本目录如下  2.首先是向lib中加入相应的jar包  3.然后在web.xml中加入配置,使spring和springmvc配置文件起作用. <?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/

spring+springMVC+mybatis整合并使用redis做缓存

1.用idea 构建一个maven 的web项目 点击下一步 继续下一步 选着maven home directory 自己的maven 安装目录,全选下一步,等一分钟就OK了 2.配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="h

Spring+SpringMvc+Mybatis整合注意事项

web.xml代码如下 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/n

2016.5.19 SSM 框架(一) Spring SpringMVC Mybatis 整合思路

前言 : 内容 依然是根据 传智播客 燕青老师 视频 及自己理解所写 1. 总体框架图 2.理解 可能从后往前更好理解一点 底层: 数据库    mysql  sqlserver oracle 等等 持久层(DAO):                   ---调用数据库   提供mapper接口 持久层 生成 Mapper接口(DAO接口), 提供访问数据库的方法和数据. 业务层(Service):                --- 调用 mapper 接口     提供Service接口

Spring+SpringMVC+MyBatis整合应用

1)搭建Spring,SpringMVC和MyBatis环境 创建一个web工程 添加MyBatis相关环境 引入数据库驱动包和DBCP连接池开发包 引入MyBatis开发包 添加Spring,SpringMVC相关技术环境 引入Spring ioc,jdbc-tx,aop开发包 引入Spring web,webmvc开发包 在src下添加spring.xml配置文件 在web.xml中配置DispatcherServlet前端控制器和中文乱码 处理过滤器CharacterEncodingFil

Spring+SpringMVC+Mybatis整合 pom示例

<!-- SpringMVC,Spring --> <dependencies> <!-- Spring mvc --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spri

Spring+SpringMVC+mybatis整合以及注解的使用

1.包结构:  2.spring配置:基本的DAO配置以及扫描Mapper(扫描出来的Mapper为首字母小写) 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/sche

Spring+SpringMVC+MyBatis整合配置

前端控制器 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="