日期:2014-05-16  浏览次数:20468 次

Struts+Spring+Hibernate连接数据库的三个方法
Struts+Spring+Hibernate连接数据库的三个方法

第一种在Spring applicationContext.xml中连接: 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
     <property name="driverClassName">   
         <value>oracle.jdbc.driver.OracleDriver</value>   
     </property>   
     <property name="url">   
         <value>jdbc:oracle:thin:@localhost:1521:test</value>   
     </property>          
     <property name="username">   
         <value>cpiclh</value>   
     </property>   
     <property name="password">   
         <value>cpiclh</value>   
     </property>   
</bean>   
  
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
     <property name="dataSource">   
         <ref local="dataSource" />   
     </property>   
     <property name="mappingResources">   
           <list>   
               <value>com/Hibernate/Pojo/FactUsers.hbm.xml</value>   
           </list>   
       </property>   
     <property name="hibernateProperties">   
         <props>   
             <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>   
             <prop key="hibernate.show_sql">true</prop>   
         </props>   
     </property>   
</bean>  


第二种在Hibernate hibernate.cfg.xml中连接: 

<session-factory>   
         <property name="connection.username">root</property>   
         <property name="connection.url">   
             jdbc:oracle:thin:@192.168.0.1:1521:test   
         </property>   
         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>   
           
         <property name="connection.password">root</property>   
         <property name="connection.driver_class">   
             oracle.jdbc.driver.OracleDriver   
         </property>   
         <mapping resource="com/hibernate/pojo/FactUsers.hbm.xml" />   
     </session-factory>  


第三种在Tomcat中的 apache-tomcat-5.5.17\conf\Catalina目录下放一个 
和目录同名的XML,内容如下 

<Context path="" docBase="ROOT">   
     <Resource name="jdbc/db" auth="Container" type="javax.sql.DataSource" removeAbandoned="true" removeAbandonedTimeout="60"  
                username="root" password="root" driverClassName="oracle.jdbc.driver.OracleDriver"  
                url="jdbc:oracle:thin:@192.168.0.1:1521:test"/>   
</Context>