Spring和Hibernate整合xml配置
阿新 • • 發佈:2018-12-11
1、基於xml的配置
dbconfig.properties資料來源檔案配置
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/databaseName
user=root
password=root
initialPoolSize=6
minPoolSize=5
maxPoolSize=300
maxIdleTime=60
acquireIncrement=5
idleConnectionTestPeriod=60
基於xml的beans.xml 配置檔案
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- 引入properties屬性檔案 --> <context:property-placeholder location="classpath:dbconfig.properties" /> <!-- 配置資料來源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${driverClass}" /> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <!--初始化時獲取的連線數,取值應在minPoolSize與maxPoolSize之間。Default: 3 --> <property name="initialPoolSize" value="${initialPoolSize}" /> <!--連線池中保留的最小連線數。 --> <property name="minPoolSize" value="${minPoolSize}" /> <!--連線池中保留的最大連線數。Default: 15 --> <property name="maxPoolSize" value="${maxPoolSize}" /> <!--最大空閒時間,60秒內未使用則連線被丟棄。若為0則永不丟棄。Default: 0 --> <property name="maxIdleTime" value="${maxIdleTime}" /> <!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。Default: 3 --> <property name="acquireIncrement" value="${acquireIncrement}" /> <!--每60秒檢查所有連線池中的空閒連線。Default: 0 --> <property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mappingLocations" value="com/hsj/domain/*.hbm.xml"></property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.cache.use_second_level_cache=true hibernate.cache.provider_class=org.hibernate.cache.OSCacheProvider </value> </property> </bean> <!-- 定義事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 使用xml配置事務 --> <aop:config> <aop:pointcut expression=" execution(* com.hsj.dao.bean.*.*(..))" id="myPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/> </aop:config> <!-- 配置通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="get*" propagation="NEVER" read-only="true"/> </tx:attributes> </tx:advice> <bean id="personDao" class="com.hsj.dao.bean.PersonDaoBean"> <property name="sessionFactory" ref="sessionFactory"></property> <!-- <property name="studentDao" ref="studentDao"/> --> </bean> <bean id="studentDao" class="com.hsj.dao.bean.StudentDaoBean"> <property name="sessionFactory" ref="sessionFactory"></property> <property name="personDao" ref="personDao"></property> </bean> </beans>
實體類配置檔案
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hsj.domain"> <class name="Person" table="t_person"> <id name="id"> <!-- 主鍵的生成策略 --> <generator class="native"></generator> </id> <property name="name" length="30" unique="true"></property> <property name="sex" length="6"></property> <property name="age"></property> <property name="birthday" type="date"></property> </class> </hibernate-mapping>
2、基於註解的配置
基於註解配置beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- 開啟掃描器開關,並指定掃描範圍 --> <context:component-scan base-package="com.hsj"></context:component-scan> <!-- 引入properties屬性檔案 --> <context:property-placeholder location="classpath:dbconfig.properties" /> <!-- 配置資料來源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${driverClass}" /> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <!--初始化時獲取的連線數,取值應在minPoolSize與maxPoolSize之間。Default: 3 --> <property name="initialPoolSize" value="${initialPoolSize}" /> <!--連線池中保留的最小連線數。 --> <property name="minPoolSize" value="${minPoolSize}" /> <!--連線池中保留的最大連線數。Default: 15 --> <property name="maxPoolSize" value="${maxPoolSize}" /> <!--最大空閒時間,60秒內未使用則連線被丟棄。若為0則永不丟棄。Default: 0 --> <property name="maxIdleTime" value="${maxIdleTime}" /> <!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。Default: 3 --> <property name="acquireIncrement" value="${acquireIncrement}" /> <!--每60秒檢查所有連線池中的空閒連線。Default: 0 --> <property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}" /> </bean> <!-- 使用註解配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 指定掃描實體範圍的包,包中的類都可以被掃描到 --> <property name="packagesToScan" value="com.hsj.domain"></property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.cache.use_second_level_cache=true hibernate.cache.provider_class=org.hibernate.cache.OSCacheProvider </value> </property> </bean> <!-- 定義事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 採用註釋的方式進行事務控制 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>