三、 建立数据持久化层
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;
}
}
接下来要把实体类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>
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>
四、 建立DAO层
DAO访问层负责封装底层的数据访问细节,不仅可以使概念清晰,而且可以提高开发效率。
1、建立DAO的接口类:BooksDao
package com.sterning.books.dao.iface;
import java.util.List;
import com.sterning.books.model.Books;

public
interface BooksDao
{
List getAll();//获得所有记录
List getBooks(int pageSize,
int startRow);//获得所有记录
int getRows();//获得总行数
int getRows(String fieldname,String value);//获得总行数
List queryBooks(String fieldname,String value);//根据条件查询
List getBooks(String fieldname,String value,int pageSize,
int startRow);//根据条件查询
Books getBook(String bookId);//根据ID获得记录
String getMaxID();//获得最大ID值
void addBook(Books book);//添加记录
void updateBook(Books book);//修改记录
void deleteBook(Books book);//删除记录
}
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;


/**
*//**
*
@author cwf
*
*/
public
class BooksMapDao
extends HibernateDaoSupport
implements BooksDao
{

public BooksMapDao()
{}

/** *//**
* 函数说明:添加信息
* 参数说明:对象
* 返回值:
*/
public
void addBook(Books book)
{
this.getHibernateTemplate().save(book);
}

/** *//**
* 函数说明:删除信息
* 参数说明: 对象
* 返回值:
*/
public
void deleteBook(Books book)
{
this.getHibernateTemplate().delete(book);
}

/** *//**
* 函数说明:获得所有的信息
* 参数说明:
* 返回值:信息的集合
*/
public List getAll()
{
String sql="FROM Books ORDER BY bookName";
return
this.getHibernateTemplate().find(sql);
}

/** *//**
* 函数说明:获得总行数
* 参数说明:
* 返回值:总行数
*/
public
int getRows()
{
String sql="FROM Books ORDER BY bookName";
List list=this.getHibernateTemplate().find(sql);
return list.size();
}

/** *//**
* 函数说明:获得所有的信息
* 参数说明:
* 返回值:信息的集合
*/
public List getBooks(int pageSize,
int startRow)
throws HibernateException
{
final
int pageSize1=pageSize;
final
int startRow1=startRow;
return
this.getHibernateTemplate().executeFind(new HibernateCallback()
{

public List doInHibernate(Session session)
throws HibernateException, SQLException
{
// TODO 自动生成方法存根
Query query=session.createQuery("FROM
Books ORDER BY bookName");
query.setFirstResult(startRow1);
query.setMaxResults(pageSize1);
return query.list();
}
});
}

/** *//**
* 函数说明:获得一条的信息
* 参数说明: ID
* 返回值:对象
*/
public Books getBook(String bookId)
{
return (Books)this.getHibernateTemplate().get(Books.class,bookId);
}

/** *//**
* 函数说明:获得最大ID
* 参数说明:
* 返回值:最大ID
*/
public String getMaxID()
{
String date=PublicUtil.getStrNowDate();
String sql="SELECT MAX(bookId)+1 FROM Books
";
String noStr
= null;
List ll
= (List)
this.getHibernateTemplate().find(sql);
Iterator itr
= ll.iterator();
if (itr.hasNext())
{
Object noint
= itr.next();
if(noint
== null)
{
noStr
= "1";

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

if(noStr.length()==1)
{
noStr="000"+noStr;
}else
if(noStr.length()==2)
{
noStr="00"+noStr;
}else
if(noStr.length()==3)
{
noStr="0"+noStr;
}else
{
noStr=noStr;
}
return noStr;
}

/** *//**
* 函数说明:修改信息
* 参数说明: 对象
* 返回值:
*/
public
void updateBook(Books pd)
{
this.getHibernateTemplate().update(pd);
}

/** *//**
* 函数说明:查询信息
* 参数说明: 集合
* 返回值:
*/
public List queryBooks(String fieldname,String value)
{
System.out.println("value:
"+value);
String sql="FROM Books where
"+fieldname+" like '%"+value+"%'"+"ORDER
BY bookName";
return
this.getHibernateTemplate().find(sql);
}

/** *//**
* 函数说明:获得总行数
* 参数说明:
* 返回值:总行数
*/
public
int getRows(String fieldname,String value)
{
String sql="";
if(fieldname==null||fieldname.equals("")||fieldname==null||fieldname.equals(""))
sql="FROM Books ORDER BY bookName";
else
sql="FROM Books where
"+fieldname+" like '%"+value+"%'"+"ORDER
BY bookName";
List list=this.getHibernateTemplate().find(sql);
return list.size();
}

/** *//**
* 函数说明:查询信息
* 参数说明: 集合
* 返回值:
*/
public List getBooks(String fieldname,String value,int pageSize,
int startRow)
{
final
int pageSize1=pageSize;
final
int startRow1=startRow;
final String queryName=fieldname;
final String queryValue=value;
String sql="";
if(queryName==null||queryName.equals("")||queryValue==null||queryValue.equals(""))
sql="FROM Books ORDER BY bookName";
else
sql="FROM Books where
"+fieldname+" like '%"+value+"%'"+"ORDER
BY bookName";
final String sql1=sql;
return
this.getHibernateTemplate().executeFind(new HibernateCallback()
{

public List doInHibernate(Session session)
throws HibernateException, SQLException
{
// TODO 自动生成方法存根
Query query=session.createQuery(sql1);
query.setFirstResult(startRow1);
query.setMaxResults(pageSize1);
return query.list();
}
});
}
}

被折叠的 条评论
为什么被折叠?



