博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts+spring+hibernate的web应用<三> Service层代码编写
阅读量:4221 次
发布时间:2019-05-26

本文共 7286 字,大约阅读时间需要 24 分钟。

现在开始编写 Service 层代码:

在 com.game.products.services.iface 包中新建 ProductsService 接口,代码如下:

 

package  com.game.products.services.iface;
import  java.util.List;
import  com.game.products.model.Products;
public   interface  ProductsService 
{
     void  addProduct(Products pd); // 添加记录
     void  deleteProduct(Products pd); // 删除记录    
    List getProducts(); // 获得所有记录
     int  getRows();; // 获得总行数
    List getProducts( int  pageSize,  int  startRow) ; // 获得一段记录
    Products getProduct(String gameId); // 根据ID获得记录
    String getMaxID(); // 获得最大ID值
     void  updateProductd(Products pd); // 修改记录
    List queryProducts(String fieldname,String value); // 根据条件查询的所有记录
     int  getRows(String fieldname,String value); // 获得总行数
    List queryProducts(String fieldname,String value, int  pageSize,  int  startRow); // 根据条件查询的一段记录
}

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

package  com.game.products.services;
import  java.util.List;
import  com.game.products.dao.iface.ProductsDao;
import  com.game.products.model.Products;
import  com.game.products.services.iface.ProductsService;
public   class  ProductsServiceImp  implements  ProductsService
{
     private  ProductsDao productsDao;
    
     public  ProductsServiceImp()
{}
    
     /** */ /**
     * 函数说明:添加信息
     * 参数说明:对象 
     * 返回值:
      */
     public   void  addProduct(Products pd) 
{
        productsDao.addProduct(pd);
    }
     /** */ /**
     * 函数说明:删除信息
     * 参数说明: 对象
     * 返回值:
      */
     public   void  deleteProduct(Products pd) 
{
        productsDao.deleteProduct(pd);
    }
     /** */ /**
     * 函数说明:获得所有的信息
     * 参数说明: 
     * 返回值:信息的集合
      */
     public  List getProducts() 
{
         return  productsDao.getProducts();
    }
    
     /** */ /**
     * 函数说明:获得总行数
     * 参数说明: 
     * 返回值:总行数
      */
     public   int  getRows() 
{
         return  productsDao.getRows();
    }
    
     /** */ /**
     * 函数说明:获得一段信息
     * 参数说明: 
     * 返回值:信息的集合
      */
     public  List getProducts( int  pageSize,  int  startRow) 
{
         return  productsDao.getProducts(pageSize, startRow);
    }
     /** */ /**
     * 函数说明:获得一条的信息
     * 参数说明: ID
     * 返回值:对象
      */
     public  Products getProduct(String gameId) 
{
         return  productsDao.getProduct(gameId);
    }
     /** */ /**
     * 函数说明:获得最大ID
     * 参数说明: 
     * 返回值:最大ID
      */
     public  String getMaxID() 
{
         return  productsDao.getMaxID();
    }
     /** */ /**
     * 函数说明:修改信息
     * 参数说明: 对象
     * 返回值:
      */
     public   void  updateProductd(Products pd) 
{
        productsDao.updateProductd(pd);
    }
     /** */ /**
     * 函数说明:查询信息
     * 参数说明: 集合
     * 返回值:
      */
     public  List queryProducts(String fieldname,String value) 
{
         return  productsDao.queryProducts(fieldname, value);
    }
    
     /** */ /**
     * 函数说明:获得总行数
     * 参数说明: 
     * 返回值:总行数
      */
     public   int  getRows(String fieldname,String value) 
{
         return  productsDao.getRows(fieldname, value);
    }
    
     /** */ /**
     * 函数说明:查询一段信息
     * 参数说明: 集合
     * 返回值:
      */
     public  List queryProducts(String fieldname,String value, int  pageSize,  int  startRow) 
{
         return  productsDao.queryProducts(fieldname, value,pageSize,startRow);
    }
     public  ProductsDao getProductsDao() 
{
         return  productsDao;
    }
     public   void  setProductsDao(ProductsDao productsDao) 
{
         this .productsDao  =  productsDao;
    }
}

 

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

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

Pager 类,封装了分页需要的属性,代码如下:

package  com.game.commons;
import  java.math. * ;
public   class  Pager 
{
     private   int  totalRows;  // 总行数
     private   int  pageSize  =   30 ;  // 每页显示的行数
     private   int  currentPage;  // 当前页号
     private   int  totalPages;  // 总页数
     private   int  startRow;  // 当前页在数据库中的起始行
    
     public  Pager() 
{
    }
    
     public  Pager( int  _totalRows) 
{
        totalRows  =  _totalRows;
        totalPages = totalRows / pageSize;
         int  mod = totalRows % pageSize;
         if (mod > 0 )
{
            totalPages ++ ;
        }
        currentPage  =   1 ;
        startRow  =   0 ;
    }
    
     public   int  getStartRow() 
{
         return  startRow;
    }
     public   int  getTotalPages() 
{
         return  totalPages;
    }
     public   int  getCurrentPage() 
{
         return  currentPage;
    }
     public   int  getPageSize() 
{
         return  pageSize;
    }
     public   void  setTotalRows( int  totalRows) 
{
         this .totalRows  =  totalRows;
    }
     public   void  setStartRow( int  startRow) 
{
         this .startRow  =  startRow;
    }
     public   void  setTotalPages( int  totalPages) 
{
         this .totalPages  =  totalPages;
    }
     public   void  setCurrentPage( int  currentPage) 
{
         this .currentPage  =  currentPage;
    }
     public   void  setPageSize( int  pageSize) 
{
         this .pageSize  =  pageSize;
    }
     public   int  getTotalRows() 
{
         return  totalRows;
    }
     public   void  first() 
{
        currentPage  =   1 ;
        startRow  =   0 ;
    }
     public   void  previous() 
{
         if  (currentPage  ==   1 ) 
{
             return ;
        }
        currentPage -- ;
        startRow  =  (currentPage  -   1 )  *  pageSize;
    }
     public   void  next() 
{
         if  (currentPage  <  totalPages) 
{
            currentPage ++ ;
        }
        startRow  =  (currentPage  -   1 )  *  pageSize;
    }
     public   void  last() 
{
        currentPage  =  totalPages;
        startRow  =  (currentPage  -   1 )  *  pageSize;
    }
     public   void  refresh( int  _currentPage) 
{
        currentPage  =  _currentPage;
         if  (currentPage  >  totalPages) 
{
            last();
        }
    }
}

 

PagerService 类,主要有个 getPager 方法返回 Pager 类。代码如下:

package  com.game.commons;
public   class  PagerService 
{
     public  Pager getPager(String currentPage,String pagerMethod, int  totalRows) 
{
         //     定义pager对象,用于传到页面
        Pager pager  =   new  Pager(totalRows);
         //     如果当前页号为空,表示为首次查询该页
         //     如果不为空,则刷新pager对象,输入当前页号等信息
         if  (currentPage  !=   null ) 
{
            pager.refresh(Integer.parseInt(currentPage));
        }
         //     获取当前执行的方法,首页,前一页,后一页,尾页。
         if  (pagerMethod  !=   null ) 
{
             if  (pagerMethod.equals( " first " )) 
{
                pager.first();
            }   else   if  (pagerMethod.equals( " previous " )) 
{
                pager.previous();
            }   else   if  (pagerMethod.equals( " next " )) 
{
                pager.next();
            }   else   if  (pagerMethod.equals( " last " )) 
{
                pager.last();
            }
        }
         return  pager;
    }
}

 

 

这个分页方法比较简单,而且功能也齐全,许多页面级的开源 table 中分页很多也是基于这个原理,所以理解了这个分页,对其他各种分页技术的理解也就迎刃而解了。

 

服务层的代码就这些了,接下来就可以写 spring 的配置文件来用 spring 管理这些 Dao 和 Service 了。

在 spring-context 包中新建 applicationContext.xml 。配置的写法如下:

<? xml version="1.0" encoding="ISO-8859-1" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
 
< beans >
     <!--  dataSource config  -->
     < bean  id ="dataSource"  class ="org.springframework.jndi.JndiObjectFactoryBean" >
         < property  name ="jndiName" >
             < value > java:comp/env/jdbc/game </ value >
         </ property >
     </ bean >
<!--  SessionFactory  -->
     < bean  id ="sessionFactory"
        class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
         < property  name ="dataSource" >
             < ref  bean ="dataSource" />
         </ property >
         < property  name ="configLocation" >
             < value > classpath:com\game\bean\hibernate\hibernate.cfg.xml </ value >
         </ property >         
     </ bean >
    
     <!--  TransactionManager   -->
     < bean  id ="transactionManager"
        class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
         < property  name ="sessionFactory" >
             < ref  local ="sessionFactory" />
         </ property >
     </ bean >
<!--  DAO  -->
     < bean  id ="productsDao"  class ="com.game.products.dao.hibernate.ProductsMapDao" >
         < property  name ="sessionFactory" >
             < ref  bean ="sessionFactory" />
         </ property >
     </ bean >
    
     <!--  Services  -->
      < bean  id ="productsService"  class ="com.game.products.services.ProductsServiceImp" >
         < property  name ="productsDao" >
             < ref  bean ="productsDao" />
         </ property >
     </ bean >  
< bean  id ="pagerService"  class ="com.game.commons.PagerService" >
     </ bean >
</beans>

 

配置文件不难,主要是些 IOC 控制。数据库链接我采用的是数据源方式,需要在 tomcat 的conf文件夹下的 server.xml 中添加数据源,添加的数据如下:

< Context  path ="/game"  docBase ="D:\tomcat-5.5.20\webapps\game"  debug ="0"  reloadable ="true" >
         < Resource
             name ="jdbc/game"
            type ="javax.sql.DataSource"
            password =""
            driverClassName ="net.sourceforge.jtds.jdbc.Driver"
            maxIdle ="2"
            maxWait ="5000"
            username ="sa"
            url ="jdbc:jtds:sqlserver://127.0.0.1:16899/game"
            maxActive ="4" />
         </ Context >

 

这个数据源是针对 tomcat 5.5 以上版本的,以下版本的写法有所不同,不同之处可以用 google 搜索得知。这个数据源很简单,并没有过多的配置来优化系统,只是为了让项目更容易让人理解。需要注意都是,我的数据链接的JDBC包是jtds包,而不是普通的那个三个jar包。

转载地址:http://uclmi.baihongyu.com/

你可能感兴趣的文章
c++string函数详解
查看>>
VC++多线程编程
查看>>
C++内存分配秘籍—new,malloc,GlobalAlloc详解
查看>>
Code Review 代码审查 不完全整理
查看>>
结构体内存对齐详解
查看>>
关于C++兼容C,C++在linux程序设计API的调用等问题
查看>>
Linux下如何编译并运行C程序
查看>>
在linux下编译运行c语言程序
查看>>
C++/CLI,摘自百度的基本概念
查看>>
浅议C /CLI的gcnew关键字
查看>>
浅议C /CLI的gcnew关键字(2)
查看>>
CR, LF, CR/LF 回车 换行
查看>>
回车(CR)与换行(LF), '\r'和'\n'的区别
查看>>
对C标准中空白字符(空格、回车符(\r)、换行符(\n)、水平制表符(\t)、垂直制表符(\v)、换页符(\f))的理解
查看>>
ASCII码表_全_完整版
查看>>
DLL导出STL模板类和成员变量
查看>>
C++ STL 一般总结
查看>>
谷歌微软等科技巨头数据科学面试107道真题:你能答出多少?
查看>>
漫画赏析:Linux 内核到底长啥样
查看>>
Json解析的四种方法
查看>>