关闭

数据查询

130人阅读 评论(0) 收藏 举报
分类:

package common.interfaces.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hikvision.test.usermanager.common.interfaces.BaseDao;

/**
 * Dao超类
 * @author shenjingyi
 *
 */
public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {

	/**
	 * 添加一条数据
	 */
	public boolean add(Object obj) {
		try {
			getHibernateTemplate().save(obj);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 删除一条数据
	 */
	public boolean delete(Object obj) {
		try {
			getHibernateTemplate().delete(obj);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 删除一组数据
	 */
	public boolean deleteAll(List<?> list) {
		try {
			getHibernateTemplate().deleteAll(list);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 通过主键删除一条数据
	 */
	@SuppressWarnings("unchecked")
	public boolean deleteById(Class model, int id) {
		Object obj = findById(model, id);
		if (obj == null) {
			return false;
		} else {
			getHibernateTemplate().delete(obj);
			return true;
		}
	}

	/**
	 * 获得所有数据
	 */
	@SuppressWarnings("unchecked")
	public List<?> findAll(Class model) {
		return getHibernateTemplate().find("from " + model.getSimpleName());
	}

	/**
	 * 通过id获得对应的那条数据
	 */
	@SuppressWarnings("unchecked")
	public Object findById(Class model, int id) {
		return getHibernateTemplate().get(model.getName(), id);
	}

	/**
	 * 通过单属性获得对应的所有数据
	 */
	@SuppressWarnings("unchecked")
	public List<?> findByProperty(Class model, String propertyName,
			Object propertValue) {
		String query = "from " + model.getSimpleName() + " as m where m." + propertyName
			+ "=?";
		System.out.println("*********" + query + "***********" + propertValue);
		return getHibernateTemplate().find(query, propertValue);
	}
	
	/**
	 * 通过多属性获得对应的所有数据
	 */
	@SuppressWarnings("unchecked")
	public List<?> findByMulProperty(Class model, String[] propertyName, Object[] propertValue) {
		String query = "from " + model.getSimpleName() + " as m where m." + propertyName[0]
		+ "=? and m." + propertyName[1] + "=?";
		System.out.println("*********" + query + "***********" + propertValue);
		return getHibernateTemplate().find(query, propertValue);
	}

	/**
	 * 模糊查询
	 */
	@SuppressWarnings("unchecked")
	public List<?> findByLikeQuery(Class model, String propertyName, String str) {
		String query = "from " + model.getSimpleName() + " as m where m." + propertyName
			+ " like '" + str + "%'";
		System.out.println("*********" + query + "***********");
		return getHibernateTemplate().find(query);
	}
	
	/**
	 * 查询数据的总条数
	 */
	@SuppressWarnings("unchecked")
	public Integer getRows(Class model) {
		StringBuffer hql = new StringBuffer();
	    hql.append("select count(m) from " + model.getSimpleName());
	    Query query = this.getSession().createQuery(hql.substring(0));
	    return (Integer)query.uniqueResult();
	}

	/**
	 * 修改特定数据
	 */
	public boolean update(Object obj) {
		try {
			getHibernateTemplate().update(obj);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	
	
	/**
	 * 查询符合条件的数据条数
	 */
	@SuppressWarnings("unchecked")
	public Integer getRows(Class model, String hql, Map map) {
		hql = "select count(m) from " + model.getSimpleName() + " m" + hql;
		StringBuilder strHql = new StringBuilder(hql);
		if (map.size() != 0) {
			if (hql.indexOf("where") == -1) {
				strHql.append(" where ");
			} else {
				strHql.append(" and ");
			}
			
			Iterator<Entry<String,Object>> it = map.entrySet().iterator();
			int flag = 0;
	        while(it.hasNext()) {
	        	Entry<String,Object> entry = it.next();
	        	if (flag == 1) {
	        		strHql.append(" and");
	        	}
	        	flag = 1;
	        	if ("m.nRegionId".equals(entry.getKey())) {
	        		strHql.append(" " + entry.getKey() + "=" + entry.getValue());
	        	} else {
	        		strHql.append(" " + entry.getKey() + "='" + entry.getValue() + "'");
	        	}
	        }    
		}
		Query query = this.getSession().createQuery(strHql.toString());
		long total = (Long) query.uniqueResult();
	    return (int) total;
	}
	
	/**
	 * 分页查询符合所有条件的记录
	 * 
	 * @param pageNo 当前页数
	 * @param pageSize 每页显示的条数
	 * @param hql 查询语句
	 * @return 查询结果
	 */
	@SuppressWarnings("unchecked")
	public List<?> findPageByQuery(int pageNo, int pageSize, String hql, Map map) {
		List<Object[]> result = null;
	    try {  
	    	StringBuilder strHql = new StringBuilder(hql);
	        if (map.size() != 0) {
		        Iterator<Entry<String,Object>> it = map.entrySet().iterator();
		        while(it.hasNext()) {
		        	Entry<String,Object> entry = it.next();
		        	if ("m.nRegionId".equals(entry.getKey())) {
		        		strHql.append(" and " + entry.getKey() + "=" + entry.getValue());
		        	} else {
		        		strHql.append(" and " + entry.getKey() + "='" + entry.getValue() + "'");
		        	}
		        }    
	        }
	        Query query = this.getSession().createQuery(strHql.toString());
	        query.setFirstResult((pageNo-1)*pageSize);
	        query.setMaxResults(pageSize);
	        result = query.list();
	    } catch(RuntimeException re) {  
	    	re.printStackTrace();
	    }
	    return result;
	}

}

多表联查的时候最好不要超过两张表


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:18108次
    • 积分:825
    • 等级:
    • 排名:千里之外
    • 原创:65篇
    • 转载:13篇
    • 译文:0篇
    • 评论:1条
    最新评论