使用 数据库配置文件 、spring.xml、c3p0连接池 链接Mysql数据库

 

1. Spring.xml

 

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans-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/context
			http://www.springframework.org/schema/context/spring-context-3.0.xsd
			http://www.springframework.org/schema/aop
			http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
           
    <tx:annotation-driven transaction-manager="transactionManager"/>
	<!-- 配置自动扫描的包: 需要加入 aop 对应的包 -->
	<context:component-scan base-package="com.hmx"/>

	<!-- 导入配置文件  -->
	<context:property-placeholder location="classpath:config/database/mysql.properties"/>

	<!-- 配置c3p0  -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="${driverClass}"/>
		<property name="jdbcUrl" value="${jdbcUrl}"/>
		<property name="properties">
            <props>
                <prop key="c3p0.acquire_increment">${c3p0.acquire_increment}</prop>
                <prop key="c3p0.idle_test_period">${c3p0.idle_test_period}</prop>
                <prop key="c3p0.timeout">${c3p0.timeout}</prop>
                <prop key="c3p0.max_size">${c3p0.max_size}</prop>
                <prop key="c3p0.max_statements">${c3p0.max_statements}</prop>
                <prop key="c3p0.min_size">${c3p0.min_size}</prop>
                <prop key="user">${user}</prop> 
                <prop key="password">${password}</prop>
            </props>
		</property>
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 配置数据源  -->
		<property name="dataSource" ref="dataSource"/>
		<!-- 配置hibernate原生属性  -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="current_session_context_class">${current_session_context_class}</prop>
			</props>
		</property>
		<!-- 扫描PO包里面的映射文件,只能扫描PO包,PO下面的子包不会扫描  -->
		<property name="packagesToScan" value="com.hmx.bean.po"/>
	</bean>
	
	<!-- 配置Spring的声明式事物 @Transactional注解要配置在ServiceImpl上 -->
	<!-- 1.配置事物管理器-->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        	<property name="sessionFactory" ref="sessionFactory" />
       </bean>
</beans>


2.mysql.properties

 

#数据库连接类
driverClass = com.mysql.jdbc.Driver
#连接资源
jdbcUrl = jdbc\:mysql\://127.0.0.1\:3306/hmx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
#用户名
user = root
#密码
password = 123456
#当连接池里面的连接用完的时候,C3P0一下获取的新的连接数
c3p0.acquire_increment = 2
#每隔120秒检查连接池里的空闲连接,单位是秒
c3p0.idle_test_period = 120
#获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒
c3p0.timeout = 1000
#最大连接数
c3p0.max_size = 100
#最大的PreparedStatement的数量
c3p0.max_statements = 100
#最小连接数
c3p0.min_size = 20
#每次都验证连接是否可用
c3p0.validate = true

#数据库方言
hibernate.dialect = org.hibernate.dialect.MySQLDialect
#连接池大小
hibernate.connection.pool_size = 5
#是否自动生成表
hibernate.hbm2ddl.auto = update
#是否格式化SQL
hibernate.format_sql = true
#是否显示SQL
hibernate.show_sql = true
#设定会话的线程绑定
current_session_context_class = thread



你可能感兴趣的:(spring)