前一篇文章
(struts+spring+hibernate的web應用<一>? 架構搭建)
讓我們打好了架子,接下來就來編寫代碼了。在編碼之前,我們需要先自行了解
strust,spring,hibernate
基礎知識,后面的文章將不會過多的介紹這些框架的基礎知識。整個項目由
Dao,Services,Web
三層組成,
Dao
層主要通過
hibernate
來操作數據庫,
Service
層主要體現了業務,事務的處理,
Web
層由
struts
來控制。整個項目的控制交由
spring
管理。
?
現在的這個小項目除了完成基本的添刪改查,還有一個簡單的分頁功能。這個分頁功能不僅前臺分頁,而且在后臺數據庫也進行了分頁處理。
?
現在就來編寫
Dao
層的代碼。
首先寫好
pojo
的代碼:
在
com.game.products.model
中新建
products.hbm.xml
類,代碼如下:
<?
xml?version="1.0"?encoding="GB2312"
?>
<!
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.game.products.model.Products"
?table
="products"
?
>
?????????
<
id?
name
="gameId"
?type
="string"
>
????????????
<
column?
name
="game_id"
?length
="5"
?
/>
????????????
<
generator?
class
="assigned"
?
/>
????????
</
id
>
????????
<
property?
name
="gameNameCn"
?type
="string"
>
????????????
<
column?
name
="game_name_cn"
?length
="100"
?
/>
????????
</
property
>
?????????
<
property?
name
="gameNameEn"
?type
="string"
>
????????????
<
column?
name
="game_name_en"
?length
="100"
?
/>
????????
</
property
>
????????
<
property?
name
="gameCapacity"
?type
="string"
>
????????????
<
column?
name
="game_capacity"
?length
="4"
?
/>
????????
</
property
>
?????????
<
property?
name
="gameVersion"
?type
="string"
>
????????????
<
column?
name
="game_version"
?length
="4"
?
/>
????????
</
property
>
??????????
<
property?
name
="gameMedia"
?type
="string"
>
????????????
<
column?
name
="game_media"
?length
="4"
?
/>
????????
</
property
>
????????
<
property?
name
="gameCopyright"
?type
="string"
>
????????????
<
column?
name
="game_copyright"
?length
="4"
?
/>
????????
</
property
>
????????
<
property?
name
="gamePrice"
?type
="string"
>
????????????
<
column?
name
="game_price"
?length
="4"
?
/>
????????
</
property
>
?
?????????
<
property?
name
="gameContent"
?type
="string"
>
????????????
<
column?
name
="game_content"
?length
="100"
?
/>
????????
</
property
>
?????
</
class
>
</
hibernate-mapping
>
?
注意這里的
ID
不是數據庫自動生成的,而是根據需要由程序生成,一般項目中的主鍵
ID
都是采取這種方式。
然后在這個包中再新建
Products
類,代碼如下:
package
?com.game.products.model;


public
?
class
?Products?
{
????
//
????Fields?
????
private
?String?gameId;
//
編號
????
private
?String?gameNameCn;
//
中文名稱
????
private
?String?gameNameEn;
//
英文名稱
????
private
?String?gameCapacity;
//
碟數
????
private
?String?gameVersion;
//
版本
????
private
?String?gameMedia;
//
介質
????
private
?String?gameCopyright;
//
版權
????
private
?String?gamePrice;
//
價格
????
private
?String?gameContent;
//
攻略
????
????
//
????Constructors
????
public
?Products()
{}
????
????
//
????Property?accessors
????
public
?String?getGameCapacity()?
{
????????
return
?gameCapacity;
????}
????
public
?
void
?setGameCapacity(String?gameCapacity)?
{
????????
this
.gameCapacity?
=
?gameCapacity;
????}
????
public
?String?getGameId()?
{
????????
return
?gameId;
????}
????
public
?
void
?setGameId(String?gameId)?
{
????????
this
.gameId?
=
?gameId;
????}
????
public
?String?getGameNameCn()?
{
????????
return
?gameNameCn;
????}
????
public
?
void
?setGameNameCn(String?gameNameCn)?
{
????????
this
.gameNameCn?
=
?gameNameCn;
????}
????
public
?String?getGameNameEn()?
{
????????
return
?gameNameEn;
????}
????
public
?
void
?setGameNameEn(String?gameNameEn)?
{
????????
this
.gameNameEn?
=
?gameNameEn;
????}
????
public
?String?getGameVersion()?
{
????????
return
?gameVersion;
????}
????
public
?
void
?setGameVersion(String?gameVersion)?
{
????????
this
.gameVersion?
=
?gameVersion;
????}
????
public
?String?getGameMedia()?
{
????????
return
?gameMedia;
????}
????
public
?
void
?setGameMedia(String?gameMedia)?
{
????????
this
.gameMedia?
=
?gameMedia;
????}
????
public
?String?getGameCopyright()?
{
????????
return
?gameCopyright;
????}
????
public
?
void
?setGameCopyright(String?gameCopyright)?
{
????????
this
.gameCopyright?
=
?gameCopyright;
????}
????
public
?String?getGameContent()?
{
????????
return
?gameContent;
????}
????
public
?
void
?setGameContent(String?gameContent)?
{
????????
this
.gameContent?
=
?gameContent;
????}
????
public
?String?getGamePrice()?
{
????????
return
?gamePrice;
????}
????
public
?
void
?setGamePrice(String?gamePrice)?
{
????????
this
.gamePrice?
=
?gamePrice;
????}
}
?
需要注意的是,我這里都是采用了
string
類型,因為在項目中傳遞數據,用
string
類型最為方便,同時也便于代碼的編寫。只是在前臺需要編寫驗證代碼,免得有字符數據插入整數字段而造成數據庫異常。
?
在
com.game.products.dao.iface
包中新建ProductsDao接口。
代碼如下所示:
package
?com.game.products.dao.iface;

import
?java.util.List;

import
?com.game.products.model.Products;


public
?
interface
?ProductsDao?
{
????List?getProducts();
//
獲得所有記錄
????List?getProducts(
int
?pageSize,?
int
?startRow);
//
獲得一段記錄
????
int
?getRows();
//
獲得總行數
????
int
?getRows(String?fieldname,String?value);
//
獲得總行數
????List?queryProducts(String?fieldname,String?value);
//
根據條件查詢的所有記錄
????List?queryProducts(String?fieldname,String?value,
int
?pageSize,?
int
?startRow);
//
根據條件查詢的一段記錄
????Products?getProduct(String?gameId);
//
根據ID獲得記錄
????String?getMaxID();
//
獲得最大ID值
????
void
?addProduct(Products?pd);
//
添加記錄
????
void
?updateProductd(Products?pd);
//
修改記錄
????
void
?deleteProduct(Products?pd);
//
刪除記錄????
}
?
在com.game.products.dao.hibernate包中新建繼承HibernateDaoSupport的ProductsMapDao類,并實現了ProductsDao接口。
代碼如下:
package
?com.game.products.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.game.products.dao.iface.ProductsDao;
import
?com.game.products.model.Products;



/**?*/
/**
?*?
@author
?cwf
?*
?
*/
public
?
class
?ProductsMapDao?
extends
?HibernateDaoSupport?
implements
?ProductsDao?
{


????
public
?ProductsMapDao()
{}
????
/**?*/
/**
?????*?函數說明:添加信息
?????*?參數說明:對象?
?????*?返回值:
?????
*/
????
public
?
void
?addProduct(Products?pd)?
{
????????
this
.getHibernateTemplate().save(pd);
????}
????
/**?*/
/**
?????*?函數說明:刪除信息
?????*?參數說明:?對象
?????*?返回值:
?????
*/
????
public
?
void
?deleteProduct(Products?pd)?
{
????????
this
.getHibernateTemplate().delete(pd);
????}
????
/**?*/
/**
?????*?函數說明:獲得所有的信息
?????*?參數說明:?
?????*?返回值:信息的集合
?????
*/
????
public
?List?getProducts()?
{
????????String?sql
=
"
FROM?Products?ORDER?BY?gameNameCn
"
;
????????
return
?
this
.getHibernateTemplate().find(sql);
????}
????

????
/**?*/
/**
?????*?函數說明:獲得總行數
?????*?參數說明:?
?????*?返回值:總行數
?????
*/
????
public
?
int
?getRows()?
{
????????String?sql
=
"
FROM?Products?ORDER?BY?gameNameCn
"
;
????????List?list
=
this
.getHibernateTemplate().find(sql);
????????
return
?list.size();
????}
????

????
/**?*/
/**
?????*?函數說明:獲得一段記錄信息
?????*?參數說明:?
?????*?返回值:信息的集合
?????
*/
????
public
?List?getProducts(
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?
{
????????????????Query?query
=
session.createQuery(
"
FROM?Products?ORDER?BY?gameNameCn
"
);
????????????????query.setFirstResult(startRow1);
????????????????query.setMaxResults(pageSize1);
????????????????
return
?query.list();
????????????}
????????}
);
????}
????
/**?*/
/**
?????*?函數說明:獲得一條的信息
?????*?參數說明:?ID
?????*?返回值:對象
?????
*/
????
public
?Products?getProduct(String?gameId)?
{
????????
return
?(Products)
this
.getHibernateTemplate().get(Products.
class
,gameId);
????}
????
/**?*/
/**
?????*?函數說明:獲得最大ID
?????*?參數說明:?
?????*?返回值:最大ID
?????
*/
????
public
?String?getMaxID()?
{
????????String?sql
=
"
SELECT?MAX(gameId)+1?FROM?Products??
"
;
????????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
?updateProductd(Products?pd)?
{
????????
this
.getHibernateTemplate().update(pd);
????}
????
/**?*/
/**
?????*?函數說明:查詢的所有信息
?????*?參數說明:?集合
?????*?返回值:
?????
*/
????
public
?List?queryProducts(String?fieldname,String?value)?
{
????????System.out.println(
"
value:?
"
+
value);
????????String?sql
=
"
FROM?Products?where?
"
+
fieldname
+
"
?like?'%
"
+
value
+
"
%'
"
+
"
ORDER?BY?gameNameCn
"
;
????????
return
?
this
.getHibernateTemplate().find(sql);
????}
????

????
/**?*/
/**
?????*?函數說明:獲得總行數
?????*?參數說明:?
?????*?返回值:總行數
?????
*/
????
public
?
int
?getRows(String?fieldname,String?value)?
{
????????String?sql
=
"
FROM?Products?where?
"
+
fieldname
+
"
?like?'%
"
+
value
+
"
%'
"
+
"
ORDER?BY?gameNameCn
"
;
????????List?list
=
this
.getHibernateTemplate().find(sql);
????????
return
?list.size();
????}
????

????
/**?*/
/**
?????*?函數說明:查詢的一段信息
?????*?參數說明:?集合
?????*?返回值:
?????
*/
????
public
?List?queryProducts(String?fieldname,String?value,
int
?pageSize,?
int
?startRow)?
{
????????
final
?
int
?pageSize1
=
pageSize;
????????
final
?
int
?startRow1
=
startRow;
????????
final
?String?sql
=
"
FROM?Products?where?
"
+
fieldname
+
"
?like?'%
"
+
value
+
"
%'
"
+
"
ORDER?BY?gameNameCn
"
;

????????
return
?
this
.getHibernateTemplate().executeFind(
new
?HibernateCallback()
{


????????????
public
?List?doInHibernate(Session?session)?
throws
?HibernateException,?SQLException?
{
????????????????Query?query
=
session.createQuery(sql);
????????????????query.setFirstResult(startRow1);
????????????????query.setMaxResults(pageSize1);
????????????????
return
?query.list();
????????????}
????????}
);
????}
}
?
在com.game.bean.hibernate包中新建hibernate.cfg.xml,代碼如下:
<?
xml?version="1.0"?encoding="GB2312"
?>
<!
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
="dialect"
>
org.hibernate.dialect.SQLServerDialect
</
property
>
????????
<
property?
name
="show_sql"
>
true
</
property
>
????????
<
mapping?
resource
="com/game/products/model/products.hbm.xml"
></
mapping
>
????
</
session-factory
>
</
hibernate-configuration
>
?
至此,DAO層的代碼已經編寫完成。下一篇,將編寫service層代碼。
struts+spring+hibernate
的
web
應用
<
三
> Service
層代碼編寫
posted on 2007-03-13 13:57
千山鳥飛絕 閱讀(19808)
評論(11) 編輯 收藏 所屬分類:
Web開發