日期:2014-05-20  浏览次数:20783 次

spring+hibernate小弟封装了一个DAO,感觉不好用,有没有好点的封装
这个是我写的DAO
Java code

package com.lulu.ssh.dao;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

@Component("dao")
public class DAO<T> {
    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    @Resource
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public T get(Class clazz, int id) {
        return (T) hibernateTemplate.get(clazz, id);
    }

    public void save(T t) {
        hibernateTemplate.save(t);
    }

    public void delete(T t) {
        hibernateTemplate.delete(t);
    }

    public void update(T t) {
        hibernateTemplate.update(t);
    }

    public List<T> search(final Class<T> clazz, final int start, final int limit, final AddRestrictions addRestrictions) throws Exception {
        return hibernateTemplate.executeFind(new HibernateCallback() {

            @Override
            public Object doInHibernate(Session s) throws HibernateException,
                    SQLException {
                Criteria c = s.createCriteria(clazz);
                if(limit != 0){
                    c.setFirstResult(start);
                    c.setMaxResults(limit);
                }
                addRestrictions.addRestrictions(c);        
                return c.list();
            }

        });
    }

}


这个上面search方法里传的接口AddRestrictions 
Java code

package com.lulu.ssh.dao;

import org.hibernate.Criteria;

public interface AddRestrictions {
    public void addRestrictions(Criteria c);
}


这个是business调用dao
Java code

package com.lulu.ssh.business;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.lulu.ssh.dao.AddRestrictions;
import com.lulu.ssh.dao.DAO;
import com.lulu.ssh.model.User;

@Component("userBusiness")
public class UserBusiness {
    private DAO<User> dao;

    public DAO<User> getDao() {
        return dao;
    }

    @Resource
    public void setDao(DAO<User> dao) {
        this.dao = dao;
    }

    public User getUser(int id) {
        return dao.get(User.class, id);
    }

        //这里可以分页,limit传0就不分页
    public List<User> getUsers(final User condition, final int start, final int limit) throws Exception {
        return dao.search(User.class, start, limit, new AddRestrictions() {

            @Override
            public void addRestrictions(Criteria c) {
                //要什么条件这里按需求添加
                                c.add(Restrictions.like("descn", condition.getDescn(), MatchMode.ANYWHERE));
                c.add(Restrictions.eq("name", condition.getName()));

            }
        });
    }
}


可以觉得这样的封装感觉总怪怪的 添加条件那块显得有点死板, 想寻一个好的方案,希望不吝教教小弟。

------解决方案--------------------
http://blog.csdn.net/hzw2312/article/details/6396590
------解决方案--------------------
[code=Java][/code]
package com.lxh.base;

import java.util.List;

import org.hibernate.LockMode;