简体   繁体   English

为每个Dao类创建BaseDAO

[英]Creating BaseDAO for Each Dao class

I created a spring application where i decided to add a BaseDAO to eliminate redundant create, update,delete,findByid,and findAll methods for every dao. 我创建了一个spring应用程序,我决定添加一个BaseDAO来消除每个dao的冗余创建,更新,删除,findByid和findAll方法。 So i created a baseDao and every dao should extend this BaseDAO. 所以我创建了一个baseDao,每个dao应该扩展这个BaseDAO。

BaseDaoImpl 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;
    }



}

Then i extends this Dao to each DAO 然后我将这个Dao扩展到每个DAO

EmployeeDAOImp EmployeeDAOImp

public class EmployeeDAOImpl extends BaseDAOImpl implements EmployeeDAO{

    private SessionFactory sessionFactory;

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

I created a BaseService like this. 我创建了这样的BaseService。 But when i try to access BaseDAO methods from EmployeeDAO it returns null pointer exception. 但是当我尝试从EmployeeDAO访问BaseDAO方法时,它返回空指针异常。 Why this happen. 为什么会这样。 I dont want to use genericDAO from google. 我不想使用谷歌的genericDAO。 Because we should create DAOs for each model. 因为我们应该为每个模型创建DAO。 I want to eliminate this. 我想消除这个。 So I follow this method. 所以我遵循这个方法。

Have you though about Spring Data project & Spring Data JPA in particular? 你有没有特别关于Spring Data项目和Spring Data JPA?

This would save you lots of time, since you would no longer need to write your DAO / Repositories from scratch, all you need to do is enable Spring Data JPA, and add needed interfaces. 这样可以节省大量时间,因为您不再需要从头开始编写DAO /存储库,只需启用Spring Data JPA并添加所需的接口即可。 It should save you tons of time. 它应该为您节省大量时间。

Your are overriding setSessionFactory from base class for no reason, its already available with extending class EmployeeDAOImpl , either remove it or try below: 你无缘无故地从基类覆盖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);
    }
}

Something like the following should work (note the use of constructor rather than setter injection). 像下面这样的东西应该工作(注意使用构造函数而不是setter注入)。 In the BaseDAO: 在BaseDAO中:

public class BaseDAOImpl implements BaseDAO {

  private final SessionFactory sessionFactory;

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

Then in the Employee DAO: 然后在Employee DAO中:

public class EmployeeDAOImpl extends BaseDAOImpl implements EmployeeDAO {

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

You can create generic dao. 你可以创建泛型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();
    }
  }

UPDATED 更新

You can use as following, TimeRange is a pojo class in the following example. 您可以使用以下示例,TimeRange是以下示例中的pojo类。 If you do not want a service layer. 如果您不想要服务层。 You can use timeRangeDao in controller. 您可以在控制器中使用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);
    }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM