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

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

    博客分类:
  • Java
阅读更多

 Service层代码编写:

现在开始编写 Service 层代码:
在 com.game.products.services.iface 包中新建 ProductsService 接口,代码如下: 
  1. package  com.game.products.services.iface;   
  2.   
  3.  import  java.util.List;   
  4.  import  com.game.products.model.Products;   
  5.   
  6.  public   interface  ProductsService   {   
  7.      void  addProduct(Products pd); // 添加记录    
  8.       void  deleteProduct(Products pd); // 删除记录        
  9.      List getProducts(); // 获得所有记录    
  10.       int  getRows();; // 获得总行数    
  11.      List getProducts( int  pageSize,  int  startRow) ; // 获得一段记录    
  12.      Products getProduct(String gameId); // 根据ID获得记录    
  13.      String getMaxID(); // 获得最大ID值    
  14.       void  updateProductd(Products pd); // 修改记录    
  15.      List queryProducts(String fieldname,String value); // 根据条件查询的所有记录    
  16.       int  getRows(String fieldname,String value); // 获得总行数    
  17.      List queryProducts(String fieldname,String value, int  pageSize,  int  startRow); // 根据条件查询的一段记录    
  18.  }    
  19.     

在 com.game.products.services 包中新建 ProductsServiceImp 类,这个类实现了 ProductsService 接口,代码如下:   

  1.       
  2.  package  com.game.products.services;       
  3.       
  4.  import  java.util.List;       
  5.       
  6.  import  com.game.products.dao.iface.ProductsDao;       
  7.  import  com.game.products.model.Products;       
  8.  import  com.game.products.services.iface.ProductsService;       
  9.       
  10.  public   class  ProductsServiceImp  implements  ProductsService  {       
  11.      private  ProductsDao productsDao;       
  12.            
  13.      public  ProductsServiceImp()  {}        
  14.            
  15.      /** */ /**        
  16.      * 函数说明:添加信息       
  17.      * 参数说明:对象        
  18.      * 返回值:       
  19.       */        
  20.       public   void  addProduct(Products pd)   {       
  21.         productsDao.addProduct(pd);       
  22.     }        
  23.         
  24.       /** */ /**        
  25.      * 函数说明:删除信息       
  26.      * 参数说明: 对象       
  27.      * 返回值:       
  28.       */        
  29.       public   void  deleteProduct(Products pd)   {       
  30.         productsDao.deleteProduct(pd);       
  31.     }        
  32.         
  33.       /** */ /**        
  34.      * 函数说明:获得所有的信息       
  35.      * 参数说明:        
  36.      * 返回值:信息的集合       
  37.       */        
  38.       public  List getProducts()   {       
  39.          return  productsDao.getProducts();       
  40.     }        
  41.            
  42.      /** */ /**        
  43.      * 函数说明:获得总行数       
  44.      * 参数说明:        
  45.      * 返回值:总行数       
  46.       */        
  47.       public   int  getRows()   {       
  48.          return  productsDao.getRows();       
  49.     }        
  50.            
  51.      /** */ /**        
  52.      * 函数说明:获得一段信息       
  53.      * 参数说明:        
  54.      * 返回值:信息的集合       
  55.       */        
  56.       public  List getProducts( int  pageSize,  int  startRow)   {       
  57.          return  productsDao.getProducts(pageSize, startRow);       
  58.     }        
  59.         
  60.       /** */ /**        
  61.      * 函数说明:获得一条的信息       
  62.      * 参数说明: ID       
  63.      * 返回值:对象       
  64.       */        
  65.       public  Products getProduct(String gameId)   {       
  66.          return  productsDao.getProduct(gameId);       
  67.     }        
  68.         
  69.       /** */ /**        
  70.      * 函数说明:获得最大ID       
  71.      * 参数说明:        
  72.      * 返回值:最大ID       
  73.       */        
  74.       public  String getMaxID()   {       
  75.          return  productsDao.getMaxID();       
  76.     }        
  77.         
  78.       /** */ /**        
  79.      * 函数说明:修改信息       
  80.      * 参数说明: 对象       
  81.      * 返回值:       
  82.       */        
  83.       public   void  updateProductd(Products pd)   {       
  84.         productsDao.updateProductd(pd);       
  85.     }        
  86.         
  87.       /** */ /**        
  88.      * 函数说明:查询信息       
  89.      * 参数说明: 集合       
  90.      * 返回值:       
  91.       */        
  92.       public  List queryProducts(String fieldname,String value)   {       
  93.          return  productsDao.queryProducts(fieldname, value);       
  94.     }        
  95.            
  96.      /** */ /**        
  97.      * 函数说明:获得总行数       
  98.      * 参数说明:        
  99.      * 返回值:总行数       
  100.       */        
  101.       public   int  getRows(String fieldname,String value)   {       
  102.          return  productsDao.getRows(fieldname, value);       
  103.     }        
  104.            
  105.      /** */ /**        
  106.      * 函数说明:查询一段信息       
  107.      * 参数说明: 集合       
  108.      * 返回值:       
  109.       */        
  110.       public  List queryProducts(String fieldname,String value, int  pageSize,  int  startRow)   {       
  111.          return  productsDao.queryProducts(fieldname, value,pageSize,startRow);       
  112.     }        
  113.         
  114.       public  ProductsDao getProductsDao()   {       
  115.          return  productsDao;       
  116.     }        
  117.         
  118.       public   void  setProductsDao(ProductsDao productsDao)   {       
  119.          this .productsDao  =  productsDao;       
  120.     }        
  121.         
  122. }       


基本的业务层代码就这些了。因为还有分页的业务,所以接下来编写分页的代码。

分页是个公共的类,所以放在 com.game.commons 中。

Pager 类,封装了分页需要的属性,代码如下:
  1. package  com.game.commons;   
  2.   
  3.  import  java.math. * ;   
  4.   
  5.  public   class  Pager   {   
  6.      private   int  totalRows;  // 总行数    
  7.       private   int  pageSize  =   30 ;  // 每页显示的行数    
  8.       private   int  currentPage;  // 当前页号    
  9.       private   int  totalPages;  // 总页数    
  10.       private   int  startRow;  // 当前页在数据库中的起始行    
  11.         
  12.      public  Pager()   {   
  13.     }    
  14.        
  15.      public  Pager( int  _totalRows)   {   
  16.         totalRows  =  _totalRows;   
  17.         totalPages = totalRows / pageSize;   
  18.          int  mod = totalRows % pageSize;   
  19.          if (mod > 0 )  {   
  20.             totalPages ++ ;   
  21.         }    
  22.         currentPage  =   1 ;   
  23.         startRow  =   0 ;   
  24.     }    
  25.        
  26.      public   int  getStartRow()   {   
  27.          return  startRow;   
  28.     }    
  29.       public   int  getTotalPages()   {   
  30.          return  totalPages;   
  31.     }    
  32.       public   int  getCurrentPage()   {   
  33.          return  currentPage;   
  34.     }    
  35.       public   int  getPageSize()   {   
  36.          return  pageSize;   
  37.     }    
  38.       public   void  setTotalRows( int  totalRows)   {   
  39.          this .totalRows  =  totalRows;   
  40.     }    
  41.       public   void  setStartRow( int  startRow)   {   
  42.          this .startRow  =  startRow;   
  43.     }    
  44.       public   void  setTotalPages( int  totalPages)   {   
  45.          this .totalPages  =  totalPages;   
  46.     }    
  47.       public   void  setCurrentPage( int  currentPage)   {   
  48.          this .currentPage  =  currentPage;   
  49.     }    
  50.       public   void  setPageSize( int  pageSize)   {   
  51.          this .pageSize  =  pageSi, e.display='inline'Codehighlighter1_784_815_Closed_Text.style.display='inline';" src="http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top>      public   void  setStartRow( int  startRow)   {   
  52.          this .startRow  =  startRow;   
  53.     }    
  54.       public   void  setTotalPages( int  totalPages)   {   
  55.          this .totalPages  =  totalPages;   
  56.     }    
  57.       public   void  setCurrentPage( int  currentPage)   {   
  58.          this .currentPage  =  currentPage;   
  59.     }    
  60.       public   void  setPageSize( int  pageSize)   {   
  61.          this .pageSize  =  pageSize;   
  62.     }   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics