spring配置hibernate的sessionFactory
1.首先通过dataSource来配置sessionFactory
<!--读入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath*:jdbc.properties</value> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl">update</prop> </props> </property> <property name="mappingResources"> <list> <value>classpath*:/test/domain/MyBean.hbm.xml</value> <value>classpath*:/test/domain/BasicBean.hbm.xml</value> </list> </property> </bean>
2.通过Hibernate.cfg.xml来配置sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean>
<!--<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean>-->
<?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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.SQLServerDialect </property> <property name="connection.url"> jdbc:sqlserver://localhost:1433 </property> <property name="connection.username">sa</property> <property name="connection.password">123456</property> <property name="connection.driver_class"> com.microsoft.sqlserver.jdbc.SQLServerDriver </property> <property name="myeclipse.connection.profile"> SQLServerDriver </property> <property name="show_sql">true</property> <property name="connection.autocommit">true</property> <mapping resource="com/chinasoft/graduation/entity/MyCourse.hbm.xml" /> <mapping resource="com/chinasoft/graduation/entity/Students.hbm.xml" /> <mapping resource="com/chinasoft/graduation/entity/Course.hbm.xml" /> <mapping resource="com/chinasoft/graduation/entity/Annoucement.hbm.xml" /> </session-factory> </hibernate-configuration>