您的位置:首页 > 编程语言 > Java开发

Struts2+Spring2+Hibernate3 web应用示例(二)

2008-05-18 10:08 225 查看
三、
建立数据持久化层


1、编写实体类Books及books.hbm.xml映射文件。

package com.sterning.books.model;

import java.util.Date;

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

四、
建立DAO层


DAO访问层负责封装底层的数据访问细节,不仅可以使概念清晰,而且可以提高开发效率。

1、建立DAO的接口类:BooksDao

package com.sterning.books.dao.iface;

import java.util.List;

import com.sterning.books.model.Books;

com.sterning.books.dao.iface.BooksDao.java

2、实现此接口的类文件,BooksMapDao

package com.sterning.books.dao.hibernate;

import java.sql.SQLException;

import java.util.Iterator;

import java.util.List;

import org.hibernate.HibernateException;

import org.hibernate.Query;

import org.hibernate.Session;

import org.springframework.orm.hibernate3.HibernateCallback;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sterning.books.dao.iface.BooksDao;

import com.sterning.books.model.Books;

import com.sterning.commons.PublicUtil;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: