⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 storagehousedaoimpl.java

📁 一个关于物流的管理系统
💻 JAVA
字号:
package com.shunshi.ssh.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.shunshi.ssh.entity.Agent;
import com.shunshi.ssh.entity.StorageHouse;
import com.shunshi.ssh.exception.AgentDaoException;
import com.shunshi.ssh.exception.StoragehouseDaoException;

public class StoragehouseDaoImpl extends HibernateDaoSupport implements IStoragehouseDao {

	public void add(StorageHouse Storagehouse) throws StoragehouseDaoException {
		try{
			getHibernateTemplate().save(Storagehouse);
		}catch(RuntimeException e){
			throw new StoragehouseDaoException(e.getMessage());
		}

	}

	public void delete(int id) throws StoragehouseDaoException {
		try{
			StorageHouse s=this.findById(id);
			getHibernateTemplate().delete(s);
		}catch(RuntimeException e){
			throw new StoragehouseDaoException(e.getMessage());
		}

	}

	public List findAll() throws StoragehouseDaoException {
		
		return null;
	}

	public StorageHouse findById(int id) throws StoragehouseDaoException {
		try{
			return (StorageHouse)getHibernateTemplate().find("from StorageHouse s where s.id=?", id).get(0);
		}catch(RuntimeException e){
			throw new StoragehouseDaoException(e.getMessage());
		}
		
	}

	public List findByUserId(int uid) throws StoragehouseDaoException {
		try{
			return getHibernateTemplate().find("from StorageHouse s where s.userId=?", uid);
		}catch(RuntimeException e){
			throw new StoragehouseDaoException(e.getMessage());
		}
	}

	public void updateState(int id, int state) throws StoragehouseDaoException {
		try{
			StorageHouse s=this.findById(id);
			if(s!=null){
				s.setState(state);
			}
			getHibernateTemplate().saveOrUpdate(s);
		}catch(RuntimeException e){
			throw new StoragehouseDaoException(e.getMessage());
		}

	}

	public void updateStoragehouse(StorageHouse s)
			throws StoragehouseDaoException {
		try{
			getHibernateTemplate().saveOrUpdate(s);
		}catch(RuntimeException e){
			throw new StoragehouseDaoException(e.getMessage());
		}

	}

	public List findByState( final int state, final int start, final int max)
			throws StoragehouseDaoException {
		try{
			List storageInfos=(List) getHibernateTemplate().execute(new HibernateCallback(){

				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					
					Query q=session.createQuery("from StorageHouse s where s.state=:state");
					q.setInteger("state",state);
					q.setMaxResults(max);
					q.setFirstResult(start);
					List storageInfos =new ArrayList();
					List<StorageHouse> storagehouse=q.list();
					int i=0;
					for(StorageHouse s:storagehouse){
						Map storagehouseMap=new HashMap();
						storagehouseMap.put("title",s.getTitle());
						storagehouseMap.put("timeLimit",s.getTimeLimit());
						storagehouseMap.put("userId",s.getUserId());
						storagehouseMap.put("state",s.getState());
						storagehouseMap.put("id", s.getId());
						storagehouseMap.put("xuhao",++i);
						storagehouseMap.put("publishDate", s.getPublishDate());
						storageInfos.add(storagehouseMap);
					}
					return storageInfos;
				}
			});
			return storageInfos;
		}catch(RuntimeException e){
			throw new StoragehouseDaoException(e.getMessage());
		}
		
	}

	public Long getTotalRowsByState(int state) throws StoragehouseDaoException {
		try{
			return  (Long) getHibernateTemplate().find("select count(*) from StorageHouse s where s.state=?",state).get(0);
		}catch(RuntimeException e){
			throw new StoragehouseDaoException(e.getMessage());
		}
	}

	public StorageHouse findByMaxId() throws StoragehouseDaoException {
		String hql="select Max(id) from StorageHouse s";
		Integer id=(Integer)getHibernateTemplate().find(hql).get(0);
		StorageHouse sh=null;
		try {
			sh=findById(id.intValue());
		} catch (StoragehouseDaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sh;
	}

}

⌨️ 快捷键说明

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