这一节将对Dao Interface Bundle 和 Dao implementation Bundle进行开发。
package net.georgezeng.test.dao; import java.util.List; public interface BaseDao<T> { void save(T t); T get(Long id); List<T> findList(T t); }
package net.georgezeng.test.dao; import net.georgezeng.test.domain.Contact; public interface ContactDao extends BaseDao<Contact> { }
package net.georgezeng.test.dao.hibernate; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.List; import net.georgezeng.test.dao.BaseDao; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public abstract class HibernateBaseDao<T> extends HibernateDaoSupport implements BaseDao<T> { protected Class<T> entityClass; @Autowired public void init(SessionFactory sessionFactory) { super.setSessionFactory(sessionFactory); } public HibernateBaseDao() { entityClass = getEntityClass(); } @Override public void save(T t) { getHibernateTemplate().save(t); } @Override public T get(Long id) { return getHibernateTemplate().get(entityClass, id); } @SuppressWarnings("unchecked") @Override public List<T> findList(T t) { return getHibernateTemplate().findByExample(t); } @SuppressWarnings("unchecked") protected Class<T> getEntityClass() { Type type = getClass().getGenericSuperclass(); Class<T> result = null; if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; result = (Class<T>) pType.getActualTypeArguments()[0]; } return result; } }
package net.georgezeng.test.contact.dao; import net.georgezeng.test.dao.ContactDao; import net.georgezeng.test.dao.hibernate.HibernateBaseDao; import net.georgezeng.test.domain.Contact; import org.springframework.stereotype.Repository; @Repository("ContactDao") public class ContactDaoImpl extends HibernateBaseDao<Contact> implements ContactDao { }
<?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 http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd" xmlns:osgi="http://www.springframework.org/schema/osgi"> <osgi:reference id="dataSource" interface="javax.sql.DataSource" /> <osgi:service ref="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager" context-class-loader="service-provider" /> <osgi:service ref="ContactDao" interface="net.georgezeng.test.dao.ContactDao" /> </beans>
<osgi:reference id="dataSource" interface="javax.sql.DataSource" />
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <context:component-scan base-package="net.georgezeng.test" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="schemaUpdate" value="false"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> </props> </property> <property name="annotatedClasses"> <list> <value>net.georgezeng.test.domain.Contact</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>
<property name="dataSource"> <ref bean="dataSource" /> </property>
<osgi:service ref="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager" context-class-loader="service-provider" />