MySQL database creation script for books: CREATETABLEbooks (partition, titlevarchar (255) NOTNULL, descriptiontext, round (255), pricedecimal (255) NOTNULL, authorvarchar (255) NOTNULL, isbnvarchar) NOTNULL,
MySQL database creation script for books: 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,
Script for creating a MySQL database for a book:
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;
Main Code of 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; ...}
Add the collection variable books to the User class:
public class User { ... private Set
books = new HashSet
(); ...}
Configure one-to-multiple associations with books in User. hbm. xml:
Book. hbm. xml code:
The code in BookService. java is relatively simple. Because there is no complicated business logic, it is only responsible for connecting the two layers of Action and Dao.
The code in BookDao. java mainly reads, updates, and deletes books:
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 List
getBookList() { 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 coordinates the operation between book applications. Receives access requests and interacts with the model to display the appropriate view to the user:
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 List
bookList; 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; } ...}
Add the relevant Book configuration in applicationContext. xml, as shown in the following code:
...
...
name/dohkoos/book/model/Book.hbm.xml
...
Include the file book. xml in the Struts. xml configuration file of struts 2. The content of the book. xml file is as follows:
/books/list.jsp
/books/show.jsp
/books/form.jsp
listBook
The View File show. jsp is used to display the information of a single book. The code is very simple and will not be released. It only lists the code of list. jsp and form. jsp.
List. jsp is used to display all books:
Listing books
Title |
Description |
Image url |
Price |
Author |
ISBN |
Publisher |
Action |
|
|
|
|
|
|
|
Show Edit Destroy |
Add
When a user creates or updates a book, view form. jsp is on the stage. Because it is used by two logics and the displayed titles and other information are different, you need to determine whether the library id exists. If it does not exist, it is a new business; otherwise, it is an update operation.
Editing book
Add book
Show
|
Back
Finally, the server verification of the Library Model is also completed. Struts 2 validation file has two formats: ActionName-validation.xml and ActionName-alias-validation.xml. The first method verifies each method in the Action, and does not meet the requirements for saveOrUpdate verification only. Create a BookAction-saveOrUpdateBook-validation.xml file in the same directory as BookAction. java:
true
Of course, you also need to create the Book-validation.xml file at the location where Book. java is located:
Price is required
...
Price is required
0.01
Price should be at least 0.01
Look at the BookAction-saveOrUpdateBook-validation.xml so long file name is not a bit speechless :) it doesn't matter, the BookAction-validation.xml configuration file can still be used, as long as the BookAction. add @ SkipValidation to the methods in java that do not require verification. You can also enable validation in the action configuration. excludeMethods parameters:
list,show,input,delete
...
Code download: https://github.com/dohkoos/JBookShelf
Original article address: add, query, modify, and delete a book (CRUD). Thank you for sharing the original article.