首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring与Hibernate两种结合方式 (转)

2012-09-21 
Spring与Hibernate两种组合方式 (转)Spring与Hibernate大致有两种组合方式,主要区别是一种是在Hibernate中

Spring与Hibernate两种组合方式 (转)

Spring与Hibernate大致有两种组合方式,主要区别是一种是在Hibernate中的hibernate.cfg.xml中配置数据源,一种是借助Spring的jdbc方式在Spring的applicationContext.xml文件中配置数据源,然后在Spring配置sessionFactory的bean有些区别

?

下面大致的说明一下

第一种

1.hibernate.cfg.xml文件

?

  <xml version='1.0' encoding='utf-8'?>   <!DOCTYPE hibernate-configuration SYSTEM    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">     <hibernate-configuration>   <session-factory>           <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriverproperty> <property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=acegi;SelectMethod=cursorproperty> <property name="connection.username">saproperty> <property name="connection.password">serverproperty> <property name="show_sql">trueproperty>   <mapping resource=""/> </session-factory>     <hibernate-configuration> 

2.在spring配置sessionFactory

?

   <bean id="sessionFactory"    value="classpath:hibernate.cfg.xml"></property>    </bean>    <bean id="txManager" ref="sessionFactory"></property>    </bean>    <tx:annotation-driven transaction-manager="txManager"/>

?或者实现hibernate零配置

?

   <bean id="sessionFactory"    value="classpath:hibernate.cfg.xml"></property>                 <property name="packagesToScan" value="com/wch/entity/"></property>   </bean>    <bean id="txManager" ref="sessionFactory"></property>    </bean>    <tx:annotation-driven transaction-manager="txManager"/> 
?

第二种

<bean id="dataSource" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>     <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=acegi;SelectMethod=cursor"/>     <property name="username" value="sa"/>     <property name="password" value="server"/>  </bean> <bean id="sessionFactory" value="dataSource"></property> <property name="mappingResources">         <list>             <value>user.hbm.xml</value>         </list>     </property>     <property name="hibernateProperties">         <props>            <prop key="hibernate.show_sql">true</prop> </props>      </property> </bean>        <bean id="txManager" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager"/> 

?

?或者实现hibernate的零配置

?

<bean id="dataSource" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>     <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=acegi;SelectMethod=cursor"/>     <property name="username" value="sa"/>     <property name="password" value="server"/>  </bean> <bean id="sessionFactory" value="dataSource"></property><property name="packagesToScan" value="com/wch/entity/"></property> </bean>        <bean id="txManager" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager"/> 
?

?

下面是一些配置例子

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>    <!-- dataSource -->    <bean id="dataSource"        value="sa"></property>        <property name="password" value="11111111"></property>    </bean>    <!--hibernate事务-->    <bean id="transactionManager"        />        </property>    </bean>    <!-- 基础事务代理 -->    <bean id="baseTxProxy" abstract="true"        />        </property>        <property name="transactionAttributes">            <props>                <prop key="save*">PROPAGATION_REQUIRED,-Throwable</prop>                <prop key="remove*">                    PROPAGATION_REQUIRED,-Throwable                </prop>                <prop key="merge">PROPAGATION_REQUIRED,-Throwable</prop>                <prop key="update">PROPAGATION_REQUIRED,-Throwable</prop>                <prop key="do*">PROPAGATION_REQUIRED,-Throwable</prop>            </props>        </property>    </bean>    <!-- sqlMapClient -->    <bean id="sqlMapClient"        />        </property>        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>                <prop key="hibernate.show_sql">false</prop>                <prop key="hibernate.format_sql">false</prop>                <prop key="hibernate.use_sql_comments">false</prop>                <!-- 为单向关联(一对一, 多对一)的外连接抓取(outer join fetch)树设置最大深度. 值为0意味着将关闭默认的外连接抓取 -->                <prop key="hibernate.max_fetch_depth">3</prop>                <!-- 为Hibernate关联的批量抓取设置默认数量 -->                <prop key="hibernate.default_batch_fetch_size">8</prop>                <!-- 强制Hibernate按照被更新数据的主键,为SQL更新排序。这么做将减少在高并发系统中事务的死锁。 -->                <prop key="hibernate.order_updates">true</prop>                <!-- session在事务完成后将被自动清洗(flush) -->                <prop key="hibernate.transaction.flush_before_completion">true</prop>                <!-- Oracle限制那些通过JDBC驱动传输的字节数组的数目. 如果你希望使用二进值 (binary)或 可序列化的 (serializable)类型的大对象, 你应该开启 hibernate.jdbc.use_streams_for_binary属性.  -->                <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>            </props>        </property>        <property name="mappingResources">            <list>                <value>com/xxx/cqry/domain/Client.hbm.xml</value>            </list>        </property>    </bean>    <!-- locator -->    <bean name="cqry.locator"        />        </property>    </bean>    <!-- daoFacade -->    <bean id="daoFacade" />        </property>    </bean>    <bean id="baseDao" />        </property>        <property name="dataSource">            <ref bean="dataSource" />        </property>        <property name="sqlMapClient">            <ref local="sqlMapClient" />        </property>    </bean>    <bean id="clientDao" parent="baseDao"></bean>    <!-- baseServer -->    <bean id="baseServer"        />        </property>    </bean>    <bean id="clientServer" parent="baseTxProxy">        <property name="target">            <bean />        </property>    </bean></beans>
?

?

?

下面是PostgresSQL and MySQL hibernate.cfg.xml examples

?

?

PostgreSQL Version:<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="connection.url">jdbc:postgresql://localhost/firsthibernate</property><property name="connection.username">postgres</property><property name="connection.driver_class">org.postgresql.Driver</property><property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property><property name="connection.password">p</property> <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>    <!--  thread is the short name for      org.hibernate.context.ThreadLocalSessionContext      and let Hibernate bind the session automatically to the thread    -->    <property name="current_session_context_class">thread</property>    <!-- this will show us all sql statements -->    <property name="hibernate.show_sql">true</property><!-- mapping files --><mapping resource="de/laliluna/example/Honey.hbm.xml" /></session-factory></hibernate-configuration>MySQL Version:<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="connection.url">jdbc:mysql://localhost/firsthibernate</property><property name="connection.username">root</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.password">r</property> <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>    <!--  thread is the short name for      org.hibernate.context.ThreadLocalSessionContext      and let Hibernate bind the session automatically to the thread    -->    <property name="current_session_context_class">thread</property>    <!-- this will show us all sql statements -->    <property name="hibernate.show_sql">true</property><!-- mapping files --><mapping resource="de/laliluna/example/Honey.hbm.xml" /></session-factory></hibernate-configuration>

热点排行