SSH_DEMO applicationContext.xml中使用hibernate连接数据库

 <context:property-placeholder location="classpath:jdbc.properties"/>
 <!-- 以下方法为使用hibernate直接连接数据库 -->
 <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <!-- property池启动时的初始值  -->
        <property name="password" value="${jdbc.password}"/>
        <!-- 连接name="initialSize" value="${initialSize}"/>-->
        <property name="initialSize" value="1"/>
        <!-- 连接池的最大值
        <property name="maxActive" value="500"/>
         -->
        <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
        <property name="maxIdle" value="2"/>
        <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
        <property name="minIdle" value="1"/>

    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
          p:dataSource-ref="dataSource">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
                <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
            </props>
        </property>
        <!-- 加载hibernate的jpa注解形式实体 -->
        <property name="packagesToScan">
            <list>
                <value>com.nbad.*</value>
            </list>
        </property>
    </bean>
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- 配置声明式事务:方法一,在Service实现类或者public实现方法上使用注解@Transactional,则此类或方法就会启用事务机制 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

jdbc.properties

#######################  DB Connection Config  #######################

###—————– DB Type —————–

#the database of the application:mysql|sqlserver|oracle

databaseType=mysql

#databaseType=sqlserver

#databaseType=oracle

###—————– MySQL5+ —————–

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8

jdbc.username=root

jdbc.password=root

hibernate.dialect=org.hibernate.dialect.MySQLDialect

###—————– SqlServer2005+ —————–

#jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

#jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=sampledb

#jdbc.username=sa

#jdbc.password=123456

#hibernate.dialect=org.hibernate.dialect.SQLServerDialect

###—————– Oracle10g+ —————–

#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver

#jdbc.url=jdbc:oracle:thin:@localhost:1521:orac10g

#jdbc.username=scott

#jdbc.password=scott123

#hibernate.dialect=org.hibernate.dialect.OracleDialect

###—————– JNDI —————–

#jndi.name=myjndi123

###—————– Hibernate —————–

hibernate.show_sql=true

hibernate.format_sql=true

hibernate.hbm2ddl.auto=update

hibernate.jdbc.fetch_size=100

hibernate.jdbc.batch_size=20

hibernate.cache.use_second_level_cache=true

hibernate.cache.use_query_cache=true

hibernate.memcached.cacheTimeSeconds=10800

#Hibernate4

hibernate.cache.region.factory_class=org.hibernate.cache.EhCacheRegionFactory

#Hibernate3

#hibernate.cache.region.factory_class=org.hibernate.cache.EhCacheProvider

###—————– C3P0 —————–

c3p0.maxPoolSize=50

c3p0.minPoolSize=1

c3p0.initialPoolSize=1

c3p0.maxIdleTime=20

###—————– DBCP —————–

dbcp.maxActive=50

dbcp.maxIdle=50

dbcp.minIdle=1

dbcp.maxWait=10000

dbcp.initialSize=1