Hibernate读取数据


public interface GenericDao<T, PK> {
    public Session getSession()
    public void saveOrUpdate(T e);
    public void save(T e);
    public void update(T e);
}

@Repository(value = "genericDao")
public class GenericDaoImpl<T, PK extends Serializable> implements
        GenericDao<T, PK> {

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

    public Session getSession() {
        // 事务必须(Required)是开启的,否则获取不到
        return sessionFactory.getCurrentSession();
    }

 
    public void saveOrUpdate(T e) {
        getSession().saveOrUpdate(e);
    }

    public void save(T e) {
        getSession().save(e); // 这里需要测一下需要不需要返回E
    }

    public void update(T e) {
        getSession().update(e);
    }
}


public interface ChartDao extends GenericDao<Chart,Long>{
    public List<Chart> getChartList(String month,String area);
    public Long getCountByIncomeId(String incomeId);
}



@Repository("chartDao")public class ChartDaoImpl extends GenericDaoImpl<Chart, Long> implements ChartDao {


    @Override
    public List<Chart> getChartList(String month, String area) {
        List<Chart> list = new ArrayList<Chart>();


        //查询方式一:通过标准SQL查询
//        StringBuilder sb = new StringBuilder();
//        sb.append("select * from TEST_CHART WHERE STATIS_MONTH='" + month + "' AND AREA_CODE='" + area + "'");
//        list = getSession().createSQLQuery(sb.toString()).addEntity(Chart.class).list();


        //查询方式二:通过Hibernate的Criteria查询,Criteria查询对面向对象进行了封装
//        Criteria criteria = getSession().createCriteria(Chart.class);
//        if (!(null == month && "".equals(month))) {
//            criteria.add(Restrictions.eq("month", month));
//        }
//        if (!(null == area && "".equals(area))) {
//            criteria.add(Restrictions.eq("area", area));
//        }
//        list=criteria.list();


        //查询方式三:通过HQL(Hibernate Query Language)查询,Query查询
        StringBuilder sb = new StringBuilder();
        sb.append("FROM Chart model where model.month=:tempMonth");
        sb.append(" AND ");
        sb.append("model.area=:tempArea");
        Query q = getSession().createQuery(sb.toString());
        q.setParameter("tempMonth", month);
        q.setParameter("tempArea", area);
        list = q.list();


        return list;
    }


    @Override
    public Long getCountByIncomeId(String incomeId) {
//    Session session = super.getSession();  
//    long count = (Long) session.createQuery("select count(model) from Chart model").uniqueResult(); 
//    session.close();  
        //查询count,通过HQL查询
        Query q = getSession().createQuery("select count(c) from Chart c where c.incomeId=:strIncomeId");
        q.setParameter("strIncomeId", incomeId);
        long count = (Long) q.uniqueResult();
        return count;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值