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
- 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));
-
- }
-
- }
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
- <?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>
<?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來操做數據庫。