图书的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 Set<book> books = new HashSet<book>(); ... } </book></book>
在User.hbm.xml中配置和图书的一对多关联:
<!-- Bidirectional one-to-many association to Book --> <set name="books" inverse="true"> <key column="user_id" not-null="true"></key> <one-to-many class="name.dohkoos.book.model.Book"></one-to-many> </set>
图书的表映射文件Book.hbm.xml代码:
<hibernate-mapping> <class name="name.dohkoos.book.model.Book" table="books"> <id name="id"> <generator class="increment"></generator> </id> <property name="title" not-null="true"></property> <property name="description"></property> <property name="image_url"></property> <property name="price" not-null="true"></property> <property name="author" not-null="true"></property> <property name="isbn" not-null="true"></property> <property name="publisher" not-null="true"></property> <many-to-one name="user" column="user_id" not-null="true"></many-to-one> </class> </hibernate-mapping>
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 List<book> getBookList() { Session session = sessionFactory.openSession(); try { Query query = session.createQuery("from Book b"); List<book> 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(); } } } </book></book>
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 List<book> 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; } ... } </book>
在applicationContext.xml中添加Book的相关配置,如下面代码所示:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> ... <property name="mappingResources"> <list> ... <value>name/dohkoos/book/model/Book.hbm.xml</value> </list> </property> </bean> ... <!-- book --> <bean id="bookDao" class="name.dohkoos.book.dao.BookDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="bookService" class="name.dohkoos.book.service.BookService"> <property name="bookDao" ref="bookDao"></property> </bean> <bean id="bookAction" class="name.dohkoos.book.action.BookAction" scope="prototype"> <property name="bookService" ref="bookService"></property> </bean>
在Struts 2配置文件struts.xml中include文件book.xml。以下是book.xml文件的内容:
<?xml version="1.0" encoding="UTF-8"?> <struts> <package name="book" extends="struts-default"> <action name="*Book" method="{1}" class="bookAction"> <result name="list">/books/list.jsp</result> <result name="show">/books/show.jsp</result> <result name="input">/books/form.jsp</result> <result type="redirectAction">listBook</result> </action> </package> </struts>
视图文件show.jsp作为显示单本图书的信息,代码很简单,就不放出来了,只把list.jsp和form.jsp的代码列出来。
list.jsp用来显示所有的图书:
<h1>Listing books</h1>
Title | Description | Image url | Price | Author | ISBN | Publisher | Action |
---|---|---|---|---|---|---|---|
Show Edit Destroy |
当用户新建或者更新图书时,视图form.jsp就上场了。因为被两个逻辑使用,显示的标题等信息也不相同,所以需要判断图书的id是否存在,如果不存在的话即是新建业务,否则就是更新操作。
<if test="book.id != null"> <h1>Editing book</h1> </if> <else> <h1>Add book</h1> </else> <form action="saveOrUpdateBook" method="post"> <div class="field"> <hidden name="book.id" value="%{book.id}"></hidden> </div> <div class="field"> <textfield name="book.title" label="Title"></textfield> </div> <div class="field"> <textarea name="book.description" label="Description" rows="5" cols="25"></textarea> </div> <div class="field"> <textfield name="book.image_url" label="Image URL"></textfield> </div> <div class="field"> <textfield name="book.price" label="Price"></textfield> </div> <div class="field"> <textfield name="book.author" label="Author"></textfield> </div> <div class="field"> <textfield name="book.isbn" label="ISBN"></textfield> </div> <div class="field"> <textfield name="book.publisher" label="Publisher"></textfield> </div> <div class="actions"> <if test="book.id != null"> <submit value="Update"></submit> </if> <else> <submit value="Save"></submit> </else> </div> </form> <if test="book.id != null"> <a href="http://codemany.com/" showbook.action>Show</a> | </if> <a action="listBook">Back</a>
最后还要完成图书模型的服务端校验。Struts 2的校验文件有两种格式:ActionName-validation.xml和ActionName-alias-validation.xml。第一种会对该Action中的每个方法进行校验,不符合只对saveOrUpdate校验的要求。在BookAction.java同目录下创建BookAction-saveOrUpdateBook-validation.xml文件:
<validators> <field name="book"> <field-validator type="visitor"> <param name="appendPrefix">true <message></message> </field-validator> </field> </validators>
当然,还要在Book.java所在的位置创建Book-validation.xml文件:
<validators> <field name="title"> <field-validator type="required"> <message>Price is required</message> </field-validator> </field> ... <field name="price"> <field-validator type="required"> <message>Price is required</message> </field-validator> <field-validator type="double"> <param name="minInclusive">0.01 <message>Price should be at least 0.01</message> </field-validator> </field> </validators>
看着BookAction-saveOrUpdateBook-validation.xml这么长的文件名是不是有点无语:)没关系,BookAction-validation.xml配置文件还是可以用的,只要在BookAction.java中那些不需要进行校验的方法上添加@SkipValidation;也可以在action配置中启用validation.excludeMethods参数:
<action name="*Book" method="{1}" class="bookAction"> <interceptor-ref name="defaultStack"> <param name="validation.excludeMethods">list,show,input,delete </interceptor-ref> ... </action>
代码下载:https://github.com/dohkoos/JBookShelf
原文地址:实现图书的增删查改(CRUD), 感谢原作者分享。