maven搭建springmvc+spring+mybatis实例

最近做了个maven管理的springmvc+spring+mybatis,还用到了阿里巴巴的 fastjson和druid连接池,配置文件如下

pom.xml文件

[html] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.dahafo.demo</groupId>
  6. <name>um</name>
  7. <packaging>war</packaging>
  8. <version>1.0.0-BUILD-SNAPSHOT</version>
  9. <properties>
  10. <java-version>1.7</java-version>
  11. <org.springframework-version>3.2.3.RELEASE</org.springframework-version>
  12. <org.aspectj-version>1.6.10</org.aspectj-version>
  13. <org.slf4j-version>1.6.6</org.slf4j-version>
  14. </properties>
  15. <dependencies>
  16. <!-- Spring -->
  17. <dependency>
  18. <groupId>org.springframework</groupId>
  19. <artifactId>spring-context</artifactId>
  20. <version>${org.springframework-version}</version>
  21. <exclusions>
  22. <!-- Exclude Commons Logging in favor of SLF4j -->
  23. <exclusion>
  24. <groupId>commons-logging</groupId>
  25. <artifactId>commons-logging</artifactId>
  26. </exclusion>
  27. </exclusions>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-webmvc</artifactId>
  32. <version>${org.springframework-version}</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-jdbc</artifactId>
  37. <version>${org.springframework-version}</version>
  38. </dependency>
  39. <!-- AspectJ -->
  40. <dependency>
  41. <groupId>org.aspectj</groupId>
  42. <artifactId>aspectjrt</artifactId>
  43. <version>${org.aspectj-version}</version>
  44. </dependency>
  45. <!-- Logging -->
  46. <dependency>
  47. <groupId>org.slf4j</groupId>
  48. <artifactId>slf4j-api</artifactId>
  49. <version>${org.slf4j-version}</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.slf4j</groupId>
  53. <artifactId>jcl-over-slf4j</artifactId>
  54. <version>${org.slf4j-version}</version>
  55. <scope>runtime</scope>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.slf4j</groupId>
  59. <artifactId>slf4j-log4j12</artifactId>
  60. <version>${org.slf4j-version}</version>
  61. <scope>runtime</scope>
  62. </dependency>
  63. <dependency>
  64. <groupId>log4j</groupId>
  65. <artifactId>log4j</artifactId>
  66. <version>1.2.15</version>
  67. <exclusions>
  68. <exclusion>
  69. <groupId>javax.mail</groupId>
  70. <artifactId>mail</artifactId>
  71. </exclusion>
  72. <exclusion>
  73. <groupId>javax.jms</groupId>
  74. <artifactId>jms</artifactId>
  75. </exclusion>
  76. <exclusion>
  77. <groupId>com.sun.jdmk</groupId>
  78. <artifactId>jmxtools</artifactId>
  79. </exclusion>
  80. <exclusion>
  81. <groupId>com.sun.jmx</groupId>
  82. <artifactId>jmxri</artifactId>
  83. </exclusion>
  84. </exclusions>
  85. <scope>runtime</scope>
  86. </dependency>
  87. <!-- @Inject -->
  88. <dependency>
  89. <groupId>javax.inject</groupId>
  90. <artifactId>javax.inject</artifactId>
  91. <version>1</version>
  92. </dependency>
  93. <!-- Servlet -->
  94. <dependency>
  95. <groupId>javax.servlet</groupId>
  96. <artifactId>servlet-api</artifactId>
  97. <version>2.5</version>
  98. <scope>provided</scope>
  99. </dependency>
  100. <dependency>
  101. <groupId>javax.servlet.jsp</groupId>
  102. <artifactId>jsp-api</artifactId>
  103. <version>2.1</version>
  104. <scope>provided</scope>
  105. </dependency>
  106. <dependency>
  107. <groupId>javax.servlet</groupId>
  108. <artifactId>jstl</artifactId>
  109. <version>1.2</version>
  110. </dependency>
  111. <!-- mybatis -->
  112. <dependency>
  113. <groupId>org.mybatis</groupId>
  114. <artifactId>mybatis</artifactId>
  115. <version>3.2.1</version>
  116. </dependency>
  117. <dependency>
  118. <groupId>org.mybatis</groupId>
  119. <artifactId>mybatis-spring</artifactId>
  120. <version>1.2.0</version>
  121. </dependency>
  122. <dependency>
  123. <groupId>mysql</groupId>
  124. <artifactId>mysql-connector-java</artifactId>
  125. <version>5.1.26</version>
  126. </dependency>
  127. <!-- Test -->
  128. <dependency>
  129. <groupId>junit</groupId>
  130. <artifactId>junit</artifactId>
  131. <version>4.7</version>
  132. <scope>test</scope>
  133. </dependency>
  134. <dependency>
  135. <groupId>com.alibaba</groupId>
  136. <artifactId>druid</artifactId>
  137. <version>0.2.20</version>
  138. </dependency>
  139. <dependency>
  140. <groupId>org.aspectj</groupId>
  141. <artifactId>aspectjweaver</artifactId>
  142. <version>1.7.2</version>
  143. </dependency>
  144. <dependency>
  145. <groupId>com.alibaba</groupId>
  146. <artifactId>fastjson</artifactId>
  147. <version>1.1.34</version>
  148. </dependency>
  149. <dependency>
  150. <groupId>com.fasterxml.jackson.core</groupId>
  151. <artifactId>jackson-core</artifactId>
  152. <version>2.2.2</version>
  153. </dependency>
  154. <dependency>
  155. <groupId>com.fasterxml.jackson.core</groupId>
  156. <artifactId>jackson-databind</artifactId>
  157. <version>2.2.2</version>
  158. </dependency>
  159. <dependency>
  160. <groupId>commons-fileupload</groupId>
  161. <artifactId>commons-fileupload</artifactId>
  162. <version>1.2.2</version>
  163. </dependency>
  164. </dependencies>
  165. <build>
  166. <plugins>
  167. <plugin>
  168. <artifactId>maven-eclipse-plugin</artifactId>
  169. <version>2.9</version>
  170. <configuration>
  171. <additionalProjectnatures>
  172. <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
  173. </additionalProjectnatures>
  174. <additionalBuildcommands>
  175. <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
  176. </additionalBuildcommands>
  177. <downloadSources>true</downloadSources>
  178. <downloadJavadocs>true</downloadJavadocs>
  179. </configuration>
  180. </plugin>
  181. <plugin>
  182. <groupId>org.apache.maven.plugins</groupId>
  183. <artifactId>maven-compiler-plugin</artifactId>
  184. <version>2.5.1</version>
  185. <configuration>
  186. <source>1.6</source>
  187. <target>1.6</target>
  188. <compilerArgument>-Xlint:all</compilerArgument>
  189. <showWarnings>true</showWarnings>
  190. <showDeprecation>true</showDeprecation>
  191. </configuration>
  192. </plugin>
  193. <plugin>
  194. <groupId>org.codehaus.mojo</groupId>
  195. <artifactId>exec-maven-plugin</artifactId>
  196. <version>1.2.1</version>
  197. <configuration>
  198. <mainClass>org.test.int1.Main</mainClass>
  199. </configuration>
  200. </plugin>
  201. </plugins>
  202. </build>
  203. <artifactId>demo-um</artifactId>
  204. </project>

web.xml

[html] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  5. <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>/WEB-INF/spring/root-context.xml</param-value>
  9. </context-param>
  10. <!-- 用spring Encoding,解决乱码问题 -->
  11. <filter>
  12. <filter-name>encodingFilter</filter-name>
  13. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  14. <init-param>
  15. <param-name>encoding</param-name>
  16. <param-value>UTF-8</param-value>
  17. </init-param>
  18. <init-param>
  19. <param-name>forceEncoding</param-name>
  20. <param-value>true</param-value>
  21. </init-param>
  22. </filter>
  23. <filter-mapping>
  24. <filter-name>encodingFilter</filter-name>
  25. <url-pattern>/*</url-pattern>
  26. </filter-mapping>
  27. <!-- Creates the Spring Container shared by all Servlets and Filters -->
  28. <listener>
  29. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  30. </listener>
  31. <!-- Processes application requests -->
  32. <servlet>
  33. <servlet-name>appServlet</servlet-name>
  34. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  35. <init-param>
  36. <param-name>contextConfigLocation</param-name>
  37. <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
  38. </init-param>
  39. <load-on-startup>1</load-on-startup>
  40. </servlet>
  41. <servlet-mapping>
  42. <servlet-name>appServlet</servlet-name>
  43. <url-pattern>/</url-pattern>
  44. </servlet-mapping>
  45. </web-app>

root-context.xml spring的主配置文件

[html] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  7. ">
  8. <context:annotation-config />
  9. <context:component-scan base-package="com.dahafo.demo.um" />
  10. <import resource="spring-mybatis.xml"/>
  11. </beans>

servlet-context.xml springmvc的主配置文件

[html] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans:beans xmlns="http://www.springframework.org/schema/mvc"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:beans="http://www.springframework.org/schema/beans"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- DispatcherServlet Context: defines this servlet‘s request-processing infrastructure -->
  10. <!-- Enables the Spring MVC @Controller programming model -->
  11. <annotation-driven />
  12. <context:component-scan base-package="com.dahafo.demo.um.controller" />
  13. <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
  14. <resources mapping="/resources/**" location="/resources/" />
  15. <resources mapping="/css/**" location="/css/" />
  16. <resources mapping="/images/**" location="/images/" />
  17. <resources mapping="/js/**" location="/js/" />
  18. <!-- Jackson转换器 -->
  19. <beans:bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
  20. <!-- fastjson转换器 -->
  21. <beans:bean id="fastJsonHttpMessageConverter"  class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"/>
  22. <beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"  >
  23. <beans:property name="messageConverters">
  24. <beans:list>
  25. <beans:ref bean="fastJsonHttpMessageConverter" /><!-- json转换器 -->
  26. </beans:list>
  27. </beans:property>
  28. </beans:bean>
  29. <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
  30. <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  31. <beans:property name="prefix" value="/WEB-INF/views/" />
  32. <beans:property name="suffix" value=".jsp" />
  33. </beans:bean>
  34. <beans:bean id="maxUploadSize" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  35. <beans:property name="maxUploadSize" value="32505856" /><!-- 上传文件大小限制为31M,31*1024*1024 -->
  36. <beans:property name="maxInMemorySize" value="4096" />
  37. </beans:bean>
  38. </beans:beans>

spring-mybatis.xml  mybatis的配置文件,数据源用 的是阿里巴巴的 druid

[html] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  9. http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  11. ">
  12. <!-- 引入属性文件 -->
  13. <context:property-placeholder location="classpath:jdbc.properties" />
  14. <!-- 配置数据源 -->
  15. <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  16. <property name="url" value="${url}" />
  17. <property name="username" value="${username}" />
  18. <property name="password" value="${password}" />
  19. <property name="initialSize" value="1" />
  20. <property name="maxActive" value="20" />
  21. <property name="minIdle" value="1" />
  22. <property name="maxWait" value="60000" />
  23. <property name="validationQuery" value="${validationQuery}" />
  24. <property name="testOnBorrow" value="false" />
  25. <property name="testOnReturn" value="false" />
  26. <property name="testWhileIdle" value="true" />
  27. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  28. <property name="minEvictableIdleTimeMillis" value="25200000" />
  29. <property name="removeAbandoned" value="true" />
  30. <property name="removeAbandonedTimeout" value="1800" />
  31. <property name="logAbandoned" value="true" />
  32. <property name="filters" value="mergeStat" />
  33. </bean>
  34. <!-- myBatis文件 -->
  35. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  36. <property name="dataSource" ref="dataSource" />
  37. <property name="mapperLocations" value="classpath:com/dahafo/demo/um/mapping/*Mapper.xml" />
  38. </bean>
  39. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  40. <property name="basePackage" value="com.dahafo.demo.um.dao" />
  41. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  42. </bean>
  43. <!-- 配置事务管理器 -->
  44. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  45. <property name="dataSource" ref="dataSource" />
  46. </bean>
  47. <!-- 注解方式配置事物 -->
  48. <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
  49. <!-- 拦截器方式配置事物 -->
  50. <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
  51. <tx:attributes>
  52. <tx:method name="add*" propagation="REQUIRED" />
  53. <tx:method name="modify*" propagation="REQUIRED" />
  54. <tx:method name="delete*" propagation="REQUIRED" />
  55. <tx:method name="find*" propagation="SUPPORTS" />
  56. <tx:method name="query" propagation="SUPPORTS" />
  57. <tx:method name="search*" propagation="SUPPORTS" />
  58. <tx:method name="*" propagation="SUPPORTS" />
  59. </tx:attributes>
  60. </tx:advice>
  61. <aop:config>
  62. <aop:pointcut id="transactionPointcut" expression="execution(* com.dahafo.demo.um.service.*.*(..))" />
  63. <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
  64. </aop:config>
  65. <!-- 配置druid监控spring jdbc -->
  66. <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
  67. </bean>
  68. <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
  69. <property name="patterns">
  70. <list>
  71. <value>com.dahafo.demo.um.service.impl</value>
  72. </list>
  73. </property>
  74. </bean>
  75. <aop:config>
  76. <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
  77. </aop:config>
  78. </beans>

很多人都发私信要源码,但是由于时间比较长了,源码我实在是找不到了,等有时间了,我再搭建一个,并开放源码,谢谢大家关注。

》》源码地址获取

springmvc + mybatis整合详细,及遇到的问题请参看以下资料:

参考资料:

http://www.springmvc,net/detail/6074493.html

http://my.spring.net/wangbiglei/blog/489583

http://my.springmvc.net/wangbiglei/blog/489604

时间: 2024-10-06 09:10:07

maven搭建springmvc+spring+mybatis实例的相关文章

Maven 搭建SpringMvc+Spring+Mybatis详细记录

总觉得,看比人写的总是那么好,每次搭建框架时都会找博客,找教程来跟着一步一步走,虽然很快搭建成功了,但是经常情况是我并不知道我干了什么,也不记得具体步骤,到底为什么要这么做,今天我详细记录了一下自己搭建的过程,并且尽量理解每一步干什么. SSM框架当下比较流行,我也是用这个框架来作为记录,尝试详细地记录下每一个步骤,学习,不要怕开头难. 一.创建一个新的Maven项目 1. new -> Maven -> Maven Project 选择webapp工程. 2.maven项目建好以后,工程目录

手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

一. 框架详情 Spring 是一个轻量级的Java开发框架,它是为了解决企业应用开发的复杂性而创建的.Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益. 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. SpringMVC 属于SpringFrameWork的后续产品,分离了控制器.模型对象.分派器以及处理程序对象的角色,这种分离让它们更容易进行定制. MyBatis 是一个基于Ja

手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)

SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式,将整个系统划分为显示层.Controller层.Service层.Dao层四层,使用SpringMVC负责请求的转发和视图管理,Spring实现业务对象管理, MyBatis作为数据对象持久化引擎. 一. 框架详情 Spring 是一个轻量级的Java开发框架,它是为了解决企业应用开发的复杂性而创建的.Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何

手把手Maven搭建SpringMVC+Spring+MyBatis框架

SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式,将整个系统划分为显示层.Controller层.Service层.Dao层四层,使用SpringMVC负责请求的转发和视图管理,Spring实现业务对象管理, MyBatis作为数据对象持久化引擎. 一. 框架详情 Spring 是一个轻量级的Java开发框架,它是为了解决企业应用开发的复杂性而创建的.Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何

单工程搭建springmvc+spring+mybatis(maven,idea)

单工程搭建springmvc+spring+mybatis(maven,idea) 1.pom.xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven

Maven搭建springMVC+spring+hibernate环境

这次不再使用struts2做控制器,采用spring自己的springMVC框架实现. 首先,改写pom.xml文件,不需要struts2的相关jar了. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apa

Maven构建 SpringMVC+Spring+MyBatis 环境整合

目录 1. Maven 项目搭建 2. Maven 插件生成 MyBatis 代码 3. 待续 ... 开发环境 开发环境请尽量保持一致,不一致的情况可能存在问题. JDK 1.7 MyEclipse 8.5 Maven 3.0.4 Spring 4.2.5.RELEASE MyBaties 3.3.1 Maven 项目搭建 现在代码管理一般都是采用Maven管理,所以现在构建项目的不二选择就是用它,百度一下介绍文档很多,环境配置这里不做介绍,下面给出建立web项目相关步骤. 1. 新建一个Ma

springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

@[email protected] 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) mybatis学习笔记(六) -- maven+spring+mybatis从零开始搭建整合详细过程(下) springmvc学习笔记(一) -- 从零搭建,基础入门 这一篇,在这些练习的基础上,将它们整合在一起! 搭建步骤如下 一.新建maven项目,配置环境,测试是否配置成

从头开始基于Maven搭建SpringMVC+Mybatis项目(1)

技术发展日新月异,许多曾经拥有霸主地位的流行技术短短几年间已被新兴技术所取代. 在Java的世界中,框架之争可能比语言本身的改变更让人关注.近几年,SpringMVC凭借简单轻便.开发效率高.与Spring框架无缝整合等特点,逐渐击败前辈Struts/Struts2,成为最常用的Web框架.而Mybatis相对于Hibernate,同样具有开发简单.效率高的优势,而且对SQL的可控性更好,有利于性能调优,逐渐也积累了挑战Hibernate的实力和人气. 当前SpringMVC+Mybatis已经