---
title: "springmvc中数据源配置相关"
author: "jeans123"
date: "2015-05-25T16:00:29Z"
tags: []
categories: ["企业架构"]
slug: "springmvc"
draft: false
lastmod: "2015-05-25T16:00:29Z"
id: 2214053
url: "https://jeansmo.iteye.com/blog/2214053"
---

&lt;!--数据源\\数据连接\\事务相关配置--&gt;  
    &lt;!--数据源\\数据连接池配置 druid--&gt;  
    &lt;bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"&gt;  
        &lt;property name="driverClassName" value="${jdbc.driverClassName}"/&gt;  
        &lt;property name="url" value="${jdbc.url}"/&gt;  
        &lt;property name="username" value="${jdbc.username}"/&gt;  
        &lt;property name="password" value="${jdbc.password}"/&gt;  
        &lt;property name="maxActive" value="30"/&gt;  
        &lt;property name="maxIdle" value="10"/&gt;  
        &lt;property name="maxWait" value="1000"/&gt;  
        &lt;property name="defaultAutoCommit" value="true"/&gt;  
    &lt;/bean&gt;

  &lt;!--数据源\\数据连接池配置 c3p0--&gt;  
     &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt;  
        &lt;property name="driverClass" value="${jdbc.driverClassName}"/&gt;  
        &lt;property name="jdbcUrl" value="${jdbc.url}"/&gt;  
        &lt;property name="user" value="${jdbc.username}"/&gt;  
        &lt;property name="password" value="${jdbc.password}"/&gt;  
        &lt;property name="maxPoolSize" value="30"/&gt;  
        &lt;property name="maxIdleTime" value="1000"/&gt;  
    &lt;/bean&gt;

     &lt;!--MyBatis特有配置,MapperScannerConfigurer将Mapper接口生成代理注入到Spring--&gt;  
       &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt;  
              &lt;property name="dataSource" ref="dataSource" /&gt;  
              &lt;property name="configLocation" value="classpath:conf/sqlMapConfig.xml"/&gt;  
       &lt;/bean&gt;  
       &lt;bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"&gt;  
              &lt;property name="basePackage" value="com.kuanxue.weixin.DAO"/&gt;  
              &lt;property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /&gt;  
       &lt;/bean&gt;

    &lt;!--bo对象与数据库关系表的注解.LocalSessionFactoryBean是将hbm.xml文件中的对象映射到数据库表--&gt;  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;  
        &lt;property name="dataSource" ref="dataSource"/&gt;  
        &lt;property name="annotatedPackages"&gt;  
            &lt;list&gt;  
                &lt;value&gt;com.lms.quartz.bo&lt;/value&gt;  
            &lt;/list&gt;  
        &lt;/property&gt;  
        &lt;property name="hibernateProperties"&gt;  
            &lt;props&gt;  
                &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5InnoDBDialect&lt;/prop&gt;  
                &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;  
                &lt;prop key="hibernate.show\_sql"&gt;${hibernate.show\_sql}&lt;/prop&gt;  
                &lt;prop key="hibernate.query.substitutions"&gt;${hibernate.query.substitutions}&lt;/prop&gt;  
                &lt;prop key="hibernate.cache.use\_query\_cache"&gt;${hibernate.cache.use\_query\_cache}&lt;/prop&gt;  
                &lt;prop key="hibernate.cache.provider\_class"&gt;${hibernate.cache.provider\_class}&lt;/prop&gt;  
                &lt;prop key="hibernate.cache.use\_second\_level\_cache"&gt;${hibernate.cache.use\_second\_level\_cache}&lt;/prop&gt;  
            &lt;/props&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;

     &lt;!--事务控制--&gt;  
     &lt;!--HibernateTemplate与HibernateTransactionManager功能类似,只是HibernateTransactionManager会自动关闭       session,但有可能会出现LazyInitializationException,解决办法是:通过OpenSessionInViewFilter来保证延迟加载不       会出现错误.该事务与DataSourceTransactionManager事务相同功能,不过HibernateTransactionManager能够保证 jdbctemplate所获得con与hibernate获得的con在同一个事务中,DataSourceTransactio管理从datasource数据源获得的连接--&gt;  
        &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
              &lt;property name="sessionFactory" ref="sessionFactory"/&gt;  
        &lt;/bean&gt;  
[  
&lt;filter&gt;  
&lt;filter-name&gt;opensession&lt;/filter-name&gt;  
&lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt;  
&lt;/filter&gt;  
&lt;filter-mapping&gt;  
&lt;filter-name&gt;opensession&lt;/filter-name&gt;  
&lt;url-pattern&gt;\*.do&lt;/url-pattern&gt;  
&lt;/filter-mapping&gt;  
]

     &lt;!--其它--&gt;