Baseservice of Java EE Project extraction and its matters needing attention

Source: Internet
Author: User

Note: The challenge is that the service needs to be injected, so how do you inject it?

1. Writing Baseservice

 PackageCn.buaa.core.service;Importjava.io.Serializable;Importjava.util.List; Public InterfaceBaseservice<t> {    //New     Public voidSave (T entity); //Update     Public voidUpdate (T entity); //Delete by ID     Public voidDelete (Serializable ID); //Query by ID     PublicT Findobjectbyid (Serializable ID); //Query List     PublicList<t>findobjects ();}

2. Implement the interface

 PackageCn.buaa.core.service.impl;Importjava.io.Serializable;Importjava.util.List;ImportCn.buaa.core.dao.BaseDao;ImportCn.buaa.core.service.BaseService; Public classBaseserviceimpl<t>ImplementsBaseservice<t> {        PrivateBasedao<t>Basedao;  Public voidSetbasedao (basedao<t>Basedao) {         This. Basedao =Basedao; } @Override Public voidSave (T entity) {Basedao.save (entity); } @Override Public voidUpdate (T entity) {basedao.update (entity); } @Override Public voidDelete (Serializable id) {basedao.delete (ID); } @Override PublicT Findobjectbyid (Serializable id) {returnBasedao.findobjectbyid (ID); } @Override PublicList<t>findobjects () {returnbasedao.findobjects (); }}

3, how to use it, as below, set the DAO in the parent class

 PackageCn.buaa.nsfw.info.service.impl;Importjava.io.Serializable;Importjava.util.List;ImportJavax.annotation.Resource;Importorg.springframework.orm.hibernate3.HibernateTemplate;ImportOrg.springframework.stereotype.Service;ImportCn.buaa.core.service.impl.BaseServiceImpl;ImportCn.buaa.nsfw.info.dao.InfoDao;ImportCn.buaa.nsfw.info.entity.Info;ImportCn.buaa.nsfw.info.service.InfoService; @Service ("Infoservice") Public classInfoserviceimplextendsBaseserviceimpl<info>ImplementsInfoservice {//injected in this way, in order to set the DAO in the parent classPrivateInfodao Infodao; @Resource Public voidSetinfodao (Infodao Infodao) {Super. Setbasedao (Infodao);  This. Infodao =Infodao; }}

Baseservice of Java EE Project extraction and its matters needing attention

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.