休眠:CRUD通用DAO [英] Hibernate: CRUD Generic DAO

查看:120
本文介绍了休眠:CRUD通用DAO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的web应用程序有很多服务表/实体,例如 payment_methods tax_codes province_codes 等等。

每次添加一个新实体时,我都必须编写一个DAO。事实是,基本上它们都是一样的,但唯一的区别是实体类本身

我知道Hibernate工具可以自动为我生成代码,但现在我不能使用它们(不要问为什么),所以我正在考虑通用DAO 。有很多关于这方面的文献,但我不能把它们放在一起,并使它与Spring一起工作。



我认为所有关于泛型的东西都会有四种基本方法:


  • listAll

  • saveOrUpdate

  • deleteById

  • getById



就是这样。




问题:



什么是最佳做法不重新发明轮子?

解决方案

这里是我的

  @Component 
public class Dao {

@Resource(name =sessionFactory)
private SessionFactory sessionFactory;

public< T> T save(final T o){
return(T)sessionFactory.getCurrentSession()。save(o);
}


public void delete(final Object object){
sessionFactory.getCurrentSession()。delete(object);
}

/ *** /
public< T> T get(final Class< T> type,final Long id){
return(T)sessionFactory.getCurrentSession()。get(type,id);
}

/ *** /
public< T> T合并(final T o){
return(T)sessionFactory.getCurrentSession()。merge(o);
}

/ *** /
public< T> void saveOrUpdate(final T o){
sessionFactory.getCurrentSession()。saveOrUpdate(o);
}

public< T>列表与LT; T> getAll(final Class< T>类型){
final Session session = sessionFactory.getCurrentSession();
final Criteria crit = session.createCriteria(type);
返回crit.list();
}
//等等,你应该知道

和然后你可以在服务层访问:

  @Autowired 
private Dao dao;

@Transactional(readOnly = true)
public List< MyEntity> getAll(){
return dao.getAll(MyEntity.class);
}


My web application has got a lot of service tables/entities, such as payment_methods, tax_codes, province_codes, etc.

Each time I add a new entity, I have to write a DAO. The thing is that, basically, they are all the same, but the only difference is the entity class itself.

I know that Hibernate tools can generate the code for me automatically but I can't use them now (don't ask why) so I'm thinking of a Generic DAO. There's a lot of literature about that but I can't put pieces together and make it work with Spring.

It's all about generics I think, it will have four basic methods:

  • listAll
  • saveOrUpdate
  • deleteById
  • getById

and that's all.


Question:

What's the best practice for not re-inventing the wheel? Isn't there something ready to use, yet?

解决方案

here's mine

@Component
public class Dao{

    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    public <T> T save(final T o){
      return (T) sessionFactory.getCurrentSession().save(o);
    }


    public void delete(final Object object){
      sessionFactory.getCurrentSession().delete(object);
    }

    /***/
    public <T> T get(final Class<T> type, final Long id){
      return (T) sessionFactory.getCurrentSession().get(type, id);
    }

    /***/
    public <T> T merge(final T o)   {
      return (T) sessionFactory.getCurrentSession().merge(o);
    }

    /***/
    public <T> void saveOrUpdate(final T o){
      sessionFactory.getCurrentSession().saveOrUpdate(o);
    }

    public <T> List<T> getAll(final Class<T> type) {
      final Session session = sessionFactory.getCurrentSession();
      final Criteria crit = session.createCriteria(type);
  return crit.list();
    }
// and so on, you shoudl get the idea

and you can then access like so in service layer:

 @Autowired
    private Dao dao;

   @Transactional(readOnly = true)
    public List<MyEntity> getAll() {
      return dao.getAll(MyEntity.class);
    }

这篇关于休眠:CRUD通用DAO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆