package cn.itcast.dao.impl; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import cn.itcast.dao.BookDao; import cn.itcast.domain.Book; import cn.itcast.exception.DaoException; import cn.itcast.util.DBCPUtil; public class BookDaoImpl implements BookDao { private QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource()); public void addBook(Book book) { try { qr .update( "insert into book (id,name,author,price,image,description,category_id) values(?,?,?,?,?,?,?)", book.getId(), book.getName(), book.getAuthor(), book.getPrice(), book.getImage(), book .getDescription(), book.getCategory_id()); } catch (SQLException e) { throw new DaoException(e); } } public List<Book> findPageBooks(int startIndex, int pagesize) { try { return qr.query("select * from book limit ?,?", new BeanListHandler<Book>(Book.class), startIndex,pagesize); } catch (SQLException e) { throw new DaoException(e); } } public int getTotalRecord() { try { Long num = (Long) qr.query("select count(*) from book", new ScalarHandler(1)); return num.intValue(); } catch (SQLException e) { throw new DaoException(e); } } public List findPageBooks(int startindex, int pagesize, String categoryId) { try { return qr.query("select * from book where category_id=? limit ?,?", new BeanListHandler<Book>(Book.class), categoryId,startindex,pagesize); } catch (SQLException e) { throw new DaoException(e); } } public int getTotalRecord(String categoryId) { try { Long num = (Long) qr.query("select count(*) from book where category_id=?", new ScalarHandler(1),categoryId); return num.intValue(); } catch (SQLException e) { throw new DaoException(e); } } public Book findBookById(String bookId) { try { return qr.query("select * from book where id=?", new BeanHandler<Book>(Book.class), bookId); } catch (SQLException e) { throw new DaoException(e); } } }
最近下载更多
asddwh LV12
2023年12月29日
1222222222222222222 LV2
2023年12月17日
3159792465 LV10
2023年9月1日
自知则知之1 LV2
2023年5月26日
微信网友_6468115061182464 LV1
2023年5月9日
Gjc175636312 LV2
2023年4月20日
13593938914 LV1
2023年1月29日
微信网友_6250372517498880 LV1
2022年12月6日
小白学代码301 LV1
2022年11月22日
微信网友_6206233028890624 LV2
2022年11月5日
最近浏览更多
吞吞吐吐她 LV4
3月28日
550423p
2月18日
暂无贡献等级
李先婷 bkxo LV2
1月29日
微信网友_6824860447887360 LV1
1月16日
walden123 LV1
1月4日
asddwh LV12
2023年12月26日
YhXyHx523 LV6
2023年12月24日
17693282606 LV11
2023年12月20日
Shengyi188
2023年12月20日
暂无贡献等级
1222222222222222222 LV2
2023年12月17日