borrowdaoimpl.java

来自「实现图书的借阅和管理信息化」· Java 代码 · 共 264 行

JAVA
264
字号
package com.moonman.libraryManager.model.daoImpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moonman.libraryManager.model.dao.BorrowDAO;
import com.moonman.libraryManager.model.vo.Books;
import com.moonman.libraryManager.model.vo.Borrow;
import com.moonman.libraryManager.model.vo.Punish;
import com.moonman.libraryManager.model.vo.Store;

public class BorrowDAOImpl extends HibernateDaoSupport implements BorrowDAO {

	/**
	 * 添加一条借阅图书信息
	 */
	public boolean addBorrow(Borrow borrow) {
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.save(borrow);
			tx.commit();
			return true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	/**
	 * 删除一条图书借阅记录
	 * 
	 * @param borrowId
	 * @return
	 */
	public boolean deleteBorrowByBorrowId(String borrowId) {
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session
					.createQuery("delete from Borrow where jyid=?").setInteger(
							0, Integer.parseInt(borrowId));
			int flag = query.executeUpdate();
			tx.commit();
			return flag > 0;
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	/**
	 * 根据读者编号查询读者借书记录
	 * 
	 * @param readerId
	 * @return
	 */
	public List<Object[]> getStoresByReaderId(String readerId) {
		Session session = this.getSession();
		try {
			Query query = session.createQuery(
					"select distinct bookId from Borrow where readerId=?")
					.setString(0, readerId);
			return query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据读者编号查询读者的信息
	 * 
	 * @param readerId
	 * @return
	 */
	public List<Borrow> getBooksByReaderId(String readerId) {
		Session session = this.getSession();
		try {
			Query query = session.createQuery("from Borrow where readerId=?")
					.setString(0, readerId);
			return query.list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

	/**
	 * 根据索取号获得官藏信息
	 * 
	 * @param sqh
	 * @return
	 */
	public Borrow getStoreBySqh(Integer sqh) {
		Session session = this.getSession();
		try {
			Query query = session.createQuery("from Borrow where bookId=?")
					.setInteger(0, sqh);
			return (Borrow) query.uniqueResult();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

	/**
	 * 根据用户编号和索取号查询用户的交易信息
	 * 
	 * @param readerId
	 * @param sqh
	 * @return
	 */
	public Borrow getBorrowByReaderIdAndSqh(Integer readerId, Integer sqh) {
		Session session = this.getSession();
		try {
			Query query = session
					.createQuery("from Borrow where readerId=? and bookId=?");
			query.setInteger(0, readerId);
			query.setInteger(1, sqh);
			return (Borrow) query.uniqueResult();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

	/**
	 * 更新借阅信息的状态
	 * 
	 * @param borrow
	 * @return
	 */
	public boolean modifyBorrowByBorrow(Borrow borrow) {
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.update(borrow);
			tx.commit();
			return true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	/**
	 * 根据读者编号和借阅状态查询读者的信息
	 * 
	 * @param readerId
	 * @param 图书状态
	 * @return
	 */
	public List<Borrow> getBorrowsByReaderIdAndStatus(Integer readerId,
			Integer status) {
		Session session = this.getSession();
		try {
			Query query = session.createQuery(
					"from Borrow where readerId=? and zt=?").setInteger(0,
					readerId).setInteger(1, status);
			return query.list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据用户编号和索取号,状态查询用户的交易信息
	 * 
	 * @param readerId
	 * @param sqh
	 * @return
	 */
	public Borrow getBorrowByReaderIdAndSqh(Integer readerId, Integer sqh,
			Integer status) {
		Session session = this.getSession();
		try {
			Query query = session
					.createQuery("from Borrow where readerId=? and bookId=? and zt=?");
			query.setInteger(0, readerId);
			query.setInteger(1, sqh);
			query.setInteger(2, status);
			return (Borrow) query.uniqueResult();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

	/**
	 * 添加一条罚款消息
	 * 
	 * @param punish
	 *            罚款实体
	 * @return
	 */
	public boolean addPunish(Punish punish) {
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.save(punish);
			tx.commit();
			return true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	/**
	 * 根据索取号码删除图书的信息
	 * 
	 * @param sqh
	 *            索取号码
	 * @return 是否删除成功
	 */
	public boolean deleteBorrowBySqh(String sqh) {
		Session session = this.getSession();
		Query query = session.createQuery("delete from Borrow where bookId=?")
				.setInteger(0, Integer.parseInt(sqh));
		query.executeUpdate();
		return true;
	}
}

⌨️ 快捷键说明

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