<o:p> </o:p>
现在的这个小项目除了完成基本的添删改查,还有一个简单的分页功能。这个分页功能不仅前台分页,而且在后台数据库也进行了分页处理。
<o:p> </o:p>
现在就来编写 Dao 层的代码。
首先写好 pojo 的代码:
在
com.game.products.model
中新建
products.hbm.xml
类,代码如下:
<o:p></o:p>
<! 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
类,代码如下:
<o:p></o:p>
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 类型最为方便,同时也便于代码的编写。只是在前台需要编写验证代码,免得有字符数据插入整数字段而造成数据库异常。
<o:p> </o:p>
在 com.game.products.dao.iface 包中新建 ProductsDao 接口。 <o:p></o:p>
代码如下所示: <o:p></o:p>
<o:p></o:p>