现在的这个小项目除了完成基本的添删改查,还有一个简单的分页功能。这个分页功能不仅前台分页,而且在后台数据库也进行了分页处理。
现在就来编写 Dao 层的代码。
首先写好 pojo 的代码:
在 com.game.products.model 中新建 products.hbm.xml 类,代码如下:
<! 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 类,代码如下:
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接口。
代码如下所示:
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接口。
代码如下:
import java.
.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 .
} ().save(pd);
/** */ /**
* 函数说明:删除信息
* 参数说明: 对象
* 返回值:
*/
public void deleteProduct(Products pd) {
this .
} ().delete(pd);
/** */ /**
* 函数说明: 的信息
* 参数说明:
* 返回值:信息的集合
*/
public List getProducts() {
String = " FROM Products ORDER BY gameNameCn " ;
return this .
} ().find( );
/** */ /**
* 函数说明:获得总行数
* 参数说明:
* 返回值:总行数
*/
public int getRows() {
String = " FROM Products ORDER BY gameNameCn " ;
List list = this .
().find( );return list.size();
}
/** */ /**
* 函数说明:获得一段记录信息
* 参数说明:
* 返回值:信息的集合
*/
public List getProducts( int pageSize, int startRow) throws HibernateException {
final int pageSize1 = pageSize;
final int startRow1 = startRow;
return this . ().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 . ().get(Products.class ,gameId);
}
/** */ /**
* 函数说明:获得最大ID
* 参数说明:
* 返回值:最大ID
*/
public String getMaxID() {
String = " SELECT MAX(gameId)+1 FROM Products " ;
String noStr = null ;
List ll = (List) this .
Iterator itr ().find( );= 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 .
} (). (pd);
/** */ /**
* 函数说明:查询的所有信息
* 参数说明: 集合
* 返回值:
*/
public List queryProducts(String fieldname,String value) {
System.out.println( " value: " + value);
String = " FROM Products where " + fieldname + " like '% " + value + " %' " + " ORDER BY gameNameCn " ;
return this .
} ().find( );
/** */ /**
* 函数说明:获得总行数
* 参数说明:
* 返回值:总行数
*/
public int getRows(String fieldname,String value) {
String = " FROM Products where " + fieldname + " like '% " + value + " %' " + " ORDER BY gameNameCn " ;
List list = this .
().find( );return list.size();
}
/** */ /**
* 函数说明:查询的一段信息
* 参数说明: 集合
* 返回值:
*/
public List queryProducts(String fieldname,String value, int pageSize, int startRow) {
final int pageSize1 = pageSize;
final int startRow1 = startRow;
final String = " FROM Products where " + fieldname + " like '% " + value + " %' " + " ORDER BY gameNameCn " ;
return this . ().executeFind(new HibernateCallback() {
public List doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(
query.setFirstResult(startRow1);
query.setMaxResults(pageSize1);
);return query.list();
}
} );
}
}
在com.game.bean.hibernate包中新建hibernate.cfg.xml,代码如下:
<! 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_ "> true </ property >
< mapping resource ="com/game/products/model/products.hbm.xml" ></ mapping >
</ session-factory >
</ hibernate-configuration >
至此,DAO层的代码已经编写完成。下一篇,将编写service层代码。