spring 与hibernate的整合
分两种情况 一种是没有hibernate.cfg.xml文件
applicationContext.xml 文件配置如下
<!--config dataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"> </property> <property name="username" value="alex"></property> <property name="password" value="vallen"></property> </bean> <!-- config sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/alex/po/TUser.hbm.xml</value> <value>com/alex/po/father.hbm.xml</value> <value>com/alex/po/son.hbm.xml</value> </list> </property> </bean>
有hibernate.cfg.xm 文件 的情况
hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="hibernate.connection.driver_class"></property> <property name="hibernate.connection.url">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.username">alex</property> <property name="hibernate.connection.password">vallen</property> <property name="hibernate.format_sql">true</property> <mapping resource="*.hbm.xml" /> </session-factory> </hibernate-configuration>
applicationContext.xml配置如下
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean>