1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| import java.sql.Connection; import java.util.List;
import com.atguigu.bookstore.beans.Book; import com.atguigu.bookstore.beans.Page; import com.atguigu.bookstore.dao.BaseDao; import com.atguigu.bookstore.dao.BookDao;
public class BookDaoImpl extends BaseDao<Book> implements BookDao {
@Override public List<Book> getBooks(Connection conn) { List<Book> beanList = null; 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) { String sql = "insert into books(title,author,price,sales,stock,img_path) values(?,?,?,?,?,?)"; update(conn,sql, book.getTitle(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(),book.getImgPath()); } @Override public void deleteBookById(Connection conn,String bookId) { String sql = "DELETE FROM books WHERE id = ?"; update(conn,sql, bookId); } @Override public Book getBookById(Connection conn,String bookId) { Book book = null; 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) { String sql = "update books set title = ? , author = ? , price = ? , sales = ? , stock = ? where id = ?"; 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) { String sql = "select count(*) from books"; long totalRecord = (long) getValue(conn,sql); page.setTotalRecord((int) totalRecord); String sql2 = "select id,title,author,price,sales,stock,img_path imgPath from books limit ?,?"; List<Book> beanList = getBeanList(conn,sql2, (page.getPageNo() - 1) * Page.PAGE_SIZE, Page.PAGE_SIZE); page.setList(beanList); return page; } @Override public Page<Book> getPageBooksByPrice(Connection conn,Page<Book> page, double minPrice, double maxPrice) { String sql = "select count(*) from books where price between ? and ?"; long totalRecord = (long) getValue(conn,sql,minPrice,maxPrice); page.setTotalRecord((int) totalRecord); String sql2 = "select id,title,author,price,sales,stock,img_path imgPath from books where price between ? and ? limit ?,?"; List<Book> beanList = getBeanList(conn,sql2, minPrice , maxPrice , (page.getPageNo() - 1) * Page.PAGE_SIZE, Page.PAGE_SIZE); page.setList(beanList); return page; }
}
|