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

📄 goodsdaoimpl.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.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.shunshi.ssh.entity.Goods;
import com.shunshi.ssh.exception.GoodsDaoException;
import com.shunshi.ssh.exception.GoodsServiceException;

public class GoodsDaoImpl extends HibernateDaoSupport implements IGoodsDao{
	
	public void add(Goods goods) throws GoodsDaoException {
		try {
			getHibernateTemplate().saveOrUpdate(goods);

		} catch (DataAccessException e) {
			throw new GoodsDaoException(e.getMessage());
		}			
	}

	public Goods findById(long id) throws GoodsDaoException {	
		try {
			Goods instance = (Goods) getHibernateTemplate().get(
					"com.shunshi.ssh.entity.Goods", id);
			return instance;
		} catch (DataAccessException e) {
			throw new GoodsDaoException(e.getMessage());
		}	
	}
	
	public List findByUserId(long userId) throws GoodsDaoException {
		try {
			String queryString = "from Goods g where g.userId=?";
			return getHibernateTemplate()
					.find(queryString, new Long(userId));
		} catch (DataAccessException e) {
			throw new GoodsDaoException(e.getMessage());
		}	
	}

	public List findAll() throws GoodsDaoException {
		try {
			String queryString = "from Goods";
			return getHibernateTemplate().find(queryString);
		} catch (DataAccessException e) {
			throw new GoodsDaoException(e.getMessage());
		}	
	}

	public void delete(long id) throws GoodsDaoException {
		try {
			Goods g=findById(id);
			if(g!=null)
				getHibernateTemplate().delete(findById(id));
		} catch (DataAccessException e) {
			throw new GoodsDaoException(e.getMessage());
		}	
	}

	public void updateGoods(Goods goods) throws GoodsDaoException {
		try {
			getHibernateTemplate().saveOrUpdate(goods);
		} catch (DataAccessException e) {
			throw new GoodsDaoException(e.getMessage());
		}	
	}

	public void updateState(long id, int state) throws GoodsDaoException {
		try {
			System.out.println("dao  updateState run");
			Goods g = findById(id);
			if (g != null)
				g.setState(new Integer(state));
			getHibernateTemplate().update(g);
		} catch (DataAccessException e) {
			throw new GoodsDaoException(e.getMessage());
		}	
	}

	public List findByState(final int state, final int start, final int max)
			throws GoodsDaoException {
		
		List goodsInfos=(List)getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q=session.createQuery("from Goods g where g.state=:state");
				q.setInteger("state",state);
				q.setMaxResults(max);
				q.setFirstResult(start);
				List goodsInfos =new ArrayList();
				List<Goods> goods=q.list();
				for(Goods g:goods){
					Map goodsMap=new HashMap();
					goodsMap.put("topic",g.getTopic());
					goodsMap.put("publishTime",g.getPublishTime());
					goodsMap.put("userName",g.getUserName());
					goodsMap.put("state",g.getState());
					goodsMap.put("id",g.getId());
					goodsInfos.add(goodsMap);
				}
				return goodsInfos;
			}		
		});
		
		return goodsInfos;		
	}
	
	public Long getTotalRowsByState(int state) throws GoodsDaoException {
		try{
			return (Long)getHibernateTemplate().find("select count(*) from Goods g where g.state=?",state).get(0);
		}catch(RuntimeException e){
			throw new GoodsDaoException(e.getMessage());
		}
	}	
	public Goods findGoodsByMaxId(){
		String hql="select Max(id) from Goods goods";
		Long id=(Long)getHibernateTemplate().find(hql).get(0);
		Goods goods=null;
		try {
			goods=findById(id.longValue());
		} catch (GoodsDaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return goods;
	}
}

⌨️ 快捷键说明

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