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

📄 storageinfodaoimpl.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.StorageHouse;
import com.shunshi.ssh.entity.StorageInfo;
import com.shunshi.ssh.exception.StoragehouseDaoException;
import com.shunshi.ssh.exception.StorageinfoDaoException;
import com.shunshi.ssh.exception.StorageinfoServiceException;

public class StorageinfoDaoImpl extends HibernateDaoSupport implements IStorageinfoDao{

	public void add(StorageInfo Storageinfo)throws StorageinfoDaoException {
		try{
			getHibernateTemplate().saveOrUpdate(Storageinfo);
		}catch(RuntimeException e){
			throw new StorageinfoDaoException(e.getMessage());
		}

	}

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

	}

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

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

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

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

	}

	public void updateStorageinfo(StorageInfo s) throws StorageinfoDaoException {
		try{
			getHibernateTemplate().saveOrUpdate(s);
		}catch(RuntimeException e){
			throw new StorageinfoDaoException(e.getMessage());
		}

	}

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

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

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

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

}

⌨️ 快捷键说明

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