之前一直用spring jdbc和jdbc的,对hibenernate老是有点担心,我的结构如下,用的是struts2+spring+hibernate3public class CommonServiceImpl extends HibernateDaoSupport implements
CommonService { protected void delete(Object object) {
getHibernateTemplate().delete(object);
} .....这里调用gethibernateTemplate
} 然后在service层的接口,这样搞:
public interface IArticleService extends CommonService
{ }
而service层的接口实现则这样搞;
public class ArticleServiceImpl extends CommonServiceImpl implements IArticleService{ //这里注入了dao
private IArticleDao articleDao; } DAO层的接口如下:
public interface IArticleDao extends CommonService{ } DAO层的实现:
public class ArticleDaoImpl extends CommonServiceImpl implements IArticleDao { } 在配置文件方面,我这样搞:
在service-context.xml中:
<bean id="articleService" parent="txProxyTemplate">
<property name="target">
<bean class="ArticleServiceImpl"> <property name="articleDao">
<ref bean="articleDao" />
</property>
</bean>
</property> </bean> 在dao-context.xml中
<bean id="articleDao" parent="txProxyTemplate">
<property name="target">
<bean class="ArticleDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</property>
</bean> 最后,是applicationContext.xml
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean> <bean id="commonServiceImpl"
class="com.trustel.service.CommonServiceImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property> </bean> <bean id="commonService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="commonServiceImpl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean> <bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">
PROPAGATION_REQUIRED
</prop> </props>
</property>
</bean>
其中我配置了opensession view了:
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter> <filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>但有个问题是,我这样写:extends HibernateDaoSupport之后,在其中这样写:
return getSession().createQuery(hql).list();
我觉得这样SPRING应该不会自动关闭session吧?
CommonService { protected void delete(Object object) {
getHibernateTemplate().delete(object);
} .....这里调用gethibernateTemplate
} 然后在service层的接口,这样搞:
public interface IArticleService extends CommonService
{ }
而service层的接口实现则这样搞;
public class ArticleServiceImpl extends CommonServiceImpl implements IArticleService{ //这里注入了dao
private IArticleDao articleDao; } DAO层的接口如下:
public interface IArticleDao extends CommonService{ } DAO层的实现:
public class ArticleDaoImpl extends CommonServiceImpl implements IArticleDao { } 在配置文件方面,我这样搞:
在service-context.xml中:
<bean id="articleService" parent="txProxyTemplate">
<property name="target">
<bean class="ArticleServiceImpl"> <property name="articleDao">
<ref bean="articleDao" />
</property>
</bean>
</property> </bean> 在dao-context.xml中
<bean id="articleDao" parent="txProxyTemplate">
<property name="target">
<bean class="ArticleDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</property>
</bean> 最后,是applicationContext.xml
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean> <bean id="commonServiceImpl"
class="com.trustel.service.CommonServiceImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property> </bean> <bean id="commonService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="commonServiceImpl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean> <bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">
PROPAGATION_REQUIRED
</prop> </props>
</property>
</bean>
其中我配置了opensession view了:
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter> <filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>但有个问题是,我这样写:extends HibernateDaoSupport之后,在其中这样写:
return getSession().createQuery(hql).list();
我觉得这样SPRING应该不会自动关闭session吧?
而且在dao,service等中最好通过HibernateTemplate,jdbcemplate等操作数据
List all = this.getHibernateTemplate().executeFind( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { // TODO Auto-generated method stub // 直接编写查询方法 Query q = session.createQuery("FROM News WHERE title=? OR abstractnote=? OR content=?"); q.setFirstResult((cp - 1)* ls); q.setMaxResults(ls); q.setString(0, "%"+keyword+"%"); q.setString(1, "%"+keyword+"%"); q.setString(2, "%"+keyword+"%"); return q.list(); } }); return all; }
because it is final: All calls to this method via a proxy will be routed directly to the proxy.