Spring MVC: Generic DAO and Service classes

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 21:13:14

i still dont know why people actually use archaic DAO / service - models with Spring Data; totally unnecessary, error-prone and whatnot.

Spring Data JPA has some extremely useful interfaces for that stuff : JpaRepository and JpaSpecificationExecutor - these encapsulate EVERYTHING you would want, you only need your standard Entities and thats it - everything else will be handled by spring, you simply input your criterias and get exactly what you want, without reinventing the wheel. Could it be that you didnt actually read the documentation? Its very useful :

official introduction : http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

doc : http://docs.spring.io/spring-data/jpa/docs/1.7.0.RELEASE/reference/html/

small howto : http://www.cubrid.org/wiki_ngrinder/entry/how-to-create-dynamic-queries-in-springdata

examples from the genius himself : https://github.com/spring-projects/spring-data-jpa-examples/tree/master/spring-data-jpa-example

example classes :

public CustomerSpecifications {

  public static Specification<Customer> customerHasBirthday() {
    return new Specification<Customer> {
      public Predicate toPredicate(Root<T> root, CriteriaQuery query, CriteriaBuilder cb) {
        return cb.equal(root.get(Customer_.birthday), today);
      }
    };
  }

  public static Specification<Customer> isLongTermCustomer() {
    return new Specification<Customer> {
      public Predicate toPredicate(Root<T> root, CriteriaQuery query, CriteriaBuilder cb) {
        return cb.lessThan(root.get(Customer_.createdAt), new LocalDate.minusYears(2));
      }
    };
  }
}

public interface CustomerRepository extends JpaRepository<Customer>, JpaSpecificationExecutor {
  // Your query methods here
}

and now you can simply Autowire your repository :

@Autowired
CustomerRepository customerRepo;

and retrieve data like this :

List<Customer> customersWithBirthDay = customerRepo.findAll(CustomerSpecifications.customerHasBirthDay());

its that easy.

The problem is that your inject directly your GenericDao in your GenericManager but none of them is a concrete Spring bean and you will never be able to use your specific CountryDao.

You must not autowire GenericDao but only define it and provide setter :

// Add DAO as a genric parameter
public abstract class GenericManagerImpl<T, D extends GenericDao<T>> implements GenericManager<T> {
    private D dao;

    protected void setDao (D dao) {
        this.dao = dao;
    }

...

}

Then, you will have to inject a concrete spring bean in your concrete services. i.e. in CountryManagerImpl:

// Instantiate your concrete service with your concrete DAO
public class CountryManagerImpl extends GenericManagerImpl<Country, CountryDao> implements CountryManager {

    // Do not redeclare your dao here in order to keep the inherited one

    // Don't forget to inject
    @Inject("countryDao")
    @Override
    protected void setDao (CountryDao dao) {
        this.dao = dao;
    }

...

}

You will have then a full spring bean injected with your concrete CountryDao type and its specific methods.

You can take a look at what we did on RESThub project regarding generic services : https://github.com/resthub/resthub-spring-stack/blob/master/resthub-common/src/main/java/org/resthub/common/service/CrudServiceImpl.java and some concrete example : https://github.com/resthub/todo-backbone-example/blob/master/src/main/java/todo/TodoController.java (with a Controller instead of a Service but it is similar)

Hope it will help.

(and sorry if there is some typos, I cannot double check right now)

and, BTW, you should consider using Spring Data instead of using GenericDaos but you will still have the same needs regarding your Services.

I think it's just the limitation of the java OO design. You need a parametrized way to pass the predicates for searching, something like:

List<T> findByPredicate(List<Predicate> predicates, Class<T> returnType);

Where the predicate class is something like this

class Predicate {
   String columnName;
   Operator operator;
   String value;
}

Hence you can express "name = 'John'", age >= 21, etc

This is not an ideal solution, code become less human readable, you will need to transform the predicates into database queries and there are few type casting needs to be done which is prone to runtime error.

You can avoid reinventing the wheel with library like Spring Data. You don't even need a generic DAO, you just need to supply an interface method like

List<Person> findByName(String name);

and an implementation will be automatically generated at application bootstrap. Have a look at Spring Data JPA for more.

Divyang Upadhyay

//Implementing GenericDao and GenericService

// StateDaO

public interface StateDao extends GenericDao<State> {

}

// StateDaoImpl

@Repository("stateDao")

public class StateDaoImpl extends GenericDaoImpl<State> implements StateDao {

    @Autowired
    SessionFactory sessionFactory;
// another specific businness operation perform

}

// StateService

public interface StateService extends  GenericService<State> {


}

// StateServiceImpl

@Repository("stateService")

public class StateServiceImpl extends GenericServiceImpl<State, StateDao> implements StateService { 

   @Resource
   StateDao stateDao;

//using stateDao object of another specific operation
}

Try This:

public interface GenericDao<T> {

    public List<T> loadAll() throws Exception;
    public Long saveOrUpdate(T domain) throws Exception;
    public void saveOrUpdate(List domainList) throws Exception;
    public void delete(T domain) throws Exception;
    public T get(Serializable id) throws Exception;
    public List<T> getListByCriteria(DetachedCriteria detachedCriteria);
    public List<T> getListByCriteria(DetachedCriteria detachedCriteria,
                                     int offset, int size);
    public List<T> filterListWithCondition(T domain) throws Exception;

}

public class GenericDaoImpl<T> extends HibernateDaoSupport implements GenericDao<T> {

        @Autowired
        SessionFactory sessionFactory;

        private Class<T> entityClass;
        private MySQLIntegrityConstraintViolationException sqlException = new MySQLIntegrityConstraintViolationException("Duplicate Record inserted");

        @Autowired
        public void setSession(SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);
        }

        public GenericDaoImpl() {
            entityClass = (Class<T>) ((ParameterizedType) getClass()
                          .getGenericSuperclass()).getActualTypeArguments()[0];
        }

        public List<T> loadAll() throws Exception{
            Session session = getHibernateTemplate().getSessionFactory().openSession();
            List<T> list = session.createQuery("from "+entityClass.getName()).list();
            session.close();
            return list;
        }

        public void delete(T domain) throws Exception {

                Session session = sessionFactory.openSession();
                Transaction tx = session.beginTransaction();

                session.delete(domain);
                tx.commit();
                session.close();

        }

        public Long saveOrUpdate(T domain) throws Exception {

            try {
                Session session = sessionFactory.openSession();
                Transaction tx = session.beginTransaction();

                session.saveOrUpdate(domain);
                tx.commit();
                Serializable ids = session.getIdentifier(domain);
                session.close();
                return (Long)ids;

            } catch (ConstraintViolationException  e) {
                throw new ConstraintViolationException("Duplicate Record inserted", sqlException, "");
            } 

        }

        public void saveOrUpdate(List domainList) throws Exception {
            try {
                Session session = sessionFactory.openSession();
                Transaction tx = session.beginTransaction();

                Object dom  = null;

                for(int i =0; i<domainList.size(); i++) {

                    dom = domainList.get(i);
                    session.saveOrUpdate(dom);

                     if ( i % 10 == 0 ) { 
                          //10, same as the JDBC batch size
                          //flush a batch of inserts and release memory:
                         session.flush();
                         session.clear();
                     }

                }

                tx.commit();
                session.close();

            } catch (ConstraintViolationException  e) {
                throw new ConstraintViolationException("Duplicate Record inserted", sqlException, "");
            } 

        }

        public T get(Serializable id) throws Exception{

                Session session = getHibernateTemplate().getSessionFactory().openSession();
                T o = (T) session.get(entityClass, id);
                return (T)o;

        }

        public List<T> getListByCriteria(DetachedCriteria detachedCriteria,
                                         int offset, int size) {
            return (List<T>) getHibernateTemplate().findByCriteria(detachedCriteria, offset, size);
        }

        public List<T> getListByCriteria(DetachedCriteria detachedCriteria) {
            return (List<T>) getHibernateTemplate().findByCriteria(detachedCriteria);
        }

        public List<T> filterListWithCondition(T domain) throws Exception {
            return (List<T>) getHibernateTemplate().findByExample(domain);
        }

}

public interface GenericService<T> {

    public List<T> loadAll() throws Exception;
    public Long saveOrUpdate(T domain) throws Exception;
    public void saveOrUpdate(List domainList) throws Exception;
    public void delete(T domain) throws Exception;
    public T get(Serializable id) throws Exception;
    public List<T> getListByCriteria(DetachedCriteria detachedCriteria);
    public List<T> getListByCriteria(DetachedCriteria detachedCriteria, int offset, int size);
    public List<T> filterListWithCondition(T domain) throws Exception;

}

public class GenericServiceImpl<T, T2 extends GenericDao<T>> implements GenericService<T> {

    @Autowired
    private T2 genericDao;

    @Override
    public List<T> loadAll() throws Exception {
        return genericDao.loadAll();
    }

    @Override
    public Long saveOrUpdate(T domain) throws Exception{
        return genericDao.saveOrUpdate(domain);
    }

    @Override
    public void delete(T domain) throws Exception {
        genericDao.delete(domain);
    }

    @Override
    public T get(Serializable id) throws Exception {
        return genericDao.get(id);
    }

    @Override
    public List<T> getListByCriteria(DetachedCriteria detachedCriteria) {
        return genericDao.getListByCriteria(detachedCriteria);
    }

    @Override
    public List<T> getListByCriteria(DetachedCriteria detachedCriteria,
            int offset, int size) {
        return genericDao.getListByCriteria(detachedCriteria, offset, size);
    }

    @Override
    public List<T> filterListWithCondition(T domain) throws Exception {
        return genericDao.filterListWithCondition(domain);
    }

    @Override
    public void saveOrUpdate(List domainList) throws Exception {
        genericDao.saveOrUpdate(domainList);
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!