注意,以下代码随手写的,只测试了一小部份:
Service类要通用,本身就有点难.这一层跟business密切相关. 不过在数据库时代,什么business都少不了几个数据库的操作,那就先把这几个操作放到base的Service中来.当然,因为通用,为了让service知道你操作什么域,也少不了要指定一下,所以增加了个initBaseService(Class service)方法.如果一个service中有多个表要操少,就不断增加initXXXXXService(Class service)方法.建议service做成粗粒度的.就是应该包含多个功能相近的表的DAO操作.
具体开发软件时,少不得扩充这个Service类,在这里如果用"适配器"设计模试来做,效果可能要好.
测试文件
Service类要通用,本身就有点难.这一层跟business密切相关. 不过在数据库时代,什么business都少不了几个数据库的操作,那就先把这几个操作放到base的Service中来.当然,因为通用,为了让service知道你操作什么域,也少不了要指定一下,所以增加了个initBaseService(Class service)方法.如果一个service中有多个表要操少,就不断增加initXXXXXService(Class service)方法.建议service做成粗粒度的.就是应该包含多个功能相近的表的DAO操作.
具体开发软件时,少不得扩充这个Service类,在这里如果用"适配器"设计模试来做,效果可能要好.
java代码: |
package common.spring.service; import java.util.List; import java.util.Iterator; import common.spring.dao.BaseDAOImpl; import common.spring.dao.IBaseDAO; import net.sf.hibernate.HibernateException; /** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author 段洪杰 * @version 1.0 */ public class BaseServiceSpringImpl implements IBaseService { private IBaseDAO baseDAO; public BaseServiceSpringImpl() { super(); } /** * 增加信息 object * @param object Object */ public void setObject(Object object) throws BaseServiceException { try { baseDAO.setObject(object); } catch (Exception ex) { throw new BaseServiceException("存object信息时出错! " + ex.toString()); } } /** * 删除信息 object * @param id String */ public void removeObjectById(String id) throws BaseServiceException { try { Object object = baseDAO.getObjectById(id); baseDAO.removeObject(object); } catch (Exception ex) { throw new BaseServiceException("删除object时出错! " + ex.toString()); } } /** * 取出信息 object * @param id String */ public Object getObjectById(String id) throws BaseServiceException { Object object=null; try { object = baseDAO.getObjectById(id); } catch (Exception ex) { throw new BaseServiceException("根据ID取出信息object时出错! " + ex.toString()); } return object; } /** * 修改信息 object * @param object Object */ public void modifyObject(Object object) throws BaseServiceException { try { baseDAO.modifyObject(object); } catch (Exception ex) { throw new BaseServiceException("修改信息object时出错! " + ex.toString()); } } /** * 取所有信息集合 * @return Iterator * @param position int, length int */ public Iterator getObjects(int position, int length) throws BaseServiceException { Iterator objects = null; try { objects = baseDAO.getObjects(position, length); } catch (Exception ex) { throw new BaseServiceException("取所有信息集合时出错! " + ex.toString()); } return objects; } /** * 取记录集合总数 * @return int */ public int getObjectsCount() throws BaseServiceException { int count = 0; try { count = baseDAO.getObjectsCount(); } catch (Exception ex) { throw new BaseServiceException(" 取指定版块记录集合总数时出错! " + ex.toString()); } return count; } /** * 取指定版块记录集合 * @return Iterator * @param boardId String, position int, length int */ public Iterator getObjects(String boardId, int position, int length) throws BaseServiceException { Iterator objects = null; try { objects = baseDAO.getObjects(boardId, position, length); } catch (Exception ex) { throw new BaseServiceException("取特定所有信息集合时出错! " + ex.toString()); } return objects; } /** * 取指定版块记录集合总数 * @return int * @param String boardId */ public int getObjectsCountByBoard(String boardId) throws BaseServiceException { int count = 0; try { count = baseDAO.getObjectsCountByBoard(boardId); } catch (Exception ex) { throw new BaseServiceException(" 取指定版块记录集合总数时出错! " + ex.toString()); } return count; } /** * * @return IBaseDAO */ public IBaseDAO getBaseDAO() { return baseDAO; } /** * * @param boardTreeDAO IInfoTreeDAO */ public void setBaseDAO(IBaseDAO baseDAO) { this.baseDAO = baseDAO; } /** * 设置一个特点的service域 * @param service Class */ public void initBaseService(Class service){ baseDAO.init(service); } } |
测试文件
java代码: |
package common.spring.service; import junit.framework.*; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import oa.pojo.*; /** * <p>Title: OA1.0</p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2004</p> * * <p>Company: </p> * * @author 段洪杰 * @version 1.0 */ public class TestBaseService extends TestCase { private IBaseService baseService = null; private ApplicationContext ac; protected void setUp() throws Exception { super.setUp(); ac = new FileSystemXmlApplicationContext("F:/jbproject/oa_1_0/web/WEB-INF/applicationContext-hibernate.xml"); baseService =(IBaseService) ac.getBean("baseService"); } protected void tearDown() throws Exception { super.tearDown(); } /** * 录入测试 * @throws Exception */ /* public void testSetObject() throws Exception { Cart c=new Cart(); c.setTitle("input service1111"); baseService.setObject(c); // assertEquals("return value", expectedReturn, actualReturn); }*/ /** * 查询总记录数测试 * @throws Exception */ public void testgetObjectCount() throws Exception { Cart c=new Cart(); baseService.initBaseService(c.getClass()); int count =baseService.getObjectsCount(); assertEquals("return value", "22", count+""); User u=new User(); baseService.initBaseService(u.getClass()); int count1 =baseService.getObjectsCount(); assertEquals("return value", "13", count1+""); } } |