标签:
最近做了个maven管理的springmvc+spring+mybatis,还用到了阿里巴巴的 fastjson和druid连接池,配置文件如下
pom.xml文件
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="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
-
-
-
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring/root-context.xml</param-value>
- </context-param>
-
- <filter>
- <filter-name>encodingFilter</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>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
-
-
-
-
- <servlet>
- <servlet-name>appServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>appServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
-
- </web-app>
root-context.xml spring的主配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- ">
- <context:annotation-config />
- <context:component-scan base-package="com.dahafo.demo.um" />
- <import resource="spring-mybatis.xml"/>
- </beans>
servlet-context.xml springmvc的主配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans:beans xmlns="http://www.springframework.org/schema/mvc"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:beans="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
-
-
-
-
- <annotation-driven />
- <context:component-scan base-package="com.dahafo.demo.um.controller" />
-
-
- <resources mapping="/resources/**" location="/resources/" />
- <resources mapping="/css/**" location="/css/" />
- <resources mapping="/images/**" location="/images/" />
- <resources mapping="/js/**" location="/js/" />
-
-
- <beans:bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
-
- <beans:bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"/>
- <beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
- <beans:property name="messageConverters">
- <beans:list>
- <beans:ref bean="fastJsonHttpMessageConverter" />
- </beans:list>
- </beans:property>
- </beans:bean>
-
- <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <beans:property name="prefix" value="/WEB-INF/views/" />
- <beans:property name="suffix" value=".jsp" />
- </beans:bean>
-
- <beans:bean id="maxUploadSize" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <beans:property name="maxUploadSize" value="32505856" />
- <beans:property name="maxInMemorySize" value="4096" />
- </beans:bean>
-
-
-
- </beans:beans>
spring-mybatis.xml mybatis的配置文件,数据源用 的是阿里巴巴的 druid
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- ">
-
- <context:property-placeholder location="classpath:jdbc.properties" />
-
-
- <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" />
- <property name="initialSize" value="1" />
- <property name="maxActive" value="20" />
- <property name="minIdle" value="1" />
- <property name="maxWait" value="60000" />
- <property name="validationQuery" value="${validationQuery}" />
- <property name="testOnBorrow" value="false" />
- <property name="testOnReturn" value="false" />
- <property name="testWhileIdle" value="true" />
- <property name="timeBetweenEvictionRunsMillis" value="60000" />
- <property name="minEvictableIdleTimeMillis" value="25200000" />
- <property name="removeAbandoned" value="true" />
- <property name="removeAbandonedTimeout" value="1800" />
- <property name="logAbandoned" value="true" />
- <property name="filters" value="mergeStat" />
- </bean>
-
-
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mapperLocations" value="classpath:com/dahafo/demo/um/mapping/*Mapper.xml" />
- </bean>
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.dahafo.demo.um.dao" />
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
- </bean>
-
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
-
-
-
-
-
- <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="modify*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="find*" propagation="SUPPORTS" />
- <tx:method name="query" propagation="SUPPORTS" />
- <tx:method name="search*" propagation="SUPPORTS" />
- <tx:method name="*" propagation="SUPPORTS" />
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut id="transactionPointcut" expression="execution(* com.dahafo.demo.um.service.*.*(..))" />
- <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
- </aop:config>
-
- <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
- </bean>
- <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
- <property name="patterns">
- <list>
- <value>com.dahafo.demo.um.service.impl</value>
- </list>
-
- </property>
- </bean>
- <aop:config>
- <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
- </aop:config>
- </beans>
很多人都发私信要源码,但是由于时间比较长了,源码我实在是找不到了,等有时间了,我再搭建一个,并开放源码,谢谢大家关注。
》》源码地址获取
springmvc + mybatis整合详细,及遇到的问题请参看以下资料:
参考资料:
http://www.springmvc,net/detail/6074493.html
http://my.spring.net/wangbiglei/blog/489583
http://my.springmvc.net/wangbiglei/blog/489604
maven搭建springmvc+spring+mybatis实例
标签:
原文地址:http://www.cnblogs.com/lucene110/p/4857811.html