package com.bookstore.dao.impl;
import java.sql.Connection;
import java.util.List;
import com.bookstore.beans.Book;
import com.bookstore.beans.Page;
import com.bookstore.dao.BaseDao;
import com.bookstore.dao.BookDao;
public class BookDaoImpl extends BaseDao<Book> implements BookDao {
@Override
public List<Book> getBooks(Connection conn) {
// 調(diào)用BaseDao中得到一個(gè)List的方法
List<Book> beanList = null;
// 寫(xiě)sql語(yǔ)句
String sql = "select id,title,author,price,sales,stock,img_path imgPath from books";
beanList = getBeanList(conn,sql);
return beanList;
}
@Override
public void saveBook(Connection conn,Book book) {
// 寫(xiě)sql語(yǔ)句
String sql = "insert into books(title,author,price,sales,stock,img_path) values(?,?,?,?,?,?)";
// 調(diào)用BaseDao中通用的增刪改的方法
update(conn,sql, book.getTitle(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(),book.getImgPath());
}
@Override
public void deleteBookById(Connection conn,String bookId) {
// 寫(xiě)sql語(yǔ)句
String sql = "DELETE FROM books WHERE id = ?";
// 調(diào)用BaseDao中通用增刪改的方法
update(conn,sql, bookId);
}
@Override
public Book getBookById(Connection conn,String bookId) {
// 調(diào)用BaseDao中獲取一個(gè)對(duì)象的方法
Book book = null;
// 寫(xiě)sql語(yǔ)句
String sql = "select id,title,author,price,sales,stock,img_path imgPath from books where id = ?";
book = getBean(conn,sql, bookId);
return book;
}
@Override
public void updateBook(Connection conn,Book book) {
// 寫(xiě)sql語(yǔ)句
String sql = "update books set title = ? , author = ? , price = ? , sales = ? , stock = ? where id = ?";
// 調(diào)用BaseDao中通用的增刪改的方法
update(conn,sql, book.getTitle(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getId());
}
@Override
public Page<Book> getPageBooks(Connection conn,Page<Book> page) {
// 獲取數(shù)據(jù)庫(kù)中圖書(shū)的總記錄數(shù)
String sql = "select count(*) from books";
// 調(diào)用BaseDao中獲取一個(gè)單一值的方法
long totalRecord = (long) getValue(conn,sql);
// 將總記錄數(shù)設(shè)置都page對(duì)象中
page.setTotalRecord((int) totalRecord);
// 獲取當(dāng)前頁(yè)中的記錄存放的List
String sql2 = "select id,title,author,price,sales,stock,img_path imgPath from books limit ?,?";
// 調(diào)用BaseDao中獲取一個(gè)集合的方法
List<Book> beanList = getBeanList(conn,sql2, (page.getPageNo() - 1) * Page.PAGE_SIZE, Page.PAGE_SIZE);
// 將這個(gè)List設(shè)置到page對(duì)象中
page.setList(beanList);
return page;
}
@Override
public Page<Book> getPageBooksByPrice(Connection conn,Page<Book> page, double minPrice, double maxPrice) {
// 獲取數(shù)據(jù)庫(kù)中圖書(shū)的總記錄數(shù)
String sql = "select count(*) from books where price between ? and ?";
// 調(diào)用BaseDao中獲取一個(gè)單一值的方法
long totalRecord = (long) getValue(conn,sql,minPrice,maxPrice);
// 將總記錄數(shù)設(shè)置都page對(duì)象中
page.setTotalRecord((int) totalRecord);
// 獲取當(dāng)前頁(yè)中的記錄存放的List
String sql2 = "select id,title,author,price,sales,stock,img_path imgPath from books where price between ? and ? limit ?,?";
// 調(diào)用BaseDao中獲取一個(gè)集合的方法
List<Book> beanList = getBeanList(conn,sql2, minPrice , maxPrice , (page.getPageNo() - 1) * Page.PAGE_SIZE, Page.PAGE_SIZE);
// 將這個(gè)List設(shè)置到page對(duì)象中
page.setList(beanList);
return page;
}
}