技术开发 频道

Struts2、Spring和Hibernate应用实例(一)



三、       建立数据持久化层
 
1、编写实体类Books及books.hbm.xml映射文件。

package com.sterning.books.model; import java.util.Date; public class Books { // Fields private String bookId;//编号 private String bookName;//书名 private String bookAuthor;//作者 private String bookPublish;//出版社 private Date bookDate;//出版日期 private String bookIsbn;//ISBN private String bookPage;//页数 private String bookPrice;//价格 private String bookContent;//内容提要 // Constructors public Books(){} // Property accessors public String getBookId() { return bookId; } public void setBookId(String bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getBookAuthor() { return bookAuthor; } public void setBookAuthor(String bookAuthor) { this.bookAuthor = bookAuthor; } public String getBookContent() { return bookContent; } public void setBookContent(String bookContent) { this.bookContent = bookContent; } public Date getBookDate() { return bookDate; } public void setBookDate(Date bookDate) { this.bookDate = bookDate; } public String getBookIsbn() { return bookIsbn; } public void setBookIsbn(String bookIsbn) { this.bookIsbn = bookIsbn; } public String getBookPage() { return bookPage; } public void setBookPage(String bookPage) { this.bookPage = bookPage; } public String getBookPrice() { return bookPrice; } public void setBookPrice(String bookPrice) { this.bookPrice = bookPrice; } public String getBookPublish() { return bookPublish; } public void setBookPublish(String bookPublish) { this.bookPublish = bookPublish; } }
com.sterning.books.model.Books.java
       接下来要把实体类Books的属性映射到books表,编写下面的books.hbm.xml文件:

<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.sterning.books.model.Books" table="books" > <id name="bookId" type="string"> <column name="book_id" length="5" /> <generator class="assigned" /> </id> <property name="bookName" type="string"> <column name="book_name" length="100" /> </property> <property name="bookAuthor" type="string"> <column name="book_author" length="100" /> </property> <property name="bookPublish" type="string"> <column name="book_publish" length="100" /> </property> <property name="bookDate" type="java.sql.Timestamp"> <column name="book_date" length="7" /> </property> <property name="bookIsbn" type="string"> <column name="book_isbn" length="20" /> </property> <property name="bookPage" type="string"> <column name="book_page" length="11" /> </property> <property name="bookPrice" type="string"> <column name="book_price" length="4" /> </property> <property name="bookContent" type="string"> <column name="book_content" length="100" /> </property> </class> </hibernate-mapping>
com.sterning.books.model.books.hbm.xml
2、hibernate.cfg.xml配置文件如下:(注意它的位置在scr/hibernate.cfg.xml

<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <mapping resource="com/sterning/books/model/books.hbm.xml"></mapping> </session-factory> </hibernate-configuration>

Com.sterning.bean.hibernate.hibernate.cfg.xml

 

 

0
相关文章