utildaoimpl.java

来自「基于Sturts+Spring+Hibernate的一个高级销售管理系统。内容丰」· Java 代码 · 共 176 行

JAVA
176
字号
package com.yuanchung.sales.dao.util.impl;

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

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yuanchung.sales.dao.util.UtilDAO;
import com.yuanchung.sales.exception.SystemException;
import com.yuanchung.sales.util.Constants;

public class UtilDAOImpl extends HibernateDaoSupport implements UtilDAO {
	static Logger logger = Logger.getLogger(UtilDAOImpl.class);

	public List findByIds(String name, String field, List idList) {
		if (idList.size() > 0) {
			StringBuffer hql = new StringBuffer("");
			hql.append("from " + name + " as model where model." + field
					+ " in (");
			for (int i = 0; i < idList.size(); i++) {
				if (i != (idList.size() - 1)) {
					hql.append(idList.get(i).toString() + ",");
				} else {
					hql.append(idList.get(i).toString() + ")");
				}
			}
			logger.debug("hql : " + hql.toString());
			try{
				return getHibernateTemplate().find(hql.toString());
			}catch(RuntimeException re){
				logger.error(re);
				throw new SystemException(Constants.SYSTEMEXCEPTION);
			}
		} else {
			return null;
		}
	}

	public List findByIds(String name, String field, String[] idList) {
		if (idList.length > 0) {
			StringBuffer hql = new StringBuffer("");
			hql.append("from " + name + " as model where model." + field
					+ " in (");
			for (int i = 0; i < idList.length; i++) {
				if (i != (idList.length - 1)) {
					hql.append(idList[i] + ",");
				} else {
					hql.append(idList[i] + ")");
				}
			}
			try{
				return getHibernateTemplate().find(hql.toString());
			}catch(RuntimeException re){
				logger.error(re);
				throw new SystemException(Constants.SYSTEMEXCEPTION);
			}
		} else {
			return null;
		}
	}

	public void deleteAll(Collection all) {
		try{
			getHibernateTemplate().deleteAll(all);
		}catch(RuntimeException re){
			logger.error(re);
			throw new SystemException(Constants.SYSTEMEXCEPTION);
		}
	}

	public List findByField(String tableName, String field, String conditions,
			String value) {
		StringBuffer hql = new StringBuffer("");
		List result = new ArrayList();
		String pre = "'";
		String rear = "'";
		if ("like".equals(conditions.toLowerCase())) {
			pre = "'%";
			rear = "%'";
		}
		hql.append("from " + tableName + " as model where model." + field + " "
				+ conditions + " " + pre + value + rear);
		logger.debug("hql : " + hql.toString());
		try {
			result =  getHibernateTemplate().find(hql.toString());
		} catch (RuntimeException re) {
			logger.error(re);
			throw new SystemException(Constants.SYSTEMEXCEPTION);
		}
		return result;
	}
	
	public List findDataByPage(final String hql,final int currentPage,final int pageCount){
		return this.getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session){
				Query query = session.createQuery(hql);
				query.setFirstResult(currentPage);
				query.setMaxResults(pageCount);
				return query.list();
			}
		});
	}
	/**
	 * @author LuWenBang
	 * @date 2009_02_27
	 * @function 搜索定量的数据;
	 */
	public List findDataByPageSql(final String sql,final int currentPage,final int pageCount){
		return this.getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session){
				Query query = session.createSQLQuery(sql);
				query.setFirstResult(currentPage);
				query.setMaxResults(pageCount);
				return query.list();
			}
		});
	}
	/**
	 * @author LuWenBang;
	 * @date 2009-03-02
	 * 根据sql语句搜索列表数量大小
	 * @param hql
	 * @return
	 */
	public int getResultSetCountBySql(String sql){
		Integer count = null;
		String temp = null;
		List list = null;
		try {
			//Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
			Session session = this.getSession();
			Query query = session.createSQLQuery(sql);
			list = query.list();
			if(list.size()>0) {
				for(int i=0;i<list.size();i++) {
					if(i==0) {
						temp = list.get(i).toString();
					}
				}
			}
			count = Integer.parseInt(temp);
		} catch(RuntimeException e) {
			logger.debug(e.getMessage());
		}
		return count;
		/*return this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Integer count = null;
				Query query = session.createSQLQuery(sql);
				query.executeUpdate();
				if(list.size()>0) {
					for(int i=0;i<list.size();i++) {
						if(i==0) {
							count = (Integer) list.get(i);
						}
					}
				}
				return 0;
			}
		});*/
	}
	public int getResultsetCount(String hql){
		String count = getHibernateTemplate().find(hql).get(0).toString();
		return Integer.parseInt(count);
	}
}

⌨️ 快捷键说明

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