ssh练手(六)BaseDaoHibernate4.java

package com.chenjia.common.dao.impl;

import org.hibernate.*;

import com.chenjia.common.dao.BaseDao;

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


/**
 *
 * @author chenjia
 *
 * @param <T>
 */
public class BaseDaoHibernate4<T> implements BaseDao<T>
{
    // DAO组件进行持久化操作底层依赖的SessionFactory组件
    private SessionFactory sessionFactory;
    // 依赖注入SessionFactory所需的setter方法
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
    public SessionFactory getSessionFactory()
    {
        return this.sessionFactory;
    }
    // 根据ID加载实体
    @SuppressWarnings("unchecked")
    public T get(Class<T> entityClazz , Serializable id)
    {
        return (T)getSessionFactory().getCurrentSession()
            .get(entityClazz , id);
    }
    // 保存实体
    public Serializable save(T entity)
    {
        return getSessionFactory().getCurrentSession()
            .save(entity);
    }
    // 更新实体
    public void update(T entity)
    {
        getSessionFactory().getCurrentSession().saveOrUpdate(entity);
    }
    // 删除实体
    public void delete(T entity)
    {
        getSessionFactory().getCurrentSession().delete(entity);
    }
    // 根据ID删除实体
    public void delete(Class<T> entityClazz , Serializable id)
    {
        getSessionFactory().getCurrentSession()
            .createQuery("delete " + entityClazz.getSimpleName()
                + " en where en.id = ?0")
            .setParameter("0" , id)
            .executeUpdate();
    }
    // 获取所有实体
    public List<T> findAll(Class<T> entityClazz)
    {
        return find("select en from "
            + entityClazz.getSimpleName() + " en");
    }
    // 获取实体总数
    
    public long findCount(Class<T> entityClazz)
    {
        List<?> l = find("select count(*) from "
            + entityClazz.getSimpleName());
        // 返回查询得到的实体总数
        if (l != null && l.size() == 1 )
        {
            return (Long)l.get(0);
        }
        return 0;
    }

    // 根据HQL语句查询实体
    @SuppressWarnings("unchecked")
    protected List<T> find(String hql)
    {
        return (List<T>)getSessionFactory().getCurrentSession()
            .createQuery(hql)
            .list();
    }
    // 根据带占位符参数HQL语句查询实体
    @SuppressWarnings("unchecked")
    protected List<T> find(String hql , Object... params)
    {
        // 创建查询
        Query query = getSessionFactory().getCurrentSession()
            .createQuery(hql);
        // 为包含占位符的HQL语句设置参数
        for(int i = 0 , len = params.length ; i < len ; i++)
        {
            query.setParameter(i + "" , params[i]);
        }
        return (List<T>)query.list();
    }
    /**
     * 使用hql 语句进行分页查询操作
     * @param hql 需要查询的hql语句
     * @param pageNo 查询第pageNo页的记录
     * @param pageSize 每页需要显示的记录数
     * @return 当前页的所有记录
     */
    @SuppressWarnings("unchecked")
    protected List<T> findByPage(String hql,
         int pageNo, int pageSize)
    {
        // 创建查询
        return getSessionFactory().getCurrentSession()
            .createQuery(hql)
            // 执行分页
            .setFirstResult((pageNo - 1) * pageSize)
            .setMaxResults(pageSize)
            .list();
    }
    /**
     * 使用hql 语句进行分页查询操作
     * @param hql 需要查询的hql语句
     * @param params 如果hql带占位符参数,params用于传入占位符参数
     * @param pageNo 查询第pageNo页的记录
     * @param pageSize 每页需要显示的记录数
     * @return 当前页的所有记录
     */
    @SuppressWarnings("unchecked")
    protected List<T> findByPage(String hql , int pageNo, int pageSize
        , Object... params)
    {
        // 创建查询
        Query query = getSessionFactory().getCurrentSession()
            .createQuery(hql);
        // 为包含占位符的HQL语句设置参数
        for(int i = 0 , len = params.length ; i < len ; i++)
        {
            query.setParameter(i + "" , params[i]);
        }
        // 执行分页,并返回查询结果
        return query.setFirstResult((pageNo - 1) * pageSize)
            .setMaxResults(pageSize)
            .list();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值