package cn. javabs. book. entity;
public class Category {
private Integer id;
private String name;
private String description;
public Category ( ) {
super ( ) ;
}
@Override
public String toString ( ) {
return "Category{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}' ;
}
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getDescription ( ) {
return description;
}
public void setDescription ( String description) {
this . description = description;
}
public Category ( Integer id, String name, String description) {
this . id = id;
this . name = name;
this . description = description;
}
}
package cn. javabs. book. entity;
public class Book {
private Integer id;
private String name;
private String author;
private String publish;
private double price;
private String description;
private String path;
private String photoName;
public Book ( ) {
super ( ) ;
}
@Override
public String toString ( ) {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", publish='" + publish + '\'' +
", price=" + price +
", description='" + description + '\'' +
", path='" + path + '\'' +
", photoName='" + photoName + '\'' +
", category=" + category +
'}' ;
}
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getAuthor ( ) {
return author;
}
public void setAuthor ( String author) {
this . author = author;
}
public String getPublish ( ) {
return publish;
}
public void setPublish ( String publish) {
this . publish = publish;
}
public double getPrice ( ) {
return price;
}
public void setPrice ( double price) {
this . price = price;
}
public String getDescription ( ) {
return description;
}
public void setDescription ( String description) {
this . description = description;
}
public String getPath ( ) {
return path;
}
public void setPath ( String path) {
this . path = path;
}
public String getPhotoName ( ) {
return photoName;
}
public void setPhotoName ( String photoName) {
this . photoName = photoName;
}
public Category getCategory ( ) {
return category;
}
public void setCategory ( Category category) {
this . category = category;
}
public Book ( Integer id, String name, String author, String publish, double price, String description, String path, String photoName, Category category) {
this . id = id;
this . name = name;
this . author = author;
this . publish = publish;
this . price = price;
this . description = description;
this . path = path;
this . photoName = photoName;
this . category = category;
}
private Category category;
}
package cn. javabs. book. dao;
import cn. javabs. book. entity. Category;
import java. util. List;
public interface CategoryDao {
int addCategory ( Category category) ;
int delCategory ( int id) ;
int updateCategory ( Category category) ;
List< Category> getAllCategory ( ) ;
Category getCategoryById ( int id) ;
}
package cn. javabs. book. dao. impl;
import cn. javabs. book. dao. CategoryDao;
import cn. javabs. book. entity. Category;
import cn. javabs. book. util. DruidUtils;
import org. apache. commons. dbutils. QueryRunner;
import org. apache. commons. dbutils. handlers. BeanHandler;
import org. apache. commons. dbutils. handlers. BeanListHandler;
import java. sql. SQLException;
import java. util. List;
public class CategoryDaoImpl implements CategoryDao {
QueryRunner qr = new QueryRunner ( DruidUtils. getDataSource ( ) ) ;
@Override
public int addCategory ( Category category) {
try {
int rows = qr. update ( "insert into category(name,descrtion)value (?,?)" , category. getName ( ) , category. getDescription ( ) ) ;
return rows;
} catch ( SQLException e) {
throw new RuntimeException ( e) ;
}
}
@Override
public int delCategory ( int id) {
try {
int rows = qr. update ( "delete from category where id=?" , id) ;
return rows;
} catch ( SQLException e) {
throw new RuntimeException ( e) ;
}
}
@Override
public int updateCategory ( Category category) {
try {
return qr. update ( "update category set nmae=?,description=? where id=?" ,
category. getName ( ) ,
category. getDescription ( ) ,
category. getId ( ) ) ;
} catch ( SQLException e) {
throw new RuntimeException ( e) ;
}
}
@Override
public List< Category> getAllCategory ( ) {
try {
List< Category> categoryList = qr. query ( "select*from category" , new BeanListHandler < Category> ( Category. class ) ) ;
return categoryList;
} catch ( SQLException e) {
throw new RuntimeException ( e) ;
}
}
@Override
public Category getCategoryById ( int id) {
try {
Category category = qr. query ( "select * from category where id =?" , new BeanHandler < Category> ( Category. class ) , id) ;
return category;
} catch ( SQLException e) {
throw new RuntimeException ( e) ;
}
}
}
package cn. javabs. book. dao;
import cn. javabs. book. entity. Book;
import java. util. List;
public interface BookDao {
int addBook ( Book book) ;
int delBook ( int id) ;
int updateBook ( Book book) ;
Book getBookById ( int id) ;
List< Book> getAllBookRecords ( int startIndex, int pageSize ) ;
int getBooksNumber ( ) ;
int getPageBookNumber ( int categoryId) ;
List< Book> getPageBooks ( int starIndex , int pageSize, int categoryId) ;
}
package cn. javabs. book. dao. impl;
import cn. javabs. book. dao. BookDao;
import cn. javabs. book. entity. Book;
import cn. javabs. book. exe. BookDaoImplException;
import cn. javabs. book. util. DruidUtils;
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 java. sql. SQLException;
import java. util. List;
public class BookDaoImpl implements BookDao {
QueryRunner qr = new QueryRunner ( DruidUtils. getDataSource ( ) ) ;
@Override
public int addBook ( Book book) {
try {
int rows = qr. update ( "insert into book (name,author,publish,price,description,path,photoName,categoryId)value (?,?,?,?,?,?,?,?)" ,
book. getName ( ) ,
book. getAuthor ( ) ,
book. getPublish ( ) ,
book. getPrice ( ) ,
book. getDescription ( ) ,
book. getPath ( ) ,
book. getPhotoName ( ) ,
book. getCategory ( ) . getId ( ) ) ;
return rows;
} catch ( SQLException e) {
throw new BookDaoImplException ( e) ;
}
}
@Override
public int delBook ( int id) {
try {
int rows = qr. update ( "delete from book where id=?" , id) ;
return rows;
} catch ( SQLException e) {
throw new BookDaoImplException ( e) ;
}
}
@Override
public int updateBook ( Book book) {
try {
int rows = qr. update ( "update book set name=?,author=?,publish=?,price=?,description=?,path=?,photoName=?,categoryId=? where id=?" ,
book. getName ( ) ,
book. getAuthor ( ) ,
book. getPublish ( ) ,
book. getPrice ( ) ,
book. getDescription ( ) ,
book. getPath ( ) ,
book. getPhotoName ( ) ,
book. getCategory ( ) . getId ( ) ,
book. getId ( ) ) ;
return rows;
} catch ( SQLException e) {
throw new BookDaoImplException ( e) ;
}
}
@Override
public Book getBookById ( int id) {
try {
Book book = qr. query ( "select * from book where id=?" , new BeanHandler < > ( Book. class ) , id) ;
return book;
} catch ( SQLException e) {
throw new BookDaoImplException ( e) ;
}
}
@Override
public List< Book> getAllBookRecords ( int startIndex, int pageSize) {
try {
List< Book> bookList = qr. query ( "select * from book limit" , new BeanListHandler < Book> ( Book. class ) , startIndex, pageSize) ;
return bookList;
} catch ( SQLException e) {
throw new BookDaoImplException ( e) ;
}
}
@Override
public int getBooksNumber ( ) {
try {
Object obj = qr. query ( "select count (*) from book" , new ScalarHandler < > ( 1 ) ) ;
Long num = ( Long) obj;
int number = num. intValue ( ) ;
return number;
} catch ( SQLException e) {
throw new BookDaoImplException ( e) ;
}
}
@Override
public int getPageBookNumber ( int categoryId) {
try {
Object obj = qr. query ( "select count (*) from book where categoryId =?" , new ScalarHandler < > ( 1 ) , categoryId) ;
Long num = ( Long) obj;
int number = num. intValue ( ) ;
return number;
} catch ( SQLException e) {
throw new BookDaoImplException ( e) ;
}
}
@Override
public List< Book> getPageBooks ( int starIndex, int pageSize, int categoryId) {
try {
List< Book> bookList = qr. query ( "select * from book where catagoryId =? ,limit ?,?" , new BeanListHandler < Book> ( Book. class ) , categoryId, starIndex, pageSize) ;
return bookList;
} catch ( SQLException e) {
throw new BookDaoImplException ( e) ;
}
}
}
package cn. javabs. book. service;
import cn. javabs. book. entity. Category;
import java. util. List;
public interface CategoryService {
int addCategory ( Category category) ;
int updateCategory ( Category category) ;
int delCategory ( int id) ;
Category getCategoryById ( int id) ;
List< Category> getAllCategory ( ) ;
}
package cn. javabs. book. service. impl;
import cn. javabs. book. dao. CategoryDao;
import cn. javabs. book. dao. impl. CategoryDaoImpl;
import cn. javabs. book. entity. Category;
import cn. javabs. book. service. CategoryService;
import java. util. List;
public class CategoryServiceImpl implements CategoryService {
CategoryDao categoryDao = new CategoryDaoImpl ( ) ;
@Override
public int addCategory ( Category category) {
return categoryDao. addCategory ( category) ;
}
@Override
public int updateCategory ( Category category) {
return categoryDao. updateCategory ( category) ;
}
@Override
public int delCategory ( int id) {
return categoryDao. delCategory ( id) ;
}
@Override
public Category getCategoryById ( int id) {
return categoryDao. getCategoryById ( id) ;
}
@Override
public List< Category> getAllCategory ( ) {
return categoryDao. getAllCategory ( ) ;
}
}
package cn. javabs. book. service;
import cn. javabs. book. entity. Book;
import cn. javabs. book. util. Page;
public interface BookService {
int addBook ( Book book) ;
int updateBook ( Book book) ;
int delBook ( int id) ;
Page findAllBooks ( int pageNum) ;
Page findAllBookPageRecords ( int pageNum, int categoryId ) ;
}
package cn. javabs. book. service. impl;
import cn. javabs. book. dao. BookDao;
import cn. javabs. book. dao. impl. BookDaoImpl;
import cn. javabs. book. entity. Book;
import cn. javabs. book. service. BookService;
import cn. javabs. book. util. Page;
import java. util. List;
public class BookServiceImpl implements BookService {
BookDao bookDao = new BookDaoImpl ( ) ;
@Override
public int addBook ( Book book) {
return bookDao. addBook ( book) ;
}
@Override
public int updateBook ( Book book) {
return bookDao. updateBook ( book) ;
}
@Override
public int delBook ( int id) {
return bookDao. delBook ( id) ;
}
@Override
public Page findAllBooks ( int pageNum) {
pageNum = 1 ;
int bookNumber = bookDao. getBooksNumber ( ) ;
Page page = new Page ( pageNum, bookNumber) ;
int startIndex = page. getStartIndex ( ) ;
int pagePageSize = page. getPageSize ( ) ;
List< Book> list = bookDao. getAllBookRecords ( startIndex, pagePageSize) ;
page. setRecords ( list) ;
return page;
}
@Override
public Page findAllBookPageRecords ( int pageNum, int categoryId) {
return null;
}
}
package cn. javabs. book. exe;
import java. sql. SQLException;
public class BookDaoImplException extends RuntimeException {
public BookDaoImplException ( ) {
super ( ) ;
}
public BookDaoImplException ( String message) {
super ( message) ;
}
public BookDaoImplException ( String message, Throwable cause) {
super ( message, cause) ;
}
public BookDaoImplException ( Throwable cause) {
super ( cause) ;
}
protected BookDaoImplException ( String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super ( message, cause, enableSuppression, writableStackTrace) ;
}
}
package cn. javabs. book. util;
import com. alibaba. druid. pool. DruidDataSourceFactory;
import javax. sql. DataSource;
import java. io. InputStream;
import java. sql. Connection;
import java. sql. SQLException;
import java. util. Properties;
public class DruidUtils {
public static DataSource dataSource;
static {
try {
InputStream in = DruidUtils. class . getClassLoader ( ) . getResourceAsStream ( "jdbc.properties" ) ;
Properties properties = new Properties ( ) ;
properties. load ( in) ;
dataSource = DruidDataSourceFactory. createDataSource ( properties) ;
} catch ( Exception e) {
throw new RuntimeException ( e) ;
}
}
public static DataSource getDataSource ( ) {
return dataSource;
}
public static Connection getConnection ( ) throws SQLException {
return dataSource. getConnection ( ) ;
}
}
package cn. javabs. book. util;
import java. util. List;
public class Page {
private List records;
private int pageSize = 3 ;
private int currentPage ;
private int totalPage;
private int prePageNum;
private int nextPageNum;
private int startIndex;
private int totalRecord;
private String url;
public List getRecords ( ) {
return records;
}
public void setRecords ( List records) {
this . records = records;
}
public int getPageSize ( ) {
return pageSize;
}
public void setPageSize ( int pageSize) {
this . pageSize = pageSize;
}
public int getCurrentPage ( ) {
return currentPage;
}
public void setCurrentPage ( int currentPage) {
this . currentPage = currentPage;
}
public int getTotalPage ( ) {
return totalPage;
}
public void setTotalPage ( int totalPage) {
this . totalPage = totalPage;
}
public int getPrePageNum ( ) {
prePageNum = currentPage- 1 > 0 ? currentPage - 1 : 1 ;
return prePageNum;
}
public void setPrePageNum ( int prePageNum) {
this . prePageNum = prePageNum;
}
public int getNextPageNum ( ) {
nextPageNum = currentPage + 1 > totalPage ? totalPage : currentPage + 1 ;
return nextPageNum;
}
public void setNextPageNum ( int nextPageNum) {
this . nextPageNum = nextPageNum;
}
public int getStartIndex ( ) {
return startIndex;
}
public void setStartIndex ( int startIndex) {
this . startIndex = startIndex;
}
public int getTotalRecord ( ) {
return totalRecord;
}
public void setTotalRecord ( int totalRecord) {
this . totalRecord = totalRecord;
}
public String getUrl ( ) {
return url;
}
public void setUrl ( String url) {
this . url = url;
}
public Page ( int currentPage, int totalRecord) {
this . currentPage = currentPage;
this . totalRecord = totalRecord;
totalPage = totalRecord % pageSize == 0 ? totalRecord / pageSize : totalRecord / pageSize+ 1 ;
}
}
driverClassName= com. mysql. jdbc. Driver
url= jdbc: mysql
username= root
password= root
create DataBase BookStore;
create table Category (
id int primary key,
name varchar ( 20 ) not null,
description varchar ( 20 ) not null
) ;
create table book (
id int primary key auto_increment,
name varchar ( 40 ) not null,
author varchar ( 30 ) ,
publish varchar ( 30 ) ,
price varchar ( 30 ) ,
description varchar ( 50 ) ,
path varchar ( 30 ) ,
photoName varchar ( 30 ) ,
categoryId int ,
) ;
alter table book add foreign key ( category) references category ( id) ;
! [ 在这里插入图片描述] ( https: / / img- blog. csdnimg. cn/ 20210108112922834. png? x- oss- process= image/ watermark, type_ZmFuZ3poZW5naGVpdGk, shadow_10, text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQ2MjA3NzM