1. 程式人生 > >使用Spring整合Hibernate配置,根據實體類自動建立表

使用Spring整合Hibernate配置,根據實體類自動建立表

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">


<!-- 開啟Spring註解支援 -->
<context:annotation-config/>
<!-- 設定Spring在哪些包找annotation,註解 -->
<context:component-scan base-package="com.wxl"></context:component-scan>
<!-- 引入properties檔案 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driver}">
</property>
<property name="url"
value="${jdbc.url}">
</property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 通過註解的方式生成表 ,使用AnnotationSessionFactoryBean類-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 其他情況使用org.springframework.orm.hibernate3.LocalSessionFactoryBean -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 設定Spring去哪個包查詢相應的實體類,使用註解建立表時使用 -->
<property name="packagesToScan">
<value>com.wxl.model</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">false</prop>  
                <prop key="hibernate.format_sql">false</prop>      
                <!-- 設定自動建立Hibernate.hbm檔案 -->
                <prop key="hibernate.hbm2ddl.auto">create </prop>
</props>

</property>

<!--<property name="mappingResources">
<list>
<value>com/lyly123/bean/Users.hbm.xml</value>
<value>com/lyly123/bean/News.hbm.xml</value>
<value>com/lyly123/bean/Model.hbm.xml</value></list>
</property>-->
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="serviceMethod" expression = "execution(* com.wxl.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
</aop:config>
</beans>