hibernateDaoSupport和JdbcDaoSupport的使用

1.HibernateDaoSupport的使用:html

 

1、HibernateDaoSupport是Spring爲Hibernate的DAO提供工具類。此類主要提供了兩個方法:
1. public final HibernateTemplate getHibernateTemplate()
2. public final void setSessionFactory(SessionFactory sessionFactory)

其中,setSessionFactory方法接收來自Spring的applicationContext的依賴注入,接收了配置在Spring中的 SessionFactory實例,getHibernateTemplate方法用來利用剛纔的SessionFactory生成Session,再生成HibernateTemplate來完成數據庫的訪問。java

 

二. 一個小例子:spring

 

  1. package test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  8.   
  9. import cn.ccb.clpm.common.model.bo.documentservice.ObjectDocRelationshipBO;  
  10.   
  11. public class HibernateDaoSupportTest extends HibernateDaoSupport {  
  12.   
  13.     public void test1() {  
  14.         List list = getHibernateTemplate().find("from ObjectDocRelationshipBO o where o.relationId = 1000000068");  
  15.         System.out.println(((ObjectDocRelationshipBO)list.get(0)).getCustomerNum());  
  16.     }  
  17.   
  18.     public static void main(String[] args) {  
  19.         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-daoSupport-test.xml");  
  20.         HibernateDaoSupportTest t = (HibernateDaoSupportTest)ac.getBean("test");  
  21.         t.test1();  
  22. //      t.getHibernateTemplate().save((ObjectDocRelationshipBO)list.get(0));   
  23.           
  24.     }  
  25.   
  26. }  
package test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import cn.ccb.clpm.common.model.bo.documentservice.ObjectDocRelationshipBO; public class HibernateDaoSupportTest extends HibernateDaoSupport { public void test1() { List list = getHibernateTemplate().find("from ObjectDocRelationshipBO o where o.relationId = 1000000068"); System.out.println(((ObjectDocRelationshipBO)list.get(0)).getCustomerNum()); } public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-daoSupport-test.xml"); HibernateDaoSupportTest t = (HibernateDaoSupportTest)ac.getBean("test"); t.test1(); // t.getHibernateTemplate().save((ObjectDocRelationshipBO)list.get(0)); } }

 

配置文件:sql

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.          
  5.         <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  6.         <property name="dataSource">  
  7.             <ref bean="jndiDataSource"/>  
  8.         </property>  
  9.         <property name="hibernateProperties">  
  10.             <props>  
  11.                 <prop key="hibernate.dialect">  
  12.                     org.hibernate.dialect.Oracle9Dialect  
  13.                 </prop>  
  14.                 <prop key="hibernate.show_sql">  
  15.                     true  
  16.                 </prop>  
  17.                 <prop key="hibernate.format_sql">  
  18.                     true  
  19.                 </prop>  
  20.                 <prop key="hibernate.query.factory_class">  
  21.                      org.hibernate.hql.classic.ClassicQueryTranslatorFactory  
  22.                 </prop>  
  23.             </props>  
  24.         </property>  
  25.         <property name="mappingResources">  
  26.             <list>  
  27.                <value>/cn/ccb/clpm/common/model/bo/documentservice/DocIndexBO.hbm.xml</value>  
  28.                <value>/cn/ccb/clpm/common/model/bo/documentservice/ObjectDocRelationshipBO.hbm.xml</value>  
  29.                <value>/cn/ccb/clpm/common/model/bo/documentservice/RawMaterialBO.hbm.xml</value>  
  30.             </list>     
  31.         </property>  
  32.              
  33.         </bean>  
  34.           
  35.         <bean id="test" class="test.HibernateDaoSupportTest">  
  36.                 <property name="sessionFactory">  
  37.                      <ref bean="sessionFactory"/>  
  38.                 </property>  
  39.         </bean>  
  40.   
  41.     <bean id="jndiDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  42.         <property name="driverClassName">  
  43.             <value>oracle.jdbc.driver.OracleDriver</value>  
  44.         </property>  
  45.         <property name="url">  
  46.             <value>jdbc:oracle:thin:@128.64.96.28:1521:clpmkfdb</value>  
  47.         </property>  
  48.         <property name="username">  
  49.             <value>clpmtest</value>  
  50.         </property>  
  51.         <property name="password">  
  52.             <value>clpmtest</value>  
  53.         </property>  
  54.     </bean>     
  55.   
  56. </beans>  
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="jndiDataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql"> true </prop> <prop key="hibernate.format_sql"> true </prop> <prop key="hibernate.query.factory_class"> org.hibernate.hql.classic.ClassicQueryTranslatorFactory </prop> </props> </property> <property name="mappingResources"> <list> <value>/cn/ccb/clpm/common/model/bo/documentservice/DocIndexBO.hbm.xml</value> <value>/cn/ccb/clpm/common/model/bo/documentservice/ObjectDocRelationshipBO.hbm.xml</value> <value>/cn/ccb/clpm/common/model/bo/documentservice/RawMaterialBO.hbm.xml</value> </list> </property> </bean> <bean id="test" class="test.HibernateDaoSupportTest"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="jndiDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <property name="url"> <value>jdbc:oracle:thin:@128.64.96.28:1521:clpmkfdb</value> </property> <property name="username"> <value>clpmtest</value> </property> <property name="password"> <value>clpmtest</value> </property> </bean> </beans>

 

這個過程當中,其實DAO的實現依然藉助了HibernateTemplate的模板訪問方式。只是,HibernateDaoSupport將依賴注入 SessionFactory的工做已經完成,獲取HibernateTemplate的工做也已經完成。注意,這種方法須在Spring的配置文件中配置SessionFactory。

在繼承HibrnateDaoSupport的DAO實現裏,Hibernate Session的管理徹底不須要Hibernate代碼打開,而由Spring來管理。Spring會根據實際的操做,採用「每次事務打開一次 session」的策略,自動提升數據庫訪問的性能。數據庫

 

2. JdbcDaoSupport的使用:session

 

藉助這個類不須要HIbernate框架,也不須要有ORM的對象關聯映射。但它和HibernateDaoSupport很相似,有JdbcTemplate來實現增、刪、改、查操做。oracle

TestA.java:
public class TestA extends JdbcDaoSupport{
    public void test(){
        int n = this.getJdbcTemplate().queryForInt("select max(userid) from User");
        System.out.println(n);
    }
 public static void main(String[] args) {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  TestA ta = (TestA)ctx.getBean("testa");
        ta.test();
 }
} app

須要在Spring的配置文件applicationContext.xml中控制反轉用到鏈接數據庫中的類是注入DataSource,以下:
applicationContext.xml:
        <bean id="testa" class="com.sun.demo.TestA">
        <property name="dataSource">
        <ref bean="myDataSource"/>
        </property>
        </bean>

*******************************************************************************框架

 

其中還有點區別就是: HibernateDaoSupport是使用HQL語言來查詢的,而JdbcDaoSupport則是使用SQL來查詢,能夠工具

更加簡單方便的使用咱們日常的SQL來操做數據庫。

相關文章
相關標籤/搜索