`
kong0itey
  • 浏览: 298742 次
社区版块
存档分类
最新评论

struts+spring+hibernate的web应用<二> Dao层代码编写

    博客分类:
  • java
 
阅读更多

前一篇文章 (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 包中新建继承HibernateDaoSupportProductsMapDao 类,并实现了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 层代码。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics