paymentdaohibernate.java

来自「这个是完整的wap项目的源码 开发语言 Java 系统架构 Struts +」· Java 代码 · 共 101 行

JAVA
101
字号
package com.longtime.wap.module.cost.dao.hibernate;

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

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.longtime.wap.common.BaseDao;
import com.longtime.wap.common.web.Page;
import com.longtime.wap.model.Payment;
import com.longtime.wap.module.cost.dao.PaymentDao;

/**
 * 消费记录Dao层实现代码
 * 
 * @author chenxq
 * @date 2007-11-23
 */
public class PaymentDaoHibernate extends BaseDao implements PaymentDao {

	/**
	 * 用来实现“根据id获取消费记录对象”的功能
	 * 
	 * @param id
	 *            消费记录id
	 * @return 消费记录对象
	 */
	public Payment retrievePaymentById(final Long id) {
		return (Payment) this.getHibernateTemplate().get(Payment.class, id);
	}

	/**
	 * 用来实现“获取某页消费记录对象集合”的功能
	 * 
	 * @param page
	 *            分页对象
	 * @return 消费记录对象集合
	 */
	public List retrievePayments(final Page page) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuilder hql = new StringBuilder(
						"from Payment order by paymentId");
				Query query = session.createQuery(hql.toString());
				return query.setFirstResult(page.getFirstResult())
						.setMaxResults(page.getPageSize()).list();
			}

		});
	}

	/**
	 * 用来实现“根据消费id数组获取消费记录集合”的功能
	 * 
	 * @param ids
	 *            消费记录id数组
	 * @return 消费记录对象集合
	 */
	public List retrievePaymentsByIds(final String[] ids) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuilder hql = new StringBuilder("from Payment where ");
				for (int i = 0; i < ids.length; i++) {
					hql.append("paymentId=" + ids[i]);
					if (i < ids.length - 1) {
						hql.append(" or ");
					}
				}
				Query query = session.createQuery(hql.toString());
				return query.list();
			}

		});
	}

	/**
	 * 用来实现“获取消费记录对象总数”的功能。在分页显示时需要用到。
	 * 
	 * @return 消费记录对象总数
	 */
	public int retrievePaymentsCount() {
		return (Integer) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						StringBuilder hql = new StringBuilder(
								"select count(*) from Payment");
						Query query = session.createQuery(hql.toString());
						return query.uniqueResult();
					}
				});
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?