Dao包BookDao类
public interface BookDao { void insertBook(Book book); int findAllBookNumber(); List<Book> findAllBookRecords(int startIndex,int pageSize); void remove(String bookId); Book getBookById(String bookId); void updateBook(Book book); int findPageBookNumber(String categoryId); List<Book> findPageBooks(int startIndex, int pageSize, String categoryId); }View Code
BookDaoImpl类
public class BookDaoImpl implements BookDao { QueryRunner qr = new QueryRunner(DbcpUtils.getDataSource()); public void insertBook(Book book) { try { qr.update("insert into book(id,name,description,author,publish,price,path,photoname,categoryId) values (?,?,?,?,?,?,?,?,?)", book.getId(), book.getName(), book.getDescription(), book.getAuthor(), book.getPublish(), book.getPrice(), book.getPath(), book.getPhotoName(), book.getCategoryId() ); } catch (SQLException e) { throw new AddBookException(e); /* throw new RuntimeException(e); */ } } public int findAllBookNumber() { try { Object obj = qr.query("select count(*) from book", new ScalarHandler(1)); Long num = (Long) obj; return num.intValue(); } catch (SQLException e) { throw new QueryBookException(e); /* throw new RuntimeException(e); */ } } public List<Book> findAllBookRecords(int startIndex, int pageSize) { try { List<Book> book = qr.query("select * from book limit ?,?", new BeanListHandler<Book>(Book.class),startIndex,pageSize); System.out.println("book:"+book); return book; } catch (SQLException e) { throw new RuntimeException(e); } } public void remove(String bookId) { try { qr.update("delete from book where id = ?",bookId); } catch (SQLException e) { throw new RuntimeException(e); } } public Book getBookById(String bookId) { try { Book book = qr.query("select * from book where id = ?", new BeanHandler<Book>(Book.class),bookId); return book; } catch (SQLException e) { throw new RuntimeException(e); } } public void updateBook(Book book) { try { qr.update("update book set name=?,description=?,author=?,publish=?,price=?,path=?,photoname=?,categoryId=? where id =?", book.getName(), book.getDescription(), book.getAuthor(), book.getPublish(), book.getPrice(), book.getPath(), book.getPhotoName(), book.getCategoryId(), book.getId() ); } catch (SQLException e) { throw new RuntimeException(e); } } @Override public int findPageBookNumber(String categoryId) { try { Object obj = qr.query("select count(*) from book where categoryId = ?",new ScalarHandler(1),categoryId); Long num = (Long) obj; return num.intValue(); } catch (SQLException e) { throw new RuntimeException(e); } } @Override public List<Book> findPageBooks(int startIndex, int pageSize, String categoryId) { try { return qr.query("select * from book where categoryId = ? limit ?,?",new BeanListHandler<Book>(Book.class),categoryId,startIndex,pageSize); } catch (SQLException e) { throw new RuntimeException(e); } } }View Code
CategoryDao类
public interface CategoryDao { void save(Category category); List<Category> getAllCategory(); Category getCategoryById(String id); void removeCategory(String c); void updateCategory(Category category); }View Code
CategoryDaoImpl类
public class CategoryDaoImpl implements CategoryDao { QueryRunner qr = new QueryRunner(DbcpUtils.getDataSource()); public void save(Category category) { try { qr.update("insert into category(id,name,description) values (?,?,?) ", category.getId(), category.getName(), category.getDescription()); } catch (SQLException e) { throw new RuntimeException(e); } } public List<Category> getAllCategory() { try { return qr.query("select * from category", new BeanListHandler<Category>(Category.class)); } catch (SQLException e) { throw new RuntimeException(e); } } public Category getCategoryById(String id) { try { Category c = qr.query("select * from category where id = ?",new BeanHandler<Category>(Category.class),id); return c; } catch (SQLException e) { throw new RuntimeException(e); } } public void removeCategory(String categoryId) { try { System.out.println("dao实现的id: " +categoryId); qr.update("delete from category where id = ? ",categoryId); } catch (SQLException e) { throw new RuntimeException(e); } } public void updateCategory(Category category) { try { qr.update("update category set name = ?,description= ? where id = ? ", category.getName(), category.getDescription(), category.getId()); } catch (SQLException e) { throw new RuntimeException(e); } } }View Code
UserDao类
public interface UserDao { void save(User user); User login(String username, String password, String type); }View Code
UserDaoImpl类
public class UserDaoImpl implements UserDao { QueryRunner qr = new QueryRunner(DbcpUtils.getDataSource()); public void save(User user) { try { qr.update("insert into user(id,username,password,type,sex) values (?,?,?,?,?)", user.getId(), user.getUsername(), user.getPassword(), user.getType(), user.getSex() ); } catch (SQLException e) { throw new RuntimeException(e); } } public User login(String username, String password,String type) { try { System.out.println("..."+username+password+type); User user = qr.query("select * from user where username = ? and password = ? and type = ?", new BeanHandler<User>(User.class),username,password,type); System.out.println("dao'user"+user); return user; } catch (SQLException e) { throw new RuntimeException(e); } } }View Code
DbcpUtils类
public class DbcpUtils { public static DataSource dataSource; static{ try { String myFile = "dbcp.properties"; InputStream in = DbcpUtils.class.getClassLoader().getResourceAsStream(myFile); Properties p = new Properties(); p.load(in); dataSource = BasicDataSourceFactory.createDataSource(p); } catch (Exception e) { throw new RuntimeException(e); } } public static DataSource getDataSource(){ return dataSource; } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } }View Code