【发布时间】:2016-08-17 03:11:57
【问题描述】:
我是 Spring 新手,正在尝试像本文 http://www.ibm.com/developerworks/library/j-genericdao/ 中那样实现 Generic DAO。我有几个实体 - ConcreteEntity1 和 ConcreteEntity2。还有,我有课
public interface GenericDao<T extends Serializable> {
public T get(long id);
public List<T> get(String hql);
public void remove(T persistentObject);
public void add(T entity);
}
和
@Repository("hibGenericDao")
public class HibGenericDaoImpl<T extends Serializable> implements GenericDao<T> {
@Autowired
private SessionFactory sessionFactory;
private Class<T> type;
public HibGenericDaoImpl(Class<T> type) {
this.type = type;
}
/** {@inheritDoc} */
@Override
public T get(long id) {
T entity;
try (Session session = sessionFactory.getCurrentSession()) {
entity = session.get(type, id);
}
return entity;
}
/** {@inheritDoc} */
@Override
public List<T> get(String hql) {
List<T> entityList;
try (Session session = sessionFactory.getCurrentSession()) {
Query query = session.createQuery(hql);
entityList = query.list();
}
return entityList;
}
/** {@inheritDoc} */
@Override
public void remove(T persistentObject) {
try (Session session = sessionFactory.getCurrentSession()) {
session.delete(persistentObject);
}
}
/** {@inheritDoc} */
@Override
public void add(T entity) {
try (Session session = sessionFactory.getCurrentSession()) {
session.saveOrUpdate(entity);
}
}
}
现在我正在尝试编写服务层,我想自动连接HibGenericDaoImpl<ConcreteEntity1>,其中字段type 包含ConcreteEntity1.class。你能说一下没有XML如何执行吗?
【问题讨论】:
标签: java spring dao spring-4 genericdao