hibernate3整合spring2时hibernate即用注解又用配置文件情况时spring配置文件的配置写法

时间:2020-11-28 20:38:03

hibernate只用注解时,spring的配置文件的配置如下

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">

		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/oa</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>5519585</value>
		</property>
		<property name="maxActive">
			<value>30</value>
		</property>

	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan">
			<list>
				<value>com.zhongkai.oa.model</value>
			</list>
		</property>

		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.show.sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
			</props>
		</property>
	</bean>

  hibernate即用注解也用配置文件的情况:首先hibernate配置文件内容如下:

<?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>

     <mapping resource="org/jbpm/graph/action/Script.hbm.xml"/>
  
  </session-factory>
</hibernate-configuration>

  然后spring配置文件内容:

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  
	  <property name="driverClassName">
	    <value>com.mysql.jdbc.Driver</value>
	  </property>
	  <property name="url">
	    <value>jdbc:mysql://localhost:3306/oa</value>
	  </property>
	  <property name="username">
	    <value>root</value>
	  </property>
	  <property name="password">
	    <value>5519585</value>
	  </property>
	  <property name="maxActive"><value>30</value></property>
	  
	</bean>
	 
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
 	  <property name="dataSource" ref="dataSource"/>   
    
    <property name="packagesToScan">
      <list>
        <value>com.zhongkai.oa.model</value>
      </list>
    </property>
   
   <property name="configLocation">
 			<value>classpath:hibernate.cfg.xml</value>
 		</property>
   
    <property name="hibernateProperties">
       <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.show.sql">true</prop>
        <prop key="hibernate.format_sql">true</prop>
        <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
    </props>
    
    
    </property>
   
 	</bean>

  在sessionFactoryBean中增加了元素

<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>