spring ioc 配置hibernate sessionFactory

1 采用hibernate配置文件进行配置

2 采用spring ioc配置

<!-- 配置SessionFactory  -->
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource">
       <ref bean="dataSource"/>
   </property>
   
   <property name="mappingResources">
       <list>
                <value>com/.../.../model/ACL.hbm.xml</value>
                <value>com/.../.../model/Module.hbm.xml</value>
                <value>com/.../Operation.hbm.xml</value>
       </list>
   </property>
  </bean>
  
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/framework" />
      <property name="username" value="sa"/>
      <property name="password" value="sa"/>
  </bean>

3 采用spring annotation ioc配置

<!-- 配置SessionFactory  -->
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
   <property name="dataSource">
       <ref bean="dataSource"/>
   </property>
   
   <!-- 扫描指定目录下的含有Entity特征的class,等同于配置mappingResources -->
   <property name="packagesToScan">
   <list>
    <value>com.**.model</value>
   </list>
  </property>
  
  <property name="hibernateProperties">
      <props>
          <prop key="hibernate.show_sql">true</prop>
          <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
          <prop key="hibernate.hbm2ddl.auto">update</prop>
          <prop key="hibernate.default_schema">framework</prop>
      </props>
  </property>
  </bean>
  
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/framework" />
      <property name="username" value="sa"/>
      <property name="password" value="sa"/>
  </bean>

 

你可能感兴趣的:(spring ioc 配置hibernate sessionFactory)