实现图书的增删查改(CRUD)
图书的MySQL数据库创建脚本: CREATE TABLE books ( id int NOT NULL AUTO_INCREMENT, title varchar(255) NOT NULL, description text, image_url varchar(255), price decimal(8, 2) NOT NULL, author varchar(255) NOT NULL, isbn varchar(255) NOT NULL,
图书的MySQL数据库创建脚本:
CREATE TABLE books ( id int NOT NULL AUTO_INCREMENT, title varchar(255) NOT NULL, description text, image_url varchar(255), price decimal(8, 2) NOT NULL, author varchar(255) NOT NULL, isbn varchar(255) NOT NULL, publisher varchar(255) NOT NULL, user_id int NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
图书实体类文件Book.java主要代码:
public class Book { private Long id; private String title; private String description; private String image_url; private double price; private String author; private String isbn; private String publisher; private User user; ... }
在User类中添加集合变量books:
public class User { ... private Setbooks = new HashSet (); ... }
在User.hbm.xml中配置和图书的一对多关联:
图书的表映射文件Book.hbm.xml代码:
BookService.java中的代码相对简单,因为没什么复杂的业务逻辑,只是负责把Action和Dao这两层连接起来。
BookDao.java中的代码主要实现图书的读取,更新和删除:
package name.dohkoos.book.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import name.dohkoos.book.model.Book; public class BookDao { private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public ListgetBookList() { Session session = sessionFactory.openSession(); try { Query query = session.createQuery("from Book b"); List bookList = query.list(); return bookList; } finally { session.close(); } } public Book getBook(Long bookId) { Session session = sessionFactory.openSession(); try { Query query = session.createQuery("from Book b where b.id = :id"); query.setLong("id", bookId); query.setMaxResults(1); return (Book)query.uniqueResult(); } finally { session.close(); } } public void saveOrUpdateBook(Book book) { Session session = sessionFactory.openSession(); Transaction ts = null; try { ts = session.beginTransaction(); session.saveOrUpdate(book); ts.commit(); } finally { session.close(); } } public void deleteBook(Long bookId) { Session session = sessionFactory.openSession(); Transaction ts = null; try { ts = session.beginTransaction(); Book book = (Book)session.get(Book.class, bookId); session.delete(book); ts.commit(); } finally { session.close(); } } }
BookAction.java负责协调图书应用程序间的运转。接收访问请求,与模型进行交互,将合适的视图展示给用户:
package name.dohkoos.book.action; import java.util.List; import name.dohkoos.account.model.User; import name.dohkoos.book.model.Book; import name.dohkoos.book.service.BookService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class BookAction extends ActionSupport { private static final long serialVersionUID = 2538923417705852774L; private Long bookId; private Book book; private ListbookList; private BookService bookService; public String list() throws Exception { bookList = bookService.getBookList(); return "list"; } public String show() throws Exception { book = bookService.getBook(bookId); return "show"; } public String input() throws Exception { if (bookId != null) { book = bookService.getBook(bookId); } return INPUT; } public String saveOrUpdate() throws Exception { User user = (User)ActionContext.getContext().getSession().get("user"); book.setUser(user); bookService.saveOrUpdateBook(book); return SUCCESS; } public String delete() throws Exception { bookService.deleteBook(bookId); return SUCCESS; } ... }
在applicationContext.xml中添加Book的相关配置,如下面代码所示:
... ......
name/dohkoos/book/model/Book.hbm.xml
在Struts 2配置文件struts.xml中include文件book.xml。以下是book.xml文件的内容:
/books/list.jsp /books/show.jsp /books/form.jsp listBook
视图文件show.jsp作为显示单本图书的信息,代码很简单,就不放出来了,只把list.jsp和form.jsp的代码列出来。
list.jsp用来显示所有的图书:
Listing books
Title | Description | Image url | Price | Author | ISBN | Publisher | Action |
---|---|---|---|---|---|---|---|
Show Edit Destroy |
当用户新建或者更新图书时,视图form.jsp就上场了。因为被两个逻辑使用,显示的标题等信息也不相同,所以需要判断图书的id是否存在,如果不存在的话即是新建业务,否则就是更新操作。
Editing book
Add book
最后还要完成图书模型的服务端校验。Struts 2的校验文件有两种格式:ActionName-validation.xml和ActionName-alias-validation.xml。第一种会对该Action中的每个方法进行校验,不符合只对saveOrUpdate校验的要求。在BookAction.java同目录下创建BookAction-saveOrUpdateBook-validation.xml文件:
true
当然,还要在Book.java所在的位置创建Book-validation.xml文件:
... Price is required Price is required 0.01 Price should be at least 0.01
看着BookAction-saveOrUpdateBook-validation.xml这么长的文件名是不是有点无语:)没关系,BookAction-validation.xml配置文件还是可以用的,只要在BookAction.java中那些不需要进行校验的方法上添加@SkipValidation;也可以在action配置中启用validation.excludeMethods参数:
list,show,input,delete ...
代码下载:https://github.com/dohkoos/JBookShelf
原文地址:实现图书的增删查改(CRUD), 感谢原作者分享。
推荐阅读
-
python实现的MySQL增删改查操作实例小结
-
javascript实现信息增删改查的方法示例代码
-
JDBC 实现通用的增删改查基础类方法
-
C#基于DBContext(EF)实现通用增删改查的REST方法实例
-
Windows 8 Metro用C#连接SQLite及创建数据库,数据表的增删改查的实现
-
JDBC 实现通用的增删改查基础类方法
-
C#基于DBContext(EF)实现通用增删改查的REST方法实例
-
Windows 8 Metro用C#连接SQLite及创建数据库,数据表的增删改查的实现
-
C#如何实现对sql server数据库的增删改查
-
python操作小程序云数据库实现简单的增删改查功能