常用的hibernate泛型dao

来源:互联网 发布:蜀美网络招聘信息 编辑:程序博客网 时间:2024/06/05 02:34
import java.io.Serializable;
import java.util.List;

import com.java1234.entity.PageBean;

/**
 * »ù´¡Êý¾Ý¿â²Ù×÷Àà
 *
 * @author ss
 *
 */
public interface BaseDAO<T> {

    /**
     * ±£´æÒ»¸ö¶ÔÏó
     *
     * @param o
     * @return
     */
    public Serializable save(T o);

    /**
     * ɾ³ýÒ»¸ö¶ÔÏó
     *
     * @param o
     */
    public void delete(T o);

    /**
     * ¸üÐÂÒ»¸ö¶ÔÏó
     *
     * @param o
     */
    public void update(T o);

    /**
     * ±£´æ»ò¸üжÔÏó
     *
     * @param o
     */
    public void saveOrUpdate(T o);
    
    /**
     * ºÏ²¢¶ÔÏó
     * @param o
     */
    public void merge(T o);

    /**
     * ²éѯ
     *
     * @param hql
     * @return
     */
    public List<T> find(String hql);

    /**
     * ²éѯ¼¯ºÏ
     *
     * @param hql
     * @param param
     * @return
     */
    public List<T> find(String hql, Object[] param);

    /**
     * ²éѯ¼¯ºÏ
     *
     * @param hql
     * @param param
     * @return
     */
    public List<T> find(String hql, List<Object> param);

    /**
     * ²éѯ¼¯ºÏ(´ø·ÖÒ³)
     *
     * @param hql
     * @param param
     * @param page
     *            ²éѯµÚ¼¸Ò³
     * @param rows
     *            ÿҳÏÔʾ¼¸Ìõ¼Ç¼
     * @return
     */
    public List<T> find(String hql, Object[] param, PageBean pageBean);

    /**
     * ²éѯ¼¯ºÏ(´ø·ÖÒ³)
     *
     * @param hql
     * @param param
     * @param page
     * @param rows
     * @return
     */
    public List<T> find(String hql, List<Object> param, PageBean pageBean);

    /**
     * »ñµÃÒ»¸ö¶ÔÏó
     *
     * @param c
     *            ¶ÔÏóÀàÐÍ
     * @param id
     * @return Object
     */
    public T get(Class<T> c, Serializable id);

    /**
     * »ñµÃÒ»¸ö¶ÔÏó
     *
     * @param hql
     * @param param
     * @return Object
     */
    public T get(String hql, Object[] param);

    /**
     * »ñµÃÒ»¸ö¶ÔÏó
     *
     * @param hql
     * @param param
     * @return
     */
    public T get(String hql, List<Object> param);

    /**
     * select count(*) from Àà
     *
     * @param hql
     * @return
     */
    public Long count(String hql);

    /**
     * select count(*) from Àà
     *
     * @param hql
     * @param param
     * @return
     */
    public Long count(String hql, Object[] param);

    /**
     * select count(*) from Àà
     *
     * @param hql
     * @param param
     * @return
     */
    public Long count(String hql, List<Object> param);

    /**
     * Ö´ÐÐHQLÓï¾ä
     *
     * @param hql
     * @return ÏìÓ¦ÊýÄ¿
     */
    public Integer executeHql(String hql);

    /**
     * Ö´ÐÐHQLÓï¾ä
     *
     * @param hql
     * @param param
     * @return ÏìÓ¦ÊýÄ¿
     */
    public Integer executeHql(String hql, Object[] param);

    /**
     * Ö´ÐÐHQLÓï¾ä
     *
     * @param hql
     * @param param
     * @return
     */
    public Integer executeHql(String hql, List<Object> param);

    /**
     * Ö´ÐÐSQLÓï¾ä
     * @param sql
     * @return
     */
    public Integer executeSql(String sql);
}



BaseDaoImpl.class


import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.java1234.dao.BaseDAO;
import com.java1234.entity.PageBean;

@Repository("baseDAO")
@SuppressWarnings("all")
public class BaseDAOImpl<T> implements BaseDAO<T> {

    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

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

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    public Serializable save(T o) {
        return this.getCurrentSession().save(o);
    }

    public void delete(T o) {
        this.getCurrentSession().delete(o);
    }

    public void update(T o) {
        this.getCurrentSession().update(o);
    }

    public void saveOrUpdate(T o) {
        this.getCurrentSession().saveOrUpdate(o);
    }

    public List<T> find(String hql) {
        return this.getCurrentSession().createQuery(hql).list();
    }

    public List<T> find(String hql, Object[] param) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (param != null && param.length > 0) {
            for (int i = 0; i < param.length; i++) {
                q.setParameter(i, param[i]);
            }
        }
        return q.list();
    }

    public List<T> find(String hql, List<Object> param) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (param != null && param.size() > 0) {
            for (int i = 0; i < param.size(); i++) {
                q.setParameter(i, param.get(i));
            }
        }
        return q.list();
    }

    public List<T> find(String hql, Object[] param, PageBean pageBean) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (param != null && param.length > 0) {
            for (int i = 0; i < param.length; i++) {
                q.setParameter(i, param[i]);
            }
        }
        return q.setFirstResult(pageBean.getStart()).setMaxResults(pageBean.getPageSize()).list();
    }

    public List<T> find(String hql, List<Object> param, PageBean pageBean) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (param != null && param.size() > 0) {
            for (int i = 0; i < param.size(); i++) {
                q.setParameter(i, param.get(i));
            }
        }
        return q.setFirstResult(pageBean.getStart()).setMaxResults(pageBean.getPageSize()).list();
    }

    public T get(Class<T> c, Serializable id) {
        return (T) this.getCurrentSession().get(c, id);
    }

    public T get(String hql, Object[] param) {
        List<T> l = this.find(hql, param);
        if (l != null && l.size() > 0) {
            return l.get(0);
        } else {
            return null;
        }
    }

    public T get(String hql, List<Object> param) {
        List<T> l = this.find(hql, param);
        if (l != null && l.size() > 0) {
            return l.get(0);
        } else {
            return null;
        }
    }

    public Long count(String hql) {
        return  (Long) this.getCurrentSession().createQuery(hql).uniqueResult();
    }

    public Long count(String hql, Object[] param) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (param != null && param.length > 0) {
            for (int i = 0; i < param.length; i++) {
                q.setParameter(i, param[i]);
            }
        }
        return (Long) q.uniqueResult();
    }

    public Long count(String hql, List<Object> param) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (param != null && param.size() > 0) {
            for (int i = 0; i < param.size(); i++) {
                q.setParameter(i, param.get(i));
            }
        }
        return (Long) q.uniqueResult();
    }

    public Integer executeHql(String hql) {
        return this.getCurrentSession().createQuery(hql).executeUpdate();
    }

    public Integer executeHql(String hql, Object[] param) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (param != null && param.length > 0) {
            for (int i = 0; i < param.length; i++) {
                q.setParameter(i, param[i]);
            }
        }
        return q.executeUpdate();
    }

    public Integer executeHql(String hql, List<Object> param) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (param != null && param.size() > 0) {
            for (int i = 0; i < param.size(); i++) {
                q.setParameter(i, param.get(i));
            }
        }
        return q.executeUpdate();
    }

    public void merge(T o) {
        // TODO Auto-generated method stub
        this.getCurrentSession().merge(o);
    }

    public Integer executeSql(String sql) {
        Query q = this.getCurrentSession().createSQLQuery(sql);
        return q.executeUpdate();
    }

}


实现:

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.java1234.dao.BaseDAO;
import com.java1234.entity.PageBean;
import com.java1234.entity.User;
import com.java1234.service.UserService;
import com.java1234.util.StringUtil;

@Service("userService")
public class UserServiceImpl implements UserService{

    @Resource
    private BaseDAO<User> baseDAO;
    
    @Override
    public void saveUser(User user) {
        // TODO Auto-generated method stub
        baseDAO.merge(user);
    }

    @Override
    public boolean existUserWithUserName(String userName) {
        String hql="select count(*) from User where userName=?";
        long count=baseDAO.count(hql, new Object[]{userName});
        if(count>0){
            return true;
        }else{
            return false;            
        }
    }

    @Override
    public User login(User user) {
        List<Object> param=new LinkedList<Object>();
        StringBuffer hql=new StringBuffer("from User u where u.userName=? and u.password=?");
        param.add(user.getUserName());
        param.add(user.getPassword());
        if(user.getStatus()==2){
            hql.append(" and u.status=2");
        }
        return baseDAO.get(hql.toString(), param);
    }

    @Override
    public List<User> findUserList(User s_user, PageBean pageBean) {
        List<Object> param=new LinkedList<Object>();
        StringBuffer hql=new StringBuffer("from User");
        if(s_user!=null){
            if(StringUtil.isNotEmpty(s_user.getUserName())){
                hql.append(" and userName like ? ");
                param.add("%"+s_user.getUserName()+"%");
            }
        }
        hql.append(" and status=1");
        if(pageBean!=null){
            return baseDAO.find(hql.toString().replaceFirst("and", "where"), param, pageBean);
        }else{
            return null;            
        }
    }

    @Override
    public Long getUserCount(User s_user) {
        List<Object> param=new LinkedList<Object>();
        StringBuffer hql=new StringBuffer("select count(*) from User");
        if(s_user!=null){
            if(StringUtil.isNotEmpty(s_user.getUserName())){
                hql.append(" and userName like ? ");
                param.add("%"+s_user.getUserName()+"%");
            }
        }
        hql.append(" and status=1");
        return baseDAO.count(hql.toString().replaceFirst("and", "where"), param);
    }

    @Override
    public void delete(User user) {
        // TODO Auto-generated method stub
        baseDAO.delete(user);
    }

    @Override
    public User getUserById(int id) {
        return baseDAO.get(User.class, id);
    }

}


0 0
原创粉丝点击