Spring学习笔记:spring集成hibernate

Spring集成hibernate有两种方式:1 完全使用hibernate编写dao层,把dao注入到spring由spring来管理生命周期,这种方式的好处是dao层与spring没有耦合关系;缺点是需要谨慎处理hibernate的session 关闭, exception, transaction. 2 使用spring的HibernateDaoSupport.这样的话dao层使用spring提供的一系列模板方法,同时不用关心session, exception,事务管理也交给了spring.第一种方式就不说了,基本无需改变dao层。只需要把dao注入就可以了,就学习一下第二种。首先需要注入一个sessionFactory.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:dao/hibernate/Hibernate.cfg.xml" /> </bean>
LocalSessionFactoryBean利用配置文件生成sessionFactory的代理实例,这样可以更好的和spring的事务协调工作。sessionFactory还有spring风格的配置, 它需要注入一个dataSource. 见下面的配置文件。
dao继承自HibernateDaoSupport:
package dao.hibernate; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import model.Company; public class CompanyDaoImpl extends HibernateDaoSupport implements CompanyDao{ public List list() { return getHibernateTemplate().find("from Company c"); } public Company getById(int id) { return (Company) getHibernateTemplate().get(Company.class, id); } public List findByName(String name) { return getHibernateTemplate().find("from Company where name like ?", name + "%"); } public void deleteById(int id) { Company c = (Company) getHibernateTemplate().load(Company.class, id); getHibernateTemplate().delete(c); } public void save(Company c) { getHibernateTemplate().save(c); } }

可以看出:所有hibernate的crud操作都封装在HibernateTemplate里面(HibernateDaoSupport持有一个HibernateTemplate),大大简化了hibernate的操作.
这样的话,dao持有一个hibernateTemplate, hibernateTemplate持有一个sessionFactory, 这样的话配置文件如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <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="mappingDirectoryLocations"> <list> <value>classpath:/model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.connection.autocommit">true</prop> <!--prop key=""></prop--> </props> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="companyDao" class="dao.hibernate.CompanyDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate" /> </bean>

测试代码:
public class Test { public static void main(String[] args) throws InterruptedException, SQLException{ ApplicationContext c = new ClassPathXmlApplicationContext("spring-test.xml"); CompanyDao dao = (CompanyDao)c.getBean("companyDao"); Company com = new Company("www.sss.com","SSS","shanghai", new Date()); dao.save(com);//success }}

你可能感兴趣的:(DAO,spring,Hibernate,exception,list,Class)