当前位置: 代码迷 >> VC/MFC >> maven筹建springmvc+spring+mybatis实例
  详细解决方案

maven筹建springmvc+spring+mybatis实例

热度:258   发布时间:2016-05-02 03:54:44.0
maven搭建springmvc+spring+mybatis实例

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

pom.xml文件

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  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.                   
  40.         <!-- AspectJ -->  
  41.         <dependency>  
  42.             <groupId>org.aspectj</groupId>  
  43.             <artifactId>aspectjrt</artifactId>  
  44.             <version>${org.aspectj-version}</version>  
  45.         </dependency>   
  46.           
  47.         <!-- Logging -->  
  48.         <dependency>  
  49.             <groupId>org.slf4j</groupId>  
  50.             <artifactId>slf4j-api</artifactId>  
  51.             <version>${org.slf4j-version}</version>  
  52.         </dependency>  
  53.         <dependency>  
  54.             <groupId>org.slf4j</groupId>  
  55.             <artifactId>jcl-over-slf4j</artifactId>  
  56.             <version>${org.slf4j-version}</version>  
  57.             <scope>runtime</scope>  
  58.         </dependency>  
  59.         <dependency>  
  60.             <groupId>org.slf4j</groupId>  
  61.             <artifactId>slf4j-log4j12</artifactId>  
  62.             <version>${org.slf4j-version}</version>  
  63.             <scope>runtime</scope>  
  64.         </dependency>  
  65.         <dependency>  
  66.             <groupId>log4j</groupId>  
  67.             <artifactId>log4j</artifactId>  
  68.             <version>1.2.15</version>  
  69.             <exclusions>  
  70.                 <exclusion>  
  71.                     <groupId>javax.mail</groupId>  
  72.                     <artifactId>mail</artifactId>  
  73.                 </exclusion>  
  74.                 <exclusion>  
  75.                     <groupId>javax.jms</groupId>  
  76.                     <artifactId>jms</artifactId>  
  77.                 </exclusion>  
  78.                 <exclusion>  
  79.                     <groupId>com.sun.jdmk</groupId>  
  80.                     <artifactId>jmxtools</artifactId>  
  81.                 </exclusion>  
  82.                 <exclusion>  
  83.                     <groupId>com.sun.jmx</groupId>  
  84.                     <artifactId>jmxri</artifactId>  
  85.                 </exclusion>  
  86.             </exclusions>  
  87.             <scope>runtime</scope>  
  88.         </dependency>  
  89.   
  90.         <!-- @Inject -->  
  91.         <dependency>  
  92.             <groupId>javax.inject</groupId>  
  93.             <artifactId>javax.inject</artifactId>  
  94.             <version>1</version>  
  95.         </dependency>  
  96.                   
  97.         <!-- Servlet -->  
  98.         <dependency>  
  99.             <groupId>javax.servlet</groupId>  
  100.             <artifactId>servlet-api</artifactId>  
  101.             <version>2.5</version>  
  102.             <scope>provided</scope>  
  103.         </dependency>  
  104.         <dependency>  
  105.             <groupId>javax.servlet.jsp</groupId>  
  106.             <artifactId>jsp-api</artifactId>  
  107.             <version>2.1</version>  
  108.             <scope>provided</scope>  
  109.         </dependency>  
  110.         <dependency>  
  111.             <groupId>javax.servlet</groupId>  
  112.             <artifactId>jstl</artifactId>  
  113.             <version>1.2</version>  
  114.         </dependency>  
  115.     <!-- mybatis -->  
  116.         <dependency>  
  117.             <groupId>org.mybatis</groupId>  
  118.             <artifactId>mybatis</artifactId>  
  119.             <version>3.2.1</version>  
  120.         </dependency>  
  121.         <dependency>  
  122.             <groupId>org.mybatis</groupId>  
  123.             <artifactId>mybatis-spring</artifactId>  
  124.             <version>1.2.0</version>  
  125.         </dependency>  
  126.         <dependency>  
  127.             <groupId>mysql</groupId>  
  128.             <artifactId>mysql-connector-java</artifactId>  
  129.             <version>5.1.26</version>  
  130.         </dependency>  
  131.           
  132.           
  133.           
  134.         <!-- Test -->  
  135.         <dependency>  
  136.             <groupId>junit</groupId>  
  137.             <artifactId>junit</artifactId>  
  138.             <version>4.7</version>  
  139.             <scope>test</scope>  
  140.         </dependency>  
  141.         <dependency>  
  142.             <groupId>com.alibaba</groupId>  
  143.             <artifactId>druid</artifactId>  
  144.             <version>0.2.20</version>  
  145.         </dependency>  
  146.   
  147.         <dependency>  
  148.             <groupId>org.aspectj</groupId>  
  149.             <artifactId>aspectjweaver</artifactId>  
  150.             <version>1.7.2</version>  
  151.         </dependency>  
  152.         <dependency>  
  153.             <groupId>com.alibaba</groupId>  
  154.             <artifactId>fastjson</artifactId>  
  155.             <version>1.1.34</version>  
  156.         </dependency>  
  157.         <dependency>  
  158.             <groupId>com.fasterxml.jackson.core</groupId>  
  159.             <artifactId>jackson-core</artifactId>  
  160.             <version>2.2.2</version>  
  161.         </dependency>  
  162.         <dependency>  
  163.             <groupId>com.fasterxml.jackson.core</groupId>  
  164.             <artifactId>jackson-databind</artifactId>  
  165.             <version>2.2.2</version>  
  166.         </dependency>  
  167.         <dependency>  
  168.             <groupId>commons-fileupload</groupId>  
  169.             <artifactId>commons-fileupload</artifactId>  
  170.             <version>1.2.2</version>  
  171.         </dependency>  
  172.     </dependencies>  
  173.       
  174.       
  175.       
  176.       
  177.       
  178.       
  179.       
  180.       
  181.       
  182.     <build>  
  183.         <plugins>  
  184.             <plugin>  
  185.                 <artifactId>maven-eclipse-plugin</artifactId>  
  186.                 <version>2.9</version>  
  187.                 <configuration>  
  188.                     <additionalProjectnatures>  
  189.                         <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>  
  190.                     </additionalProjectnatures>  
  191.                     <additionalBuildcommands>  
  192.                         <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>  
  193.                     </additionalBuildcommands>  
  194.                     <downloadSources>true</downloadSources>  
  195.                     <downloadJavadocs>true</downloadJavadocs>  
  196.                 </configuration>  
  197.             </plugin>  
  198.             <plugin>  
  199.                 <groupId>org.apache.maven.plugins</groupId>  
  200.                 <artifactId>maven-compiler-plugin</artifactId>  
  201.                 <version>2.5.1</version>  
  202.                 <configuration>  
  203.                     <source>1.6</source>  
  204.                     <target>1.6</target>  
  205.                     <compilerArgument>-Xlint:all</compilerArgument>  
  206.                     <showWarnings>true</showWarnings>  
  207.                     <showDeprecation>true</showDeprecation>  
  208.                 </configuration>  
  209.             </plugin>  
  210.             <plugin>  
  211.                 <groupId>org.codehaus.mojo</groupId>  
  212.                 <artifactId>exec-maven-plugin</artifactId>  
  213.                 <version>1.2.1</version>  
  214.                 <configuration>  
  215.                     <mainClass>org.test.int1.Main</mainClass>  
  216.                 </configuration>  
  217.             </plugin>  
  218.         </plugins>  
  219.     </build>  
  220.     <artifactId>demo-um</artifactId>  
  221. </project>  


web.xml

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  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.   
  6.   
  7.   
  8.     <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->  
  9.     <context-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>/WEB-INF/spring/root-context.xml</param-value>  
  12.     </context-param>  
  13.     <!-- 用spring Encoding,解决乱码问题 -->  
  14.     <filter>      
  15.             <filter-name>encodingFilter</filter-name>      
  16.             <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>      
  17.             <init-param>      
  18.                 <param-name>encoding</param-name>      
  19.                 <param-value>UTF-8</param-value>      
  20.             </init-param>      
  21.             <init-param>      
  22.                 <param-name>forceEncoding</param-name>      
  23.                 <param-value>true</param-value>      
  24.             </init-param>      
  25.         </filter>      
  26.          <filter-mapping>      
  27.             <filter-name>encodingFilter</filter-name>      
  28.             <url-pattern>/*</url-pattern>      
  29.         </filter-mapping>   
  30.     <!-- Creates the Spring Container shared by all Servlets and Filters -->  
  31.     <listener>  
  32.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  33.     </listener>  
  34.   
  35.            
  36.       
  37.           
  38.     <!-- Processes application requests -->  
  39.     <servlet>  
  40.         <servlet-name>appServlet</servlet-name>  
  41.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  42.         <init-param>  
  43.             <param-name>contextConfigLocation</param-name>  
  44.             <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>  
  45.         </init-param>  
  46.         <load-on-startup>1</load-on-startup>  
  47.     </servlet>  
  48.           
  49.     <servlet-mapping>  
  50.         <servlet-name>appServlet</servlet-name>  
  51.         <url-pattern>/</url-pattern>  
  52.     </servlet-mapping>  
  53.   
  54. </web-app>  

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

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  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在CODE上查看代码片派生到我的代码片
 
  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.   
  10.     <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->  
  11.       
  12.     <!-- Enables the Spring MVC @Controller programming model -->  
  13.     <annotation-driven />  
  14.     <context:component-scan base-package="com.dahafo.demo.um.controller" />  
  15.   
  16.     <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->  
  17.     <resources mapping="/resources/**" location="/resources/" />  
  18.     <resources mapping="/css/**" location="/css/" />  
  19.     <resources mapping="/images/**" location="/images/" />  
  20.     <resources mapping="/js/**" location="/js/" />  
  21.   
  22.     <!-- Jackson转换器 -->  
  23.     <beans:bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />  
  24.     <!-- fastjson转换器 -->  
  25.     <beans:bean id="fastJsonHttpMessageConverter"  class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"/>  
  26.     <beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"  >   
  27.         <beans:property name="messageConverters">  
  28.            <beans:list>  
  29.               <beans:ref bean="fastJsonHttpMessageConverter" /><!-- json转换器 -->  
  30.            </beans:list>  
  31.         </beans:property>  
  32.      </beans:bean>  
  33.     <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->  
  34.     <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  35.         <beans:property name="prefix" value="/WEB-INF/views/" />  
  36.         <beans:property name="suffix" value=".jsp" />  
  37.     </beans:bean>  
  38.       
  39.     <beans:bean id="maxUploadSize" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  40.         <beans:property name="maxUploadSize" value="32505856" /><!-- 上传文件大小限制为31M,31*1024*1024 -->  
  41.         <beans:property name="maxInMemorySize" value="4096" />  
  42.     </beans:bean>  
  43.       
  44.       
  45.       
  46. </beans:beans>  


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

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
    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.     <!-- 配置数据源 -->  
    16.     <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
    17.         <property name="url" value="${url}" />  
    18.         <property name="username" value="${username}" />  
    19.         <property name="password" value="${password}" />  
    20.         <property name="initialSize" value="1" />  
    21.         <property name="maxActive" value="20" />  
    22.         <property name="minIdle" value="1" />  
    23.         <property name="maxWait" value="60000" />  
    24.         <property name="validationQuery" value="${validationQuery}" />  
    25.         <property name="testOnBorrow" value="false" />  
    26.         <property name="testOnReturn" value="false" />  
    27.         <property name="testWhileIdle" value="true" />  
    28.         <property name="timeBetweenEvictionRunsMillis" value="60000" />  
    29.         <property name="minEvictableIdleTimeMillis" value="25200000" />  
    30.         <property name="removeAbandoned" value="true" />  
    31.         <property name="removeAbandonedTimeout" value="1800" />  
    32.         <property name="logAbandoned" value="true" />  
    33.         <property name="filters" value="mergeStat" />  
    34.     </bean>  
    35.   
    36.     <!-- myBatis文件 -->  
    37.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    38.         <property name="dataSource" ref="dataSource" />  
    39.         <property name="mapperLocations" value="classpath:com/dahafo/demo/um/mapping/*Mapper.xml" />  
    40.     </bean>  
    41.   
    42.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    43.         <property name="basePackage" value="com.dahafo.demo.um.dao" />  
    44.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />  
    45.     </bean>  
    46.   
    47.     <!-- 配置事务管理器 -->  
    48.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    49.         <property name="dataSource" ref="dataSource" />  
    50.     </bean>  
    51.   
    52.     <!-- 注解方式配置事物 -->  
    53.     <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->  
    54.   
    55.     <!-- 拦截器方式配置事物 -->  
    56.     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">  
    57.         <tx:attributes>  
    58.             <tx:method name="add*" propagation="REQUIRED" />  
    59.             <tx:method name="modify*" propagation="REQUIRED" />  
    60.             <tx:method name="delete*" propagation="REQUIRED" />  
    61.             <tx:method name="find*" propagation="SUPPORTS" />  
    62.             <tx:method name="query" propagation="SUPPORTS" />  
    63.             <tx:method name="search*" propagation="SUPPORTS" />  
    64.             <tx:method name="*" propagation="SUPPORTS" />  
    65.         </tx:attributes>  
    66.     </tx:advice>  
    67.     <aop:config>  
    68.         <aop:pointcut id="transactionPointcut" expression="execution(* com.dahafo.demo.um.service.*.*(..))" />  
    69.         <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />  
    70.     </aop:config>  
    71.     <!-- 配置druid监控spring jdbc -->  
    72.     <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">  
    73.     </bean>  
    74.     <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">  
    75.         <property name="patterns">  
    76.             <list>  
    77.                 <value>com.dahafo.demo.um.service.impl</value>  
    78.             </list>  
    79.               
    80.         </property>  
    81.     </bean>  
    82.     <aop:config>  
    83.         <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />  
    84.     </aop:config>  
    85. </beans

 

很多朋友找我要源码,我整理了一下,【源码地址下载

 

 

  相关解决方案