服务层应该了解存储过程和参数吗?
我们在Hibernate3.6-Spring 3.1中有一个应用程序。
我有像这样的通用 DAO 实现,( http://www .ibm.com/developerworks/java/library/j-genericdao/index.html)
public abstract class GenericDaoImpl<T, ID extends Serializable> implements GenericDao<T, ID>
{
public int executeSP(final String SP_NAME, Map<String, Object> params)
{
SQLQuery sq = getSession().createSQLQuery(SP_NAME);
sq.setProperties(params);
return sq.executeUpdate();
}
}
这个服务实现
@Transactional
public class PejlAnalysisServiceImpl implements PejlAnalysisService, InitializingBean
{
private CisternDao cisternDao;
private PejlDataDao pejlDataDao;
private GenericDao genericDao; // <-- THIS?
private void test()
{
Map<String, Object> params = new HashMap<String, Object>();
params.put("PARAM1",100); //AND THIS
genericDao.executeSP("MY_STORED_PROCEDURE", params); //And THIS
}
}
这真的是正确的方法吗?
PS存储过程处理不同的数据库表,因此它们不属于特定的DAO impl。
We have an application in Hibernate3.6-Spring 3.1.
I have my generic DAO impl like this,( http://www.ibm.com/developerworks/java/library/j-genericdao/index.html)
public abstract class GenericDaoImpl<T, ID extends Serializable> implements GenericDao<T, ID>
{
public int executeSP(final String SP_NAME, Map<String, Object> params)
{
SQLQuery sq = getSession().createSQLQuery(SP_NAME);
sq.setProperties(params);
return sq.executeUpdate();
}
}
And this Service Implementation
@Transactional
public class PejlAnalysisServiceImpl implements PejlAnalysisService, InitializingBean
{
private CisternDao cisternDao;
private PejlDataDao pejlDataDao;
private GenericDao genericDao; // <-- THIS?
private void test()
{
Map<String, Object> params = new HashMap<String, Object>();
params.put("PARAM1",100); //AND THIS
genericDao.executeSP("MY_STORED_PROCEDURE", params); //And THIS
}
}
Is this really the right way to do it?
P.S. the stored procedure deal with different database tables so they do not belong to a specific DAO impl.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上取决于你的设计。在我编写的一个较小的项目中,我有一个其他组件使用的 StoredProcedure 对象,因为单个存储过程是存储数据的唯一方法。我建议首先避免使用存储过程,但如果您坚持使用它们,则必须确定它们与您的设计的整合程度。它们是业务逻辑的重要部分,还是只是持久层的实现细节?
It really depends on your design. In one smaller project I wrote, I had a StoredProcedure object that the other components used because a single stored procedure was the one and only way of storing data. I'd recommend avoiding stored procedures in the first place, but if you're stuck with them, you have to decide how integral they are to your design. Are they an essential piece of business logic, or just an implementation detail of your persistence layer?