Spring DAO可以合并到Service层吗? [英] Can Spring DAO be merged into Service layer?

查看:312
本文介绍了Spring DAO可以合并到Service层吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用spring框架和mybatis开发一个Web应用程序.

I'm developing a web application with spring framework and mybatis.

在大多数情况下(至少对我而言),DAO的方法非常简短,如下所示:

In most cases(at least for me), DAO's methods are very short something like this:

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
  public User getUser(String userId) {
    return (User) getSqlSession().selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
  }
}

所以基本上,我需要在DAO中为每个查询编写一个方法(例如getUser(String userId)),该方法将被转发到使用它的服务对象.对我来说似乎不必要地多余了.

So basically, I need to write a method(e.g. getUser(String userId)) in DAO for each query which is being forwarded to service objects where it is being used. It seems unnecessarally redundunt to me.

我的同事正在尝试使其变得简单.他这样写CommonDao:

My co-worker is trying to make it simple. He wrote CommonDao like this:

@Repository
public class CommonDao {
    @Autowired
    private SqlSessionTemplate sqlSession;

    public Object insert(String queryId, Object params) {
        return sqlSession.insert(queryId, params);
    }

    public Object update(String queryId, Object params) {
        return sqlSession.update(queryId, params);
    }

    public Object delete(String queryId, Object params) {
        return sqlSession.delete(queryId, params);
    }

    public Object selectOne(String queryId) {
        return sqlSession.selectOne(queryId);
    }

    public Object selectOne(String queryId, Object params) {
        return sqlSession.selectOne(queryId, params);
    }
}

因此我们可以在以下服务中使用这些方法:

So we can use these methods in services like:

@Service
public class CrudService {
    ...
    @Autowired
    private CommonDao commonDao;
    ...

    public UserDto selectUser(Integer userId) {
        ...
        UserDto userDto = (UserDto) commonDao.selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
        ...
    }
}

我有点喜欢这种方法,因为它使代码更简单.但是我不确定这是否是一个很好的准则.

I'm kinda like this approch since it makes codes simpler. But I'm not sure it is a good prectice to follow.

推荐答案

为避免样板代码,同时具有类型安全性,并使服务层不受DAO实施细节的影响,请使用

To avoid a boilerplate code and at the same time have type safety and leave your service layer free from DAO implementation details use spring-mybatis MapperScannerConfigurer.

在这种情况下,您可以使用类型安全的映射器替换DAO.

In this case you can replace your DAOs with type-safe mappers.

相当于您的DAO

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
  public User getUser(String userId) {
    return (User)getSqlSession().selectOne(
        "org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
  }
}

将是此映射器类

package org.mybatis.spring.sample.mapper;

interface UserMapper {
  User getUser(String userId);
}

如果将其重命名为UserDao,则完全不需要更改服务.服务仅取决于声明的映射器接口.

If you rename it to UserDao you will not need to change your services at all. Service only depends on the declared mapper interface.

请注意,您需要定义此接口以便具有类型安全性,并且还定义服务的依赖关系.

Note that you need to define this interface in order to have type safety and also define the dependency of you service.

当然,您需要配置spring-mybatis,以便它基于代码中定义的映射器接口生成映射器实现.这非常简单,并且有许多选项如何做到这一点.

Of course you need configure spring-mybatis so that it generates mapper implementation based on the mapper interfaces defined in your code. This is rather straightforward and there are many options how to do that.

这篇关于Spring DAO可以合并到Service层吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆