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

importjava.util.Date;


publicclassBooks
{
//Fields
privateStringbookId;//编号
privateStringbookName;//书名
privateStringbookAuthor;//作者
privateStringbookPublish;//出版社
privateDatebookDate;//出版日期
privateStringbookIsbn;//ISBN
privateStringbookPage;//页数
privateStringbookPrice;//价格
privateStringbookContent;//内容提要

//Constructors

publicBooks()
{}

//Propertyaccessors


publicStringgetBookId()
{
returnbookId;
}


publicvoidsetBookId(StringbookId)
{
this.bookId=bookId;
}


publicStringgetBookName()
{
returnbookName;
}


publicvoidsetBookName(StringbookName)
{
this.bookName=bookName;
}


publicStringgetBookAuthor()
{
returnbookAuthor;
}


publicvoidsetBookAuthor(StringbookAuthor)
{
this.bookAuthor=bookAuthor;
}


publicStringgetBookContent()
{
returnbookContent;
}


publicvoidsetBookContent(StringbookContent)
{
this.bookContent=bookContent;
}


publicDategetBookDate()
{
returnbookDate;
}


publicvoidsetBookDate(DatebookDate)
{
this.bookDate=bookDate;
}


publicStringgetBookIsbn()
{
returnbookIsbn;
}


publicvoidsetBookIsbn(StringbookIsbn)
{
this.bookIsbn=bookIsbn;
}


publicStringgetBookPage()
{
returnbookPage;
}


publicvoidsetBookPage(StringbookPage)
{
this.bookPage=bookPage;
}


publicStringgetBookPrice()
{
returnbookPrice;
}


publicvoidsetBookPrice(StringbookPrice)
{
this.bookPrice=bookPrice;
}


publicStringgetBookPublish()
{
returnbookPublish;
}


publicvoidsetBookPublish(StringbookPublish)
{
this.bookPublish=bookPublish;
}
}
com.sterning.books.model.Books.java
接下来要把实体类Books的属性映射到books表,编写下面的books.hbm.xml文件:
<?xmlversion="1.0"?>
<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<classname="com.sterning.books.model.Books"table="books">
<idname="bookId"type="string">
<columnname="book_id"length="5"/>
<generatorclass="assigned"/>
</id>
<propertyname="bookName"type="string">
<columnname="book_name"length="100"/>
</property>
<propertyname="bookAuthor"type="string">
<columnname="book_author"length="100"/>
</property>
<propertyname="bookPublish"type="string">
<columnname="book_publish"length="100"/>
</property>
<propertyname="bookDate"type="java.sql.Timestamp">
<columnname="book_date"length="7"/>
</property>
<propertyname="bookIsbn"type="string">
<columnname="book_isbn"length="20"/>
</property>
<propertyname="bookPage"type="string">
<columnname="book_page"length="11"/>
</property>
<propertyname="bookPrice"type="string">
<columnname="book_price"length="4"/>
</property>
<propertyname="bookContent"type="string">
<columnname="book_content"length="100"/>
</property>
</class>
</hibernate-mapping>
com.sterning.books.model.books.hbm.xml
2、hibernate.cfg.xml配置文件如下:(注意它的位置在scr/hibernate.cfg.xml)
<?xmlversion="1.0"encoding="ISO-8859-1"?>
<!DOCTYPEhibernate-configurationPUBLIC
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<propertyname="show_sql">true</property>

<mappingresource="com/sterning/books/model/books.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
Com.sterning.bean.hibernate.hibernate.cfg.xml
四、 建立DAO层
DAO访问层负责封装底层的数据访问细节,不仅可以使概念清晰,而且可以提高开发效率。
1、建立DAO的接口类:BooksDao
packagecom.sterning.books.dao.iface;

importjava.util.List;

importcom.sterning.books.model.Books;


publicinterfaceBooksDao
{
ListgetAll();//获得所有记录
ListgetBooks(intpageSize,intstartRow);//获得所有记录
intgetRows();//获得总行数
intgetRows(Stringfieldname,Stringvalue);//获得总行数
ListqueryBooks(Stringfieldname,Stringvalue);//根据条件查询
ListgetBooks(Stringfieldname,Stringvalue,intpageSize,intstartRow);//根据条件查询
BooksgetBook(StringbookId);//根据ID获得记录
StringgetMaxID();//获得最大ID值
voidaddBook(Booksbook);//添加记录
voidupdateBook(Booksbook);//修改记录
voiddeleteBook(Booksbook);//删除记录
}
com.sterning.books.dao.iface.BooksDao.java
2、实现此接口的类文件,BooksMapDao
packagecom.sterning.books.dao.hibernate;

importjava.sql.SQLException;
importjava.util.Iterator;
importjava.util.List;

importorg.hibernate.HibernateException;
importorg.hibernate.Query;
importorg.hibernate.Session;
importorg.springframework.orm.hibernate3.HibernateCallback;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;

importcom.sterning.books.dao.iface.BooksDao;
importcom.sterning.books.model.Books;
importcom.sterning.commons.PublicUtil;



/***//**
*@authorcwf
*
*/

publicclassBooksMapDaoextendsHibernateDaoSupportimplementsBooksDao
{


publicBooksMapDao()
{}


/***//**
*函数说明:添加信息
*参数说明:对象
*返回值:
*/

publicvoidaddBook(Booksbook)
{
this.getHibernateTemplate().save(book);
}


/***//**
*函数说明:删除信息
*参数说明:对象
*返回值:
*/

publicvoiddeleteBook(Booksbook)
{
this.getHibernateTemplate().delete(book);
}


/***//**
*函数说明:获得所有的信息
*参数说明:
*返回值:信息的集合
*/

publicListgetAll()
{
Stringsql="FROMBooksORDERBYbookName";
returnthis.getHibernateTemplate().find(sql);
}


/***//**
*函数说明:获得总行数
*参数说明:
*返回值:总行数
*/

publicintgetRows()
{
Stringsql="FROMBooksORDERBYbookName";
Listlist=this.getHibernateTemplate().find(sql);
returnlist.size();
}


/***//**
*函数说明:获得所有的信息
*参数说明:
*返回值:信息的集合
*/

publicListgetBooks(intpageSize,intstartRow)throwsHibernateException
{
finalintpageSize1=pageSize;
finalintstartRow1=startRow;

returnthis.getHibernateTemplate().executeFind(newHibernateCallback()
{


publicListdoInHibernate(Sessionsession)throwsHibernateException,SQLException
{
//TODO自动生成方法存根
Queryquery=session.createQuery("FROMBooksORDERBYbookName");
query.setFirstResult(startRow1);
query.setMaxResults(pageSize1);
returnquery.list();
}
});
}


/***//**
*函数说明:获得一条的信息
*参数说明:ID
*返回值:对象
*/

publicBooksgetBook(StringbookId)
{
return(Books)this.getHibernateTemplate().get(Books.class,bookId);
}


/***//**
*函数说明:获得最大ID
*参数说明:
*返回值:最大ID
*/

publicStringgetMaxID()
{
Stringdate=PublicUtil.getStrNowDate();
Stringsql="SELECTMAX(bookId)+1FROMBooks";
StringnoStr=null;
Listll=(List)this.getHibernateTemplate().find(sql);
Iteratoritr=ll.iterator();

if(itr.hasNext())
{
Objectnoint=itr.next();

if(noint==null)
{
noStr="1";

}else
{
noStr=noint.toString();
}
}


if(noStr.length()==1)
{
noStr="000"+noStr;

}elseif(noStr.length()==2)
{
noStr="00"+noStr;

}elseif(noStr.length()==3)
{
noStr="0"+noStr;

}else
{
noStr=noStr;
}
returnnoStr;
}


/***//**
*函数说明:修改信息
*参数说明:对象
*返回值:
*/

publicvoidupdateBook(Bookspd)
{
this.getHibernateTemplate().update(pd);
}


/***//**
*函数说明:查询信息
*参数说明:集合
*返回值:
*/

publicListqueryBooks(Stringfieldname,Stringvalue)
{
System.out.println("value:"+value);
Stringsql="FROMBookswhere"+fieldname+"like'%"+value+"%'"+"ORDERBYbookName";
returnthis.getHibernateTemplate().find(sql);
}


/***//**
*函数说明:获得总行数
*参数说明:
*返回值:总行数
*/

publicintgetRows(Stringfieldname,Stringvalue)
{
Stringsql="";
if(fieldname==null||fieldname.equals("")||fieldname==null||fieldname.equals(""))
sql="FROMBooksORDERBYbookName";
else
sql="FROMBookswhere"+fieldname+"like'%"+value+"%'"+"ORDERBYbookName";
Listlist=this.getHibernateTemplate().find(sql);
returnlist.size();
}


/***//**
*函数说明:查询信息
*参数说明:集合
*返回值:
*/

publicListgetBooks(Stringfieldname,Stringvalue,intpageSize,intstartRow)
{
finalintpageSize1=pageSize;
finalintstartRow1=startRow;
finalStringqueryName=fieldname;
finalStringqueryValue=value;
Stringsql="";

if(queryName==null||queryName.equals("")||queryValue==null||queryValue.equals(""))
sql="FROMBooksORDERBYbookName";
else
sql="FROMBookswhere"+fieldname+"like'%"+value+"%'"+"ORDERBYbookName";

finalStringsql1=sql;

returnthis.getHibernateTemplate().executeFind(newHibernateCallback()
{


publicListdoInHibernate(Sessionsession)throwsHibernateException,SQLException
{
//TODO自动生成方法存根
Queryquery=session.createQuery(sql1);
query.setFirstResult(startRow1);
query.setMaxResults(pageSize1);
returnquery.list();
}
});
}

}
分享到:
相关推荐
Struts2+Spring+Hibernate和Struts2+Spring+Ibatis是两种常见的Java Web应用程序集成框架,它们分别基于ORM框架Hibernate和轻量级数据访问框架Ibatis。这两种框架结合Spring,旨在提供一个强大的、可扩展的、易于...
Struts2+Spring2+Hibernate3 web应用示例
这个“博客Struts2+Spring2+Hibernate3 web应用示例”旨在展示如何将这三个框架有效地结合在一起,以实现一个完整的后端管理系统,比如书管理应用。 Struts2是Action导向的MVC框架,负责处理HTTP请求,提供控制器层...
Struts2、Spring3和Hibernate4是Java Web开发中的三个核心框架,它们分别负责MVC(Model-View-Controller)架构中的控制层、业务层和服务层。本示例结合这三个框架,提供了一种完整的三层架构实现,以提高开发效率和...
《轻量级Java EE企业应用实战(第4版)》这本书深入探讨了Struts 2、Spring 4和Hibernate这三大框架的整合开发,旨在帮助读者掌握如何在实际项目中高效构建Java EE应用。SSH(Struts 2、Spring、Hibernate)是Java ...
《轻量级Java EE企业应用实战:Struts2+Spring3+Hibernate整合开发(第3版)》介绍了Java EE领域的三个开源框架:Struts 2、Spring和Hibernate。其中Struts 2升级到2.2.1,Spring升级到3.0.5,Hibernate升级到了...
这个“Struts2+Hibernate+Spring基于单表的增删改查code”项目是一个典型的Java Web应用程序示例,用于教学初学者如何在实际开发中运用这三个框架来实现对数据库中单个表的操作。 **Struts2** 是一个MVC(Model-...
### Struts2+Spring3+Hibernate整合 #### 一、简介与背景 本文将详细介绍如何实现Struts2、Spring3和Hibernate三个框架的整合。这种整合通常被称为SSH(Struts2-Spring-Hibernate)框架集成,是Java Web开发中非常...
Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们的整合应用极大地提升了开发效率和项目的可维护性。在本示例中,我们将探讨如何将这三个框架集成在一起,实现一个完整的MVC(模型-视图-控制器)架构。 ...
应广大网友需求,自己抽了点时间写了一个Struts2.0+Spring2.0+Hibernate3.1的登录及增删改查的完整演示示例,以此奉献给大家,由于本人水平有限,有写的不好的地方还望大家多提宝贵意见,如果有不明白的地方请关注我的...
Struts2.3 + Spring3.2 + Hibernate4.1 + HTML5 + CSS3开发示例代码。 其中包括如下文件: 介绍文档 Struts2.3 + Spring3.2 + Hibernate4.1 + HTML5 + CSS3开发示例.docx Eclipse工程文件 SSH2.zip Zip包是Eclipse...