[英]Creating BaseDAO for Each Dao class
我創建了一個spring應用程序,我決定添加一個BaseDAO來消除每個dao的冗余創建,更新,刪除,findByid和findAll方法。 所以我創建了一個baseDao,每個dao應該擴展這個BaseDAO。
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
public class EmployeeDAOImpl extends BaseDAOImpl implements EmployeeDAO{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
}
我創建了這樣的BaseService。 但是當我嘗試從EmployeeDAO訪問BaseDAO方法時,它返回空指針異常。 為什么會這樣。 我不想使用谷歌的genericDAO。 因為我們應該為每個模型創建DAO。 我想消除這個。 所以我遵循這個方法。
你有沒有特別關於Spring Data項目和Spring Data JPA?
這樣可以節省大量時間,因為您不再需要從頭開始編寫DAO /存儲庫,只需啟用Spring Data JPA並添加所需的接口即可。 它應該為您節省大量時間。
你無緣無故地從基類覆蓋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);
}
}
像下面這樣的東西應該工作(注意使用構造函數而不是setter注入)。 在BaseDAO中:
public class BaseDAOImpl implements BaseDAO {
private final SessionFactory sessionFactory;
public BaseDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
然后在Employee DAO中:
public class EmployeeDAOImpl extends BaseDAOImpl implements EmployeeDAO {
@Inject
public EmployeeDAOImpl (SessionFactory sessionFactory) {
super(sessionFactory);
}
}
你可以創建泛型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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.