一、Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库连接url地址 --> <property name="connection.url"> jdbc:mysql://localhost:3306/spring </property> <!-- 数据库连接的用户名 --> <property name="connection.username">root</property> <!-- 数据库连接的用户密码 --> <property name="connection.password">root</property> <!-- 数据库连接驱动 --> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 数据库方言 --> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- c3p0的配置 --> <!-- hibernate4.org.hibernate.c3p0.internal.C3P0ConnectionProvider --> <property name="hibernate.connection.provider_class"> org.hibernate.c3p0.internal.C3P0ConnectionProvider </property> <!-- 最小数量 --> <property name="hibernate.c3p0.min_size">3</property> <!-- 最大数量 --> <property name="hibernate.c3p0.max_size">10</property> <!--设定数据库连接的过期时间,以秒为单位, 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 --> <property name="hibernate.c3p0.timeout">120</property> <!--每3000秒检查所有连接池中的空闲连接 以秒为单位 --> <property name="hibernate.c3p0.idle_test_period">3000</property> <!-- 当连接池耗尽并接到获得连接的请求,则新增加连接的数量 --> <property name="hibernate.c3p0.acquire_increment">5</property> <!-- 缓存 Statement 对象的数量 --> <property name="hibernate.c3p0.max_statements">100</property> <!-- 是否每次连接都验证连接是否可用 --> <property name="hibernate.c3p0.validate">true</property> <!-- 显示sql语句 --> <property name="hibernate.show_sql">true</property> <!-- hbm2ddl --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 映射文件 --> <mapping resource="com/buslines/domain/Admin.hbm.xml" /> </session-factory> </hibernate-configuration> |
二、Spring相关配置
(1)applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- hibernate c3p0 sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- 创建模板对象 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 封装dao对象 --> <bean id="hibernateDaoSupport" class="org.springframework.orm.hibernate4.support.HibernateDaoSupport" abstract="true"> <property name="hibernateTemplate" ref="hibernateTemplate" /> </bean> <!--事务管理器 --> <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 事务管理器的通知 --> <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager"> <tx:attributes> <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" /> <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" /> <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- aop配置 --> <aop:config> <aop:pointcut expression="execution(* com.buslines.service..*.*(..))" id="txpoint" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txpoint" /> </aop:config> <import resource="spring-dao.xml"/> <import resource="spring-service.xml"/> <import resource="spring-action.xml"/> </beans> |
(2)spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置dao对象 --> <bean id="baseDaoImpl" class="com.buslines.dao.impl.BaseDaoImpl" parent="hibernateDaoSupport"/> <bean id="adminDaoImpl" class="com.buslines.dao.impl.AdminDaoImpl" parent="baseDaoImpl"/> </beans> |
(3)spring-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置dao对象 --> <bean id="baseServiceImpl" class="com.buslines.service.impl.BaseServiceImpl"> <property name="baseDao" ref="baseDaoImpl" /> </bean> <bean id="adminServiceImpl" class="com.buslines.service.impl.AdminServiceImpl" parent="baseServiceImpl"> <property name="adminDao" ref="adminDaoImpl" /> </bean> </beans> |