package com.java214.dao; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.java214.domain.Book; import com.java214.domain.BookType; @Service @Transactional public class BookDAO { @Resource SessionFactory factory; /*每页显示记录数目*/ private final int PAGE_SIZE = 5; /*保存查询后总的页数*/ private int totalPage; public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getTotalPage() { return totalPage; } /*保存查询到的总记录数*/ private int recordNumber; public void setRecordNumber(int recordNumber) { this.recordNumber = recordNumber; } public int getRecordNumber() { return recordNumber; } /*添加Book信息*/ public void AddBook(Book book) throws Exception { Session s = factory.getCurrentSession(); s.merge(book); } /*查询Book信息*/ @Transactional(propagation=Propagation.NOT_SUPPORTED) public ArrayList<Book> QueryBookInfo(String bookName,BookType bookType,String barcode,String publishDate,int currentPage) { Session s = factory.getCurrentSession(); String hql = "From Book book where 1=1"; if(!bookName.equals("")) hql = hql + " and book.bookName like '%" + bookName + "%'"; if(null != bookType && bookType.getBookTypeId() != null && bookType.getBookTypeId()!=0) hql += " and book.bookType.bookTypeId=" + bookType.getBookTypeId(); if(!barcode.equals("")) hql = hql + " and book.barcode like '%" + barcode + "%'"; if(!publishDate.equals("")) hql = hql + " and book.publishDate like '%" + publishDate + "%'"; Query q = s.createQuery(hql); /*计算当前显示页码的开始记录*/ int startIndex = (currentPage-1) * this.PAGE_SIZE; q.setFirstResult(startIndex); q.setMaxResults(this.PAGE_SIZE); List bookList = q.list(); return (ArrayList<Book>) bookList; } @Transactional(propagation=Propagation.NOT_SUPPORTED) public ArrayList<Book> QueryBookInfo(String bookName,BookType bookType,String barcode,String publishDate) { Session s = factory.getCurrentSession(); String hql = "From Book book where 1=1"; if(!bookName.equals("")) hql = hql + " and book.bookName like '%" + bookName + "%'"; if(null != bookType && bookType.getBookTypeId()!=0) hql += " and book.bookType.bookTypeId=" + bookType.getBookTypeId(); if(!barcode.equals("")) hql = hql + " and book.barcode like '%" + barcode + "%'"; if(!publishDate.equals("")) hql = hql + " and book.publishDate like '%" + publishDate + "%'"; Query q = s.createQuery(hql); List bookList = q.list(); return (ArrayList<Book>) bookList; } @Transactional(propagation=Propagation.NOT_SUPPORTED) public ArrayList<Book> QueryAllBookInfo() { Session s = factory.getCurrentSession(); String hql = "From Book"; Query q = s.createQuery(hql); List bookList = q.list(); return (ArrayList<Book>) bookList; } /*计算总的页数和记录数*/ @Transactional(propagation=Propagation.NOT_SUPPORTED) public void CalculateTotalPageAndRecordNumber(String bookName,BookType bookType,String barcode,String publishDate) { Session s = factory.getCurrentSession(); String hql = "From Book book where 1=1"; if(!bookName.equals("")) hql = hql + " and book.bookName like '%" + bookName + "%'"; if(null != bookType && bookType.getBookTypeId()!=null && bookType.getBookTypeId()!=0 ) hql += " and book.bookType.bookTypeId=" + bookType.getBookTypeId(); if(!barcode.equals("")) hql = hql + " and book.barcode like '%" + barcode + "%'"; if(!publishDate.equals("")) hql = hql + " and book.publishDate like '%" + publishDate + "%'"; Query q = s.createQuery(hql); List bookList = q.list(); recordNumber = bookList.size(); int mod = recordNumber % this.PAGE_SIZE; totalPage = recordNumber / this.PAGE_SIZE; if(mod != 0) totalPage++; } /*根据主键获取对象*/ @Transactional(propagation=Propagation.NOT_SUPPORTED) public Book GetBookByBarcode(String barcode) { Session s = factory.getCurrentSession(); Book book = (Book)s.get(Book.class, barcode); return book; } /*更新Book信息*/ public void UpdateBook(Book book) throws Exception { Session s = factory.getCurrentSession(); s.merge(book); } /*删除Book信息*/ public void DeleteBook (String barcode) throws Exception { Session s = factory.getCurrentSession(); Object book = s.load(Book.class, barcode); s.delete(book); } }
最近下载更多
Yht2219553 LV2
3月14日
958546428 LV3
2023年11月7日
Seaskye LV14
2023年11月4日
jiemomo LV12
2023年10月18日
zj20020510 LV5
2023年9月14日
huAres LV2
2023年9月3日
最代码官方 LV168
2023年8月25日
最近浏览更多
5454wpg
昨天
暂无贡献等级
571818771 LV3
昨天
momomo228 LV2
11月21日
Peny_ZH LV5
9月21日
yimaoermao LV1
9月14日
pangzhihui LV14
6月27日
微信网友_7055338587410432
6月27日
暂无贡献等级
TY0165 LV20
6月16日
sink122406 LV12
5月16日
yuquanzuo LV2
5月14日