Dao层代码编写:
整个项目由 Dao、Services、Web 三层组成, Dao 层主要通过 hibernate 来操作数据库, Service 层主要体现了业务、事务的处理, Web 层由 struts 来控制。整个项目的控制交由 spring 管理。
现在的这个小项目除了完成基本的添删改查,还有一个简单的分页功能。这个分页功能不仅前台分页,而且在后台数据库也进行了分页处理。
首先写好 pojo 的代码:
在 com.game.products.model 中新建 products.hbm.xml 类,代码如下:
注意这里的 ID 不是数据库自动生成的,而是根据需要由程序生成,一般项目中的主键 ID 都是采取这种方式。
xm
- < 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();
- }