`
primer_2004
  • 浏览: 124464 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

struts+spring+hibernate的web应用(2.1)

    博客分类:
  • Java
阅读更多

 Dao层代码编写:

整个项目由 Dao、Services、Web 三层组成, Dao 层主要通过 hibernate 来操作数据库, Service 层主要体现了业务、事务的处理, Web 层由 struts 来控制。整个项目的控制交由 spring 管理。

现在的这个小项目除了完成基本的添删改查,还有一个简单的分页功能。这个分页功能不仅前台分页,而且在后台数据库也进行了分页处理。
 
首先写好 pojo 的代码:
 
    在 com.game.products.model 中新建 products.hbm.xml 类,代码如下:
xm
  1. < hibernate-mapping >    
  2.          < class  name ="com.game.products.model.Products"  table ="products"   >    
  3.                    < id  name ="gameId"  type ="string" >    
  4.                          < column  name ="game_id"  length ="5"   />    
  5.                          < generator  class ="assigned"   />    
  6.                    </id>  
  7.                    < property  name ="gameNameCn"  type ="string" >    
  8.                           < column  name ="game_name_cn"  length ="100"   />    
  9.                    < /property  >  
  10.                    < property  name ="gameNameEn"  type ="string" >    
  11.                           < column  name ="game_name_en"  length ="100"   />    
  12.                    < /property  >  
  13.                    < property  name ="gameCapacity"  type ="string" >    
  14.                           < column  name ="game_capacity"  length ="4"   />    
  15.                    < /property  >  
  16.                    < property  name ="gameVersion"  type ="string" >    
  17.                            < column  name ="game_version"  length ="4"   />    
  18.                     < /property  >  
  19.                     < property  name ="gameMedia"  type ="string" >    
  20.                            < column  name ="game_media"  length ="4"   />    
  21.                     < /property  >  
  22.                     < property  name ="gameCopyright"  type ="string" >    
  23.                            < column  name ="game_copyright"  length ="4"   />    
  24.                    < /property  >  
  25.                    < property  name ="gamePrice"  type ="string" >    
  26.                             < column  name ="game_price"  length ="4"   />    
  27.                    < /property  >  
  28.                    < property  name ="gameContent"  type ="string" >    
  29.                              < column  name ="game_content"  length ="100"   />    
  30.                    < /property  >  
  31.           < /class  >  
  32. < /hibernate-mapping >   

注意这里的 ID 不是数据库自动生成的,而是根据需要由程序生成,一般项目中的主键 ID 都是采取这种方式。
然后在这个包中再新建 Products 类,代码如下:
  1. package  com.game.products.model;   
  2.      public   class  Products   {   
  3.      // Fields     
  4.       private  String gameId; // 编号    
  5.       private  String gameNameCn; // 中文名称    
  6.       private  String gameNameEn; // 英文名称    
  7.       private  String gameCapacity; // 碟数    
  8.       private  String gameVersion; // 版本    
  9.       private  String gameMedia; // 介质    
  10.       private  String gameCopyright; // 版权    
  11.       private  String gamePrice; // 价格    
  12.       private  String gameContent; // 攻略   
  13.        
  14.      // Constructors    
  15.        public  Products()  {}    
  16.        
  17.      //  Property accessors    
  18.        public  String getGameCapacity()   {   
  19.          return  gameCapacity;   
  20.     }    
  21.     
  22.       public   void  setGameCapacity(String gameCapacity)   {   
  23.          this .gameCapacity  =  gameCapacity;   
  24.     }    
  25.     
  26.       public  String getGameId()   {   
  27.          return  gameId;   
  28.     }    
  29.     
  30.       public   void  setGameId(String gameId)   {   
  31.          this .gameId  =  gameId;   
  32.     }    
  33.     
  34.       public  String getGameNameCn()   {   
  35.          return  gameNameCn;   
  36.     }    
  37.     
  38.       public   void  setGameNameCn(String gameNameCn)   {   
  39.          this .gameNameCn  =  gameNameCn;   
  40.     }    
  41.     
  42.       public  String getGameNameEn()   {   
  43.          return  gameNameEn;   
  44.     }    
  45.     
  46.       public   void  setGameNameEn(String gameNameEn)   {   
  47.          this .gameNameEn  =  gameNameEn;   
  48.     }    
  49.     
  50.       public  String getGameVersion()   {   
  51.          return  gameVersion;   
  52.     }    
  53.     
  54.       public   void  setGameVersion(String gameVersion)   {   
  55.          this .gameVersion  =  gameVersion;   
  56.     }    
  57.     
  58.       public  String getGameMedia()   {   
  59.          return  gameMedia;   
  60.     }    
  61.     
  62.       public   void  setGameMedia(String gameMedia)   {   
  63.          this .gameMedia  =  gameMedia;   
  64.     }    
  65.     
  66.       public  String getGameCopyright()   {   
  67.          return  gameCopyright;   
  68.     }    
  69.     
  70.       public   void  setGameCopyright(String gameCopyright)   {   
  71.          this .gameCopyright  =  gameCopyright;   
  72.     }    
  73.     
  74.       public  String getGameContent()   {   
  75.          return  gameContent;   
  76.     }    
  77.     
  78.       public   void  setGameContent(String gameContent)   {   
  79.          this .gameContent  =  gameContent;   
  80.     }    
  81.     
  82.       public  String getGamePrice()   {   
  83.          return  gamePrice;   
  84.     }    
  85.     
  86.       public   void  setGamePrice(String gamePrice)   {   
  87.          this .gamePrice  =  gamePrice;   
  88.     }    
  89.   
  90. }   
 
需要注意的是,我这里都是采用了 String 类型,因为在项目中传递数据,用 String  类型最为方便,同时也便于代码的编写。只是在前台需要编写验证代码,免得有字符数据插入整数字段而造成数据库异常。
在 com.game.products.dao.iface 包中新建ProductsDao接口。代码如下所示:
 
  1. package  com.game.products.dao.iface;   
  2. import  java.util.List;   
  3. import  com.game.products.model.Products;   
  4. public   interface  ProductsDao   {   
  5.      List getProducts(); // 获得所有记录    
  6.      List getProducts( int  pageSize,  int  startRow); // 获得一段记录    
  7.      int  getRows(); // 获得总行数    
  8.      int  getRows(String fieldname,String value); // 获得总行数    
  9.      List queryProducts(String fieldname,String value); // 根据条件查询的所有记录    
  10.      List queryProducts(String fieldname,String value, int  pageSize, int startRow); //根据条件查询的一段记录
         
    Products getProduct(String gameId); // 根据ID获得记录    
  11.      String getMaxID(); // 获得最大ID值    
  12.      void  addProduct(Products pd); // 添加记录    
  13.      void  updateProductd(Products pd); // 修改记录    
  14.      void  deleteProduct(Products pd); // 删除记录        
  15.  }   

在com.game.products.dao.hibernate包中新建继承HibernateDaoSupport的ProductsMapDao类,并实现了ProductsDao接口。 代码如下:
  1. package  com.game.products.dao.hibernate;   
  2.   
  3.  import  java.sql.SQLException;   
  4.  import  java.util.Iterator;   
  5.  import  java.util.List;   
  6.   
  7.  import  org.hibernate.HibernateException;   
  8.  import  org.hibernate.Query;   
  9.  import  org.hibernate.Session;   
  10.  import  org.springframework.orm.hibernate3.HibernateCallback;   
  11.  import  org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  12.   
  13.  import  com.game.products.dao.iface.ProductsDao;   
  14.  import  com.game.products.model.Products;   
  15.   
  16.   
  17.  /** */ /**    
  18.  *  @author  cwf   
  19.  *   
  20.   */    
  21.  public class ProductsMapDao  extends  HibernateDaoSupport implements ProductsDao {   
  22.   
  23.      public  ProductsMapDao()  {}    
  24.     
  25.       /** */ /**    
  26.      * 函数说明:添加信息   
  27.      * 参数说明:对象    
  28.      * 返回值:   
  29.       */    
  30.       public   void  addProduct(Products pd)   {   
  31.          this .getHibernateTemplate().save(pd);   
  32.     }    
  33.     
  34.       /** */ /**    
  35.      * 函数说明:删除信息   
  36.      * 参数说明: 对象   
  37.      * 返回值:   
  38.       */    
  39.       public   void  deleteProduct(Products pd)   {   
  40.          this .getHibernateTemplate().delete(pd);   
  41.     }    
  42.     
  43.       /** */ /**    
  44.      * 函数说明:获得所有的信息   
  45.      * 参数说明:    
  46.      * 返回值:信息的集合   
  47.       */    
  48.       public  List getProducts()   {   
  49.         String sql = " FROM Products ORDER BY gameNameCn " ;   
  50.          return   this .getHibernateTemplate().find(sql);   
  51.     }    
  52.        
  53.      /** */ /**    
  54.      * 函数说明:获得总行数   
  55.      * 参数说明:    
  56.      * 返回值:总行数   
  57.       */    
  58.       public   int  getRows()   {   
  59.         String sql = " FROM Products ORDER BY gameNameCn " ;   
  60.         List list = this .getHibernateTemplate().find(sql);   
  61.          return  list.size();   
  62.     }    
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics