Hibernate

xiaoxiao2021-03-06  23

Note that the following code is handwritten, only a small part is tested: the Service class is universal, it is a bit difficult. This layer is closely related to Business, but in the database era, what is less than a few databases, then Just put these operations in the BASE's service. Of course, because of common, in order to let Service know what domain you do, you can't specify, so add a InitBaseService method. If a service There are multiple tables to be worried, and the initXXxxService method is increasing. It is recommended that service is a thick particle size. It should contain a DAO operation of multiple functions similar to the table. When developing software, you should not expand this service class, Here if you use the "Adapter" design tool to do, the effect may be good.

Java code:

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;

/ ** *

Title: *

Description: *

Copyright: Copyright (c) 2004 *

Company: * @ Author Duan Hongjie * @version 1.0 * / public class baseservices {private ibasedao based {super ();}

/ ** * add information object * @param object Object * / public void setObject (Object object) throws BaseServiceException {try {baseDAO.setObject (object);} catch (Exception ex) {throw new BaseServiceException ( "Error information stored object ! " EX.TOSTRING ());}}

/ ** * delete information 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 ("Error when deleting Object!" EX.TOSTRING ());}}

/ * ** extracted information 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 ( "error while according to ID information extracted object!" ex.toString ());} return object;} / ** * modification information object * @param object Object * / public void modifyObject (Object object) throws BaseServiceException {try { Basedao.ModifyObject (Object);} catch (exception ex) {throw new baseserviceException ("Modify Information Object When an error!" EX.TOSTRING ());}}

/ ** * get all of the information set * @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 ("Error takes all the information collection!" ex.Tostring ());} Return Objects;}

/ ** * Total collection records takes * @return int * / public int getObjectsCount () throws BaseServiceException {int count = 0; try {count = baseDAO.getObjectsCount ();} catch (Exception ex) {throw new BaseServiceException ( "take An error occurred when the total number of specified sectors recorded! " Ex.toTostring ());} return count;}

/ ** * take the specified area record set * @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 ("Error taking a specific collection!" EX.TOSTRING ());} Return Objects;} / ** * Total take the specified recording area set * @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 ("Error with the total number of specified reaches recorded collection!" ex.Tostring ());} return count;}

/ ** * * @return ibasedao * / public ibasedao getBasedao () {Return Basedao;

/ ** * * @Param Boardtreedao Iinfotreedao * / public void setbasedao (ibasedao basedao) {this.basedao = basedao;

/ ** * Set a feature service domain * @Param Service Class * / Public Void InitbaseService (Class Service) {basedao.init (service);}}

Test file

Java code:

package common.spring.service; import junit.framework *;. import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import oa.pojo *.;

/ ** *

Title: OA1.0 * *

Description: * *

Copyright: Copyright (c) 2004 * *

Company: * * @author segment HongJie * @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 () } / ** * Enter test * @throws exception * /

/ * Public void testSetObject () throws Exception {Cart c = new Cart (); c.setTitle ( "input service1111"); baseService.setObject (c); // assertEquals ( "return value", expectedReturn, actualReturn);} * /

/ * ** Total number of records query test * @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 " ");}}

转载请注明原文地址:https://www.9cbs.com/read-66202.html

New Post(0)