将来如何为SpringMVC3设计服务层以实现Hibernate
我们现在正在使用SpringJDBCTemplate来实现服务层,但在将来的某个时候,我们希望转到hibernate。我关心的是,当我们移动到Hibernate时,当前服务层的设计方式是否有效 目前我们有:为了清楚起见,我省略了细节 上下文xml中的数据源将来如何为SpringMVC3设计服务层以实现Hibernate,hibernate,spring-mvc,jdbc,jdbctemplate,Hibernate,Spring Mvc,Jdbc,Jdbctemplate,我们现在正在使用SpringJDBCTemplate来实现服务层,但在将来的某个时候,我们希望转到hibernate。我关心的是,当我们移动到Hibernate时,当前服务层的设计方式是否有效 目前我们有:为了清楚起见,我省略了细节 上下文xml中的数据源 <bean id="dataSource" destroy-method="close" class="...BasicDataSource"> <property name="" value="" />
<bean id="dataSource" destroy-method="close" class="...BasicDataSource">
<property name="" value="" />
<property name="" value="" />
<property name="username" value="" />
<property name="password" value=""/>
</bean>
控制器:
@Autowired
private MyService myService;
我在SpringMVC中看到了很多hibernate服务层的实现,其中大多数都使用了
接口
。添加接口的好处是什么?我是否应该在我的设计中添加一个接口
?在过渡到hibernate时,这会使事情变得更容易吗?我唯一的建议是使用接口和贫血域模型。通过这种方式,您可以轻松地使用spring切换接口实现
这就是使用接口的好处
假设您有以下打包和类:
org.company.samples.dao.Dao // Generic Dao Interface
org.company.samples.dao.DomainEntityDao // Domain Dao extends Generi Dao
org.company.samples.dao.jdbc.BaseDao // Generic implementation of Dao Interface
org.company.samples.dao.jdbc.DomainEntityDaoImpl // Domain Dao implementation extends BaseDao
然后你想切换到hibernate,这样你就可以完成这个结构
org.company.samples.dao.Dao // Generic Dao Interface
org.company.samples.dao.DomainEntityDao // Domain Dao extends Generi Dao
org.company.samples.dao.hibernate.BaseDao // Generic Hibernate implementation of Dao Interface
org.company.samples.dao.hibernate.DomainEntityDaoImpl // Domain Dao implementation extends BaseDao
如果在DAO中使用注释,那么在spring中从jdbc切换到hibernate非常简单:
与此相反:
<context:component-scan base-package="org.company.samples.dao.jdbc" />
您可以为此更改它:
<context:component-scan base-package="org.company.samples.dao.hibernate" />
就这样,您可以在不更改服务类型的情况下切换daos实现。太好了,谢谢。所以我的注释应该在接口或实现中?实现中,请阅读spring如何使用代理来理解它。我记不清为什么了。
<context:component-scan base-package="org.company.samples.dao.hibernate" />