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

📄 bookdao.java

📁 模仿当当网基于struts+hierbernate与mysql的商务网站。
💻 JAVA
字号:
package org.whatisjava.dang.dao;import java.util.List;import org.hibernate.FlushMode;import org.hibernate.Query;import org.hibernate.Session;import org.whatisjava.dang.domain.Book;public class BookDao {	/**	 * 按上架时间 降序	 */	public final static int ADD_TIME_DESC = 0;	/**	 * 按上架时间 升序	 */	public final static int ADD_TIME_ASC = 1;	/**	 * 按出版时间 降序	 */	public final static int PUBLISH_TIME_DESC = 2;	/**	 * 按出版时间 升序	 */	public final static int PUBLISH_TIME_ASC = 3;	/**	 * 按价格 降序	 */	public final static int DANG_PRICE_DESC = 4;	/**	 * 按价格 升序	 */	public final static int DANG_PRICE_ASC = 5;	private static final String[] ORDER_BY = {  "add_time desc" ,			"add_time asc", "publish_time desc" ,"publish_time asc",			"dang_price desc","dang_price asc"};	public List<Book> findBookByPage(int cId, int orderBy, int numPerPage,			int page) {		if (orderBy < 0 || orderBy >= ORDER_BY.length) {			orderBy = ADD_TIME_DESC;		}		Session session = HbSessionFactory.getSession();		session.setFlushMode(FlushMode.NEVER);		StringBuilder hql = new StringBuilder(				"from Book b where b.category_id=? order by b.")				.append(ORDER_BY[orderBy]);		Query query = session.createQuery(hql.toString());		query.setInteger(0, cId);		query.setMaxResults(numPerPage);		query.setFirstResult((page - 1) * numPerPage);		List<Book> books = query.list();		session.close();		return books;	}	public int getBookTotalPageNum(int cId, int numPerPage) {		Session session = HbSessionFactory.getSession();		session.setFlushMode(FlushMode.NEVER);		Query query = session				.createQuery("select count(*) from Book b where b.category_id=?");		query.setInteger(0, cId);		int num = (Integer) query.list().get(0);		session.close();		if (num % numPerPage == 0) {			return num / numPerPage;		} else {			return num / numPerPage + 1;		}	}	public Book getById(int id) {		Session session=HbSessionFactory.getSession();		session.setFlushMode(FlushMode.NEVER);		Book book=(Book)session.get(Book.class, id);		session.close();		return book;	}}

⌨️ 快捷键说明

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