在《自己动手写代码,整合Spring和Hibernate》系列博文的前两篇中,我们需要为每个DAO类定义一个SessionFactory,需要在DAO的每个方法中获取Session、关闭Session。我们可以写一个公用的DAO类,然后让其他DAO类继承它,实现代码复用。
公用DAO类(抽象类):
package com.zzj.dao; import java.io.Serializable; import org.hibernate.Session; import org.hibernate.SessionFactory; public abstract class DAOSupport { private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } /** * 保存 * @param t * @return */ protected Serializable save(Object t){ Session session = sessionFactory.openSession(); session.beginTransaction(); Serializable id = session.save(t); session.getTransaction().commit(); session.close(); return id; } protected Object findByID(Class<?> clazz, Serializable id){ Session session = sessionFactory.openSession(); Object t = session.get(clazz, id); session.close(); return t; } }CustomerDAO类:
package com.zzj.dao; import com.zzj.entity.Customer; public class CustomerDAO extends DAOSupport{ /** * 查询 * @param ID * @return */ public Customer findByID(String ID){ return (Customer) findByID(Customer.class, ID); } /** * 保存 * @param customer * @return */ public String save(Customer customer){ return (String) super.save(customer); } }
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置数据源 --> <!-- <bean name="dataSource" class="com.zzj.base.MyDataSource"> <property name="url"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> <property name="username"> <value>system</value> </property> <property name="password"> <value>1223</value> </property> <property name="driveClass"> <value>oracle.jdbc.driver.OracleDriver</value> </property> </bean> --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="JdbcUrl"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> <property name="User"> <value>system</value> </property> <property name="Password"> <value>1223</value> </property> <property name="DriverClass"> <value>oracle.jdbc.driver.OracleDriver</value> </property> </bean> <!-- 配置sessionFactory对象 --> <bean name="sessionFactory" class="com.zzj.base.MySessionFactory"> <property name="dataSource" ref="dataSource"/> <property name="dialect"> <value>org.hibernate.dialect.Oracle9Dialect</value> </property> <property name="mappings"> <list> <value>com/zzj/entity/Customer.hbm.xml</value> </list> </property> </bean> <!-- 配置DAO --> <bean id="customerDAO" class="com.zzj.dao.CustomerDAO"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
以上是使用继承达到了代码的复用,下面使用组合模式达到相同的目的。
公用DAO类(为了区别继承,另外命名,非抽象类):
package com.zzj.dao; import java.io.Serializable; import org.hibernate.Session; import org.hibernate.SessionFactory; public class DAOTemplate { private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } /** * 保存 * @param t * @return */ protected Serializable save(Object t){ Session session = sessionFactory.openSession(); session.beginTransaction(); Serializable id = session.save(t); session.getTransaction().commit(); session.close(); return id; } protected Object findByID(Class<?> clazz, Serializable id){ Session session = sessionFactory.openSession(); Object t = session.get(clazz, id); session.close(); return t; } }CustomerDAO类:
package com.zzj.dao; import com.zzj.entity.Customer; public class CustomerDAO{ private DAOTemplate daoTemplate; public void setDaoTemplate(DAOTemplate daoTemplate) { this.daoTemplate = daoTemplate; } /** * 查询 * @param ID * @return */ public Customer findByID(String ID){ return (Customer) daoTemplate.findByID(Customer.class, ID); } /** * 保存 * @param customer * @return */ public String save(Customer customer){ return (String) daoTemplate.save(customer); } }Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置数据源 --> <!-- <bean name="dataSource" class="com.zzj.base.MyDataSource"> <property name="url"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> <property name="username"> <value>system</value> </property> <property name="password"> <value>1223</value> </property> <property name="driveClass"> <value>oracle.jdbc.driver.OracleDriver</value> </property> </bean> --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="JdbcUrl"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> <property name="User"> <value>system</value> </property> <property name="Password"> <value>1223</value> </property> <property name="DriverClass"> <value>oracle.jdbc.driver.OracleDriver</value> </property> </bean> <!-- 配置sessionFactory对象 --> <bean name="sessionFactory" class="com.zzj.base.MySessionFactory"> <property name="dataSource" ref="dataSource"/> <property name="dialect"> <value>org.hibernate.dialect.Oracle9Dialect</value> </property> <property name="mappings"> <list> <value>com/zzj/entity/Customer.hbm.xml</value> </list> </property> </bean> <!-- 配置DAOTemplate --> <bean name="daoTemplate" class="com.zzj.dao.DAOTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置DAO --> <bean id="customerDAO" class="com.zzj.dao.CustomerDAO"> <property name="daoTemplate" ref="daoTemplate"></property> </bean> </beans>上面两种方法中,推荐使用组合。 当然了,具体使用哪种方式,就看个人喜好了。
注:每一个具体的DAO类,最终都需要注入一个sessionFactory。