spring,springmvc,jpa,mybatis整合配置

来源:互联网 发布:淘宝神笔模块怎么添加 编辑:程序博客网 时间:2024/05/05 20:47

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>mvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<filter>
<filter-name>encode</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>
</filter>
<filter-mapping>
<filter-name>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<servlet>
<servlet-name>gy</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>gy</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


</web-app>



..-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<context:component-scan base-package="cn.gson" />
<mvc:default-servlet-handler />
<mvc:annotation-driven />


<!-- 指定静态资源的路径映射 -->
<mvc:resources location="/WEB-INF/images/" mapping="/images/**" />


<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>


<!-- 配置拦截器 -->


<mvc:interceptors> 默认拦截所有请求 <bean class="cn.gson.spring.mvc3.NatureInterceptor" 
/> 可以配置拦截指定请求 <mvc:interceptor> 需要拦截的请求 <mvc:mapping path="/user" /> <mvc:mapping 
path="/user/*" /> 排除的请求 <mvc:exclude-mapping path="/user/add" /> <bean class="cn.gson.spring.mvc3.LoginInterceptor" 
/> </mvc:interceptor> </mvc:interceptors> 


<!-- 集成自定义的转换器 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 
<property name="converters"> <list> 自定义的类型转换器 <bean class="cn.gson.spring.mvc3.PhoneConverter" 
/> <bean class="cn.gson.spring.mvc3.DateConverter" /> </list> </property> 
</bean> 


<!-- 验证框架的支持配置 -->


<!-- 指定消息资源文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:ValidationMessages"></property>
<property name="fileEncodings" value="utf-8"></property>
<property name="cacheSeconds" value="120" />
</bean>


<!-- 验证框架 -->
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<property name="validationMessageSource" ref="messageSource"></property>
</bean>


<!-- 为mvc注册类型转换器和验证器 -->
<!-- <mvc:annotation-driven validator="validator" conversion-service="conversionService" 
/> -->




<!-- 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 文件最大上传大小100M->104857600 -->
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="4096" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean>


<!-- 继承fastjson输出 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 配置Fastjson支持 -->
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
<property name="features">
<list>
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>




<!-- 数据源的配置 -->
<!-- 数据源公共部分 -->
<!-- 加载配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db-resource.properties</value>
</list>
</property>
</bean>


<!-- 创建一个带连接池的dataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource">
<bean class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="${db.driver.class}" />
<property name="driverUrl" value="${db.url}" />
<property name="user" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maximumConnectionCount" value="${proxool.maxConnCount}" />
<property name="minimumConnectionCount" value="${proxool.minConnCount}" />
<property name="statistics" value="${proxool.statistics}" />
<property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}" />
<property name="trace" value="${proxool.trace}" />
</bean>
</property>
</bean>


<!-- jpa相关的整合 start -->
<!-- 指定jpa的Dao所在根包 -->
<jpa:repositories base-package="cn.gson.gy_crm.model.dao.jpa" />


<!-- Entity管理工厂,指定数据源 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>


<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 开启注解事务,指定事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- jpa相关的整合 end -->


<!-- MyBatis整合配置 start -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 指定mybatis的配置文件的路径 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath*:mappers/*.xml"></property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>


<!-- MyBatis Mapper接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- MyBatis DAO接口所在包名,basePackage指定的包下面的类会自动查找 -->
<property name="basePackage" value="cn.gson.gy_crm.model.dao.mappers" />
</bean>


<!-- 配置事务管理器 -->
<bean id="mybatisTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>


<!-- 开启注解事务,指定事务管理器 -->
<tx:annotation-driven transaction-manager="mybatisTransactionManager" />
<!-- MyBatis整合配置 end -->
</beans>
0 0
原创粉丝点击