为每个Dao类创建BaseDAO

时间:2014-12-02 05:59:40

标签: java spring spring-mvc

我创建了一个spring应用程序,我决定添加一个BaseDAO来消除冗余创建, 每个dao的update,delete,findByid和findAll方法。所以我创建了一个baseDao,每个dao应该扩展这个BaseDAO。

BaseDaoImpl

public class BaseDAOImpl implements BaseDAO{

    SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }

    @Override
    public void create(ModelBase modelBase) {

         Session session = this.sessionFactory.getCurrentSession();
         session.persist(modelBase);
    }

    @Override
    public void update(ModelBase modelBase) {

        Session session = this.sessionFactory.getCurrentSession();
         session.update(modelBase);
    }

    @Override
    public Collection findAll(Class aClass) {

        Session session = this.sessionFactory.getCurrentSession();
        Collection  modelCols = session.createQuery("from "+aClass.getSimpleName()).list();
        return modelCols;
    }

    @Override
    public ModelBase findById(Class aClass, Integer id) {
        Session session = this.sessionFactory.getCurrentSession();     
        ModelBase modelBase = (ModelBase) session.load(aClass, new Integer(id));
        return modelBase;
    }



}

然后我将这个Dao扩展到每个DAO

EmployeeDAOImp

public class EmployeeDAOImpl extends BaseDAOImpl implements EmployeeDAO{

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }
}

我创建了这样的BaseService。但是当我尝试从EmployeeDAO访问BaseDAO方法时,它返回空指针异常。 为什么会这样。我不想使用谷歌的genericDAO。因为我们应该创建DAO 对于每个型号。我想消除这个。所以我遵循这个方法。

4 个答案:

答案 0 :(得分:1)

你有没有关于Spring Data项目&特别是Spring Data JPA?

这将为您节省大量时间,因为您不再需要从头开始编写DAO /存储库,您只需启用Spring Data JPA并添加所需的接口。它应该为您节省大量时间。

答案 1 :(得分:0)

您无缘无故地从基类覆盖setSessionFactory,它已经可用于扩展类EmployeeDAOImpl,要么将其删除,要么尝试以下内容:

public class EmployeeDAOImpl extends BaseDAOImpl implements EmployeeDAO{

   //this reference should be from base class,
   // the extending class ref is hiding base ref.
  //  private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sf){
      super.setSessionFactory(sf);
    }
}

答案 2 :(得分:0)

以下内容应该有效(注意使用构造函数而不是setter注入)。在BaseDAO:

public class BaseDAOImpl implements BaseDAO {

  private final SessionFactory sessionFactory;

  public BaseDAOImpl(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }
}

然后在员工DAO中:

public class EmployeeDAOImpl extends BaseDAOImpl implements EmployeeDAO {

  @Inject
  public EmployeeDAOImpl (SessionFactory sessionFactory) {
    super(sessionFactory);
  }
}

答案 3 :(得分:0)

您可以创建通用dao。

@Repository("genericDao")
public class GenericDaoImpl<T,PK extends Serializable> implements GenericDao<T, PK> {

    protected Class<T> entityClass;

    public T create(T t) {
       this.entityManager.persist(t);
       return t;
    }

    public T read(PK id,Class<T> c) {
       return (T)this.entityManager.find(c, id);
    }

    public T update(T t) {
       return this.entityManager.merge(t);
    }

    public void delete(T t) {
        t = this.entityManager.merge(t);
       this.entityManager.remove(t);
    }

    public List<T> getAll(Class<T> c){
        return this.entityManager.createQuery("SELECT o FROM "+ c.getName() +" o").getResultList();
    }
  }

已更新

您可以使用以下示例,TimeRange是以下示例中的pojo类。如果您不想要服务层。您可以在控制器中使用timeRangeDao。

@Service("timeRangeService")
public class TimeRangeServiceImpl implements TimeRangeService{
    @Autowired
    GenericDao<TimeRange,Long> timeRangeDao;

    public List<TimeRange> getAllTimeRanges(){
            return timeRangeDao.getAll(TimeRange.class);
    }

    @Transactional
    public void createTimeRange(TimeRange c) {
            timeRangeDao.create(c);
    }

    @Transactional
    public void update(TimeRange p) {
            timeRangeDao.update(p);
    }

    @Transactional
    public TimeRange getTimeRange(long id) {
            return timeRangeDao.read(id, TimeRange.class);
    }

    @Transactional
    public void delete(long id) {
            TimeRange timeRange = new TimeRange();
            timeRange.setId(id);
            timeRangeDao.delete(timeRange);
    }

 }
相关问题