图书信息管理模块实现

1.数据库t_book表,主键id为bigint类型,type_id为bigint类型(图书所属类别的id)

图书信息管理模块实现
文章图片
image.png 2.录入一些数据,注意type_id字段,一定要是t_type表存在的id

图书信息管理模块实现
文章图片
image.png 3.图书实体类Book.java

package com.soft1841.book.entity; import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleLongProperty; import javafx.beans.property.SimpleStringProperty; /** * 图书实体类 */ public class Book { private final SimpleLongProperty id = new SimpleLongProperty(); private final SimpleLongProperty typeId = new SimpleLongProperty(); private final SimpleStringProperty name = new SimpleStringProperty(""); private final SimpleStringProperty author = new SimpleStringProperty(""); private final SimpleDoubleProperty price = new SimpleDoubleProperty(); private final SimpleStringProperty cover = new SimpleStringProperty(""); private final SimpleStringProperty summary = new SimpleStringProperty(""); private final SimpleIntegerProperty stock = new SimpleIntegerProperty(); public Book() {}public Book(Long id, Long typeId, String name, String author, Double price, String cover, String summary,Integer stock) { setId(id); setTypeId(typeId); setName(name); setAuthor(author); setPrice(price); setCover(cover); setSummary(summary); setStock(stock); }public long getId() { return id.get(); }public SimpleLongProperty idProperty() { return id; }public void setId(long id) { this.id.set(id); }public long getTypeId() { return typeId.get(); }public SimpleLongProperty typeIdProperty() { return typeId; }public void setTypeId(long typeId) { this.typeId.set(typeId); }public String getName() { return name.get(); }public SimpleStringProperty nameProperty() { return name; }public void setName(String name) { this.name.set(name); }public String getAuthor() { return author.get(); }public SimpleStringProperty authorProperty() { return author; }public void setAuthor(String author) { this.author.set(author); }public double getPrice() { return price.get(); }public SimpleDoubleProperty priceProperty() { return price; }public void setPrice(double price) { this.price.set(price); }public String getCover() { return cover.get(); }public SimpleStringProperty coverProperty() { return cover; }public void setCover(String cover) { this.cover.set(cover); }public String getSummary() { return summary.get(); }public SimpleStringProperty summaryProperty() { return summary; }public void setSummary(String summary) { this.summary.set(summary); }public int getStock() { return stock.get(); }public SimpleIntegerProperty stockProperty() { return stock; }public void setStock(int stock) { this.stock.set(stock); } }

4.BookDAO接口
package com.soft1841.book.dao; import cn.hutool.db.Entity; import com.soft1841.book.entity.Book; import java.sql.SQLException; import java.util.List; /** * 图书DAO */ public interface BookDAO {/** * 新增图书,返回自增主键 * * @param book * @return * @throws SQLException */ Long insertBook(Book book) throws SQLException; /** * 根据id删除图书 * * @param id * @return */ int deleteBookById(long id) throws SQLException; /** * 更新图书信息 * * @param book * @return */ int updateBook(Book book) throws SQLException; /** * 查询所有图书 * * @return */ List selectAllBooks() throws SQLException; /** * 根据id查询图书信息 * * @param id * @return */ Entity getBookById(long id) throws SQLException; /** * 根据书名关键词模糊查询图书 * @param keywords * @return * @throws SQLException */ List selectBooksLike(String keywords) throws SQLException; /** * 根据图书类别查询图书 * @param typeId * @return * @throws SQLException */ List selectBooksByTypeId(long typeId) throws SQLException; /** * 根据图书类别统计图书数量 * @param typeId * @return * @throws SQLException */ int countByType(long typeId) throws SQLException; }

5.BookDAOImpl实现类
package com.soft1841.book.dao.impl; import cn.hutool.db.Db; import cn.hutool.db.Entity; import cn.hutool.db.sql.Condition; import com.soft1841.book.dao.BookDAO; import com.soft1841.book.entity.Book; import java.sql.SQLException; import java.util.List; public class BookDAOImpl implements BookDAO { @Override public Long insertBook(Book book) throws SQLException { return Db.use().insertForGeneratedKey( Entity.create("t_book") .set("type_id", book.getTypeId()) .set("name", book.getName()) .set("author", book.getAuthor()) .set("price", book.getPrice()) .set("cover", book.getCover()) .set("summary", book.getSummary()) .set("stock",book.getStock()) ); }@Override public int deleteBookById(long id) throws SQLException { return Db.use().del( Entity.create("t_book").set("id", id) ); }@Override public int updateBook(Book book) throws SQLException { //只修改了图书的价格和库存 return Db.use().update( Entity.create().set("price", book.getPrice()) .set("stock",book.getStock()), Entity.create("t_book").set("id", book.getId()) ); }@Override public List selectAllBooks() throws SQLException { return Db.use().query("SELECT * FROM t_book "); }@Override public Entity getBookById(long id) throws SQLException { return Db.use().queryOne("SELECT * FROM t_book WHERE id = ? ", id); }@Override public List selectBooksLike(String keywords) throws SQLException { return Db.use().findLike("t_book", "name", keywords, Condition.LikeType.Contains); }@Override public List selectBooksByTypeId(long typeId) throws SQLException { return Db.use().query("SELECT * FROM t_book WHERE type_id = ? ", typeId); }@Override public int countByType(long typeId) throws SQLException { return Db.use().queryNumber("SELECT COUNT(*) FROM t_book WHERE type_id = ? ", typeId).intValue(); } }

6.DAOFactory工厂类中增加方法,获得BookDAO的实现类对象
public static BookDAO getBookDAOInstance() { return new BookDAOImpl(); }

7.单元测试
package com.soft1841.book.dao; import cn.hutool.db.Entity; import com.soft1841.book.entity.Book; import com.soft1841.book.utils.DAOFactory; import org.junit.Test; import java.sql.SQLException; import java.util.List; public class BookDAOTest { private BookDAO bookDAO = DAOFactory.getBookDAOInstance(); @Test public void insertBook() throws SQLException { Book book = new Book(); book.setTypeId(1); book.setName("测试书籍"); book.setAuthor("匿名"); System.out.println(bookDAO.insertBook(book)); }@Test public void deleteBookById() throws SQLException { bookDAO.deleteBookById(40); }@Test public void updateBook() throws SQLException { Book book = new Book(); book.setId(40); book.setPrice(11.1); book.setStock(99); bookDAO.updateBook(book); }@Test public void selectAllBooks() throws SQLException { List bookList = bookDAO.selectAllBooks(); bookList.forEach(entity -> System.out.println(entity.getStr("name"))); }@Test public void getBookById() throws SQLException { Entity entity = bookDAO.getBookById(1); System.out.println(entity); }@Test public void selectBooksLike() throws SQLException { List bookList = bookDAO.selectBooksLike("少"); bookList.forEach(entity -> System.out.println(entity.getStr("name"))); }@Test public void selectBooksByTypeId() throws SQLException { List bookList = bookDAO.selectBooksByTypeId(1); bookList.forEach(entity -> System.out.println(entity.getStr("name"))); }@Test public void countByType() throws SQLException { int n = bookDAO.countByType(1); System.out.println(n); } }

8.book.fxml布局文件

9.add_book.fxml布局文件