package com.computerbookshop.dao.book;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.computerbookshop.common.Common;
import com.computerbookshop.entity.BookType;
import com.computerbookshop.entity.Books;
import com.computerbookshop.entity.HotspotSearch;
import com.computerbookshop.entity.Publish;
public class BookDaoImpl extends HibernateDaoSupport implements BookDao{
//书籍分页
public List<Books> selectPageBook(final String hql,final int size, final int firstNum) {
List<Books> books=this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query =session.createQuery(hql);
query.setMaxResults(size);
query.setFirstResult(firstNum);
return query.list();
}});
return books;
}
/**
* 根据查询条件获取书籍数量
* @param hqlCount 带聚合函数count 的hql语句
* @return
*/
public int getTotalNum(final String hqlCount){
return Integer.parseInt(this.getHibernateTemplate().find(hqlCount).get(0).toString());
}
/**
* 书籍分页方法
* @param size 每页条数
* @param page 第几页
* @return firstResult 的Number
*/
public int selectBookForpage(int page,int size){
return (page-1)* size;
}
public List<Books> SelectAllBooks(int page, int size) {
DetachedCriteria hql = null;
hql = DetachedCriteria.forClass(Books.class);
//System.out.println("in bookdaoImpl method");
List<Books> books=this.getHibernateTemplate().findByCriteria(hql, page, size);
// if(books.size()>0)
// System.out.println("books.size()"+books.size());
return books;
}
public List<Books> selectBookByName(String name,int page){
List<Books> books =null;
// books=this.getHibernateTemplate().find(hql);
System.out.println("in bookdaoImpl booksearch"+books.size());
return books;
}
public List<HotspotSearch> selectHotspotSearch(final String hql) {
List<HotspotSearch> hotspotSearchs=this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query =session.createQuery(hql);
query.setMaxResults(5);
return query.list();
}});
return hotspotSearchs;
}
public List<Books> selectBookByCondition(final String hql,final int selectNum) {
List<Books> books=this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query=session.createQuery(hql);
//query.setFirstResult(0);
query.setMaxResults(selectNum);
return query.list();
}});
return Common.interceptBooksDesc(books);
}
// heyuanbo///heyuanbo///heyuanbo///heyuanbo///heyuanbo///heyuanbo/////////////////////////////
List<Books> books;
// 得到选择的页的书籍
public List<Books> selectBooksPerPage(final String hql, final int idIndex, final int length){
books = this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(idIndex);
query.setMaxResults(length);
List list = query.list();
return list;
}});
return books;
}
// 根据查询条件得到books表记录条数
public long getCounts(String condition, String bookTypeName, String publishName){
String hqlByType = getHQL(condition, bookTypeName, publishName);
String hqlCount = "select count(*) " + hqlByType;
long count = (Long)this.getHibernateTemplate().find(hqlCount).get(0);
return count;
}
// 根据查询条件获得页数
public int getAllPagesCount(String condition, String bookTypeName, String publishName){
long count = getCounts(condition, bookTypeName, publishName);
int totalPageCount = (int)Math.ceil(count/15.0);
return totalPageCount;
}
// 查看特定图书详细信息
public Books getBook(int bookId){
Books book = (Books)this.getHibernateTemplate().get(Books.class, new Integer(bookId));
if (book == null){
throw new ObjectRetrievalFailureException(Books.class, new Integer(bookId));
}
System.out.println("查看特定图书详细信息-书名: " + book.getBookName()); // 测试语句
return book;
}
// 得到页码(第几页)
public int getPageNum(int pageNum, String condition, String bookTypeName, String publishName){
int totalPageCount = getAllPagesCount(condition, bookTypeName, publishName);
if (pageNum > totalPageCount){
pageNum = totalPageCount;
}
if (pageNum < 1){
pageNum = 1;
}
return pageNum;
}
// 得到查询第一条记录的起始位置
public int getIdIndex(int pageNum, String condition, String bookTypeName, String publishName){
int idIndex = 15 * (getPageNum(pageNum, condition, bookTypeName, publishName) - 1);
return idIndex;
}
// 根据查询条件得到排序的查询语句
public String getHQLByOrder( int type, String condition, String bookTypeName, String publishName){
String hqlOrder = getHQL(condition, bookTypeName, publishName);
switch(type){
case 1:
hqlOrder += " order by bookClicks desc"; // 按点击率降序
break;
case 2:
hqlOrder += " order by bookName"; // 按字母升序
break;
case 3:
hqlOrder += " order by bookName desc"; // 按字母降序
break;
case 4:
hqlOrder += " order by bookPrice"; // 按价格升序
break;
case 5:
hqlOrder += " order by bookPrice desc"; // 按价格降序
break;
case 6:
hqlOrder += " order by publishTime"; // 按出版时间升序
break;
case 7:
hqlOrder += " order by publishTime desc"; // 按出版时间降序
break;
case 8:
hqlOrder += " order by bookAddTime"; // 按上架时间升序
break;
case 9:
hqlOrder += " order by bookAddTime desc"; // 按上架时间降序
break;
default:
hqlOrder += " order by bookClicks desc"; // 按点击率降序
break;
}
return hqlOrder;
}
// heyuanbo///heyuanbo///heyuanbo///heyuanbo///heyuanbo///heyuanbo/////////////////////////////
//&&&&&图书后台管理&&&&&&&图书后台管理&&&&&&图书后台管理&&&&&&&&&&&图书后台管理&&&&&&&图书后台管理&&&&//
// 查询所有的图书类别
public List<BookType> selectBookTypes(){
String hql = "from BookType order by typeName";
List<BookType> bookTypes = this.getHibernateTemplate().find(hql);
return bookTypes;
}
// 查询所有的出版社
public List<Publish> selectPublishes(){
String hql = "from Publish order by publishName";
List<Publish> publishes = this.getHibernateTemplate().find(hql);
return publishes;
}
// 图书入库
public boolean addBook(Books book){
try{
this.getHibernateTemplate().save(book);
}catch(Exception ex){
return false;
}
return true;
}
// 添加图书类别
public boolean addBookType(BookType bookType){
try{
this.getHibernateTemplate().save(bookType);
}catch(Exception ex){
return false;
}
return true;
}
// 添加出版社
public boolean addPublish(Publish publish){
try{
this.getHibernateTemplate().save(publish);
}catch(Exception ex){
return false;
}
return true;
}
// 修改图书信息
public boolean updateBookInfo(Books book) {
try{
this.getHibernateTemplate().update(book);
}catch(Exception ex){
return false;
}
return true;
}
// 根据查询条件得到不同的hql语句
public String getHQL(String condition, String bookTypeName, String publishName) {
String hql = "";
if (condition.equals(" ") && bookTypeName.equals(" ")
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java 毕业项目源码 (1346个子文件)
BookDaoImpl.class 11KB
BookBiz.class 7KB
BookReviewFrontAction.class 7KB
BookReviewAction.class 7KB
BookManagerAction.class 7KB
NewsAction.class 5KB
OrderDaoImpl.class 5KB
UpdateBookInfoAction.class 5KB
SearchAction.class 5KB
OrderAction.class 5KB
BackBookscanAction.class 5KB
BookReviewDaoImpl.class 5KB
BookscanAction.class 4KB
SearchBooksAction.class 4KB
OrderBiz.class 4KB
Books.class 4KB
UserManageAction.class 4KB
BackOrderBooksAction.class 4KB
OrderBooksAction.class 4KB
Users.class 4KB
UserForntAction.class 4KB
SearchOrderAction.class 4KB
UserDaoImpl.class 4KB
UserManageForm.class 3KB
UserForntForm.class 3KB
BookManagerForm.class 3KB
BooksAction.class 3KB
RoleControl.class 3KB
RegisterAction.class 3KB
AddBookTypeAction.class 3KB
AddPublishAction.class 3KB
NewsFrontAction.class 3KB
BookReviewBiz.class 3KB
NewsDaoImpl.class 3KB
Orders.class 2KB
HibernateSessionFactory.class 2KB
UpdateBookInfoForm.class 2KB
FileUpLoader.class 2KB
LoginBiz.class 2KB
RegisterForm.class 2KB
UserManageBiz.class 2KB
BookReview.class 2KB
LoginAction.class 2KB
BookDao.class 2KB
OrderDetail.class 2KB
BookReviewFrontForm.class 2KB
BookReviewForm.class 2KB
NewsForm.class 2KB
Page.class 2KB
Common.class 2KB
OrderDaoImpl$2.class 2KB
SearchOrderForm.class 2KB
SalestatAction.class 2KB
OrderDaoImpl$1.class 2KB
NewsBiz.class 2KB
BookReviewDaoImpl$1.class 1KB
MyFilter.class 1KB
OrderForm.class 1KB
SearchBooksForm.class 1KB
BookNews.class 1KB
BookDaoImpl$4.class 1KB
Publish.class 1KB
BookType.class 1KB
RegisterBiz.class 1KB
BookDaoImpl$1.class 1KB
BookTypeDaoImpl.class 1KB
BookDaoImpl$3.class 1KB
BookDaoImpl$2.class 1KB
LoginForm.class 1KB
SalestatForm.class 1KB
HotspotSearch.class 1KB
AddBookTypeForm.class 1KB
AddPublishForm.class 1KB
HotspotSearchDaoImpl.class 1KB
BackBookscanForm.class 1KB
BackOrderBooksForm.class 1KB
SearchForm.class 1KB
OrderBooksForm.class 1KB
BookscanForm.class 1KB
BooksForm.class 1KB
UserDao.class 1KB
ShopcarActionAction.class 1KB
HotspotSearchAction.class 1KB
HotspotSearchBiz.class 990B
BookReviewDao.class 963B
OrderDao.class 962B
CarItem.class 883B
CounterListener.class 868B
HotspotSearchForm.class 848B
ShopcarActionForm.class 848B
NewsFrontForm.class 836B
NewsDao.class 452B
Execption.class 323B
HotspotSearchDao.class 284B
BookTypeDao.class 260B
.classpath 1KB
andreas08.css 6KB
index.css 5KB
default.css 4KB
default.css 4KB
共 1346 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
励志的蜗牛
- 粉丝: 25
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功