public class BookDaoImpl extends BaseDao implements BookDao {
@Test
public void selectTest() throws Exception {
}
// 查询图书列表(分页)
@Override
public List<Book> selectbook(int pageIndex, int pageSize) throws Exception {
// 创建list集合存放book对象
List<Book> list = new ArrayList<Book>();
String sql = "select * from book limit ?,?";
Object[] obj = { pageIndex, pageSize };
ResultSet rs = executeSelect(sql, obj);
if (rs != null) {
while (rs.next()) {
// 创建book对象
Book book = new Book();
book.setBookid(rs.getInt("bookid"));
book.setBookname(rs.getString("bookname"));
book.setBookpicture(rs.getString("bookpicture"));
book.setBookprice(rs.getDouble("bookprice"));
book.setBookabout(rs.getString("bookabout"));
book.setBookauthor(rs.getString("bookauthor"));
book.setBookcategory(rs.getInt("bookcategory"));
book.setBookdatatime(rs.getDate("bookdatetime"));
list.add(book);
}
}
return list;
}
// 查询book表中的记录数
@Override
public int getCount() throws Exception {
int result = 0;
String sql = "select count(*) as num from book";
ResultSet rs = executeSelect(sql);
if (rs != null) {
if (rs.next()) {
result = rs.getInt("num");
}
closeAll();
}
return result;
}
// 按名称模糊查询(分页)
@Override
public List<Book> likebook(int category, String name,int pageIndex, int pageSize)
throws Exception {
// 创建list集合存放book对象
List<Book> list = new ArrayList<Book>();
StringBuffer sb=new StringBuffer("select * from book where 1=1");
if(category!=0)
{
sb=sb.append(" and bookcategory='"+category+"' ");
}
if(name!="")
{
sb=sb.append(" and bookname like '%"+name+"%'");
}
sb=sb.append(" limit ?,?");
Object[] obj = { pageIndex, pageSize };
ResultSet rs = executeSelect(sb.toString(), obj);
if (rs != null) {
while (rs.next()) {
// 创建book对象
Book book = new Book();
book.setBookid(rs.getInt("bookid"));
book.setBookname(rs.getString("bookname"));
book.setBookpicture(rs.getString("bookpicture"));
book.setBookprice(rs.getDouble("bookprice"));
book.setBookabout(rs.getString("bookabout"));
book.setBookauthor(rs.getString("bookauthor"));
book.setBookcategory(rs.getInt("bookcategory"));
book.setBookdatatime(rs.getDate("bookdatetime"));
list.add(book);
}
}
return list;
}
@Override
public int getselectCount(int category,String name) throws Exception {
int result = 0;
StringBuffer sb=new StringBuffer("select count(*) as num from book where 1=1 ");
if(category!=0)
{
sb=sb.append(" and bookcategory='"+category+"' ");
}
if(name!="")
{
sb=sb.append(" and bookname like '%"+name+"%'");
}
ResultSet rs = executeSelect(sb.toString());
if (rs != null) {
if (rs.next()) {
result = rs.getInt("num");
}
closeAll();
}