˵˵Ϊʲô±à³Ì¼Óservice²ã_JAVA_±à³Ì¿ª·¢_³ÌÐòÔ±¾ãÀÖ²¿

ÖйúÓÅÐãµÄ³ÌÐòÔ±ÍøÕ¾³ÌÐòԱƵµÀCXYCLUB¼¼ÊõµØͼ
ÈÈËÑ£º
¸ü¶à>>
 
ÄúËùÔÚµÄλÖ㺠³ÌÐòÔ±¾ãÀÖ²¿ > ±à³Ì¿ª·¢ > JAVA > ˵˵Ϊʲô±à³Ì¼Óservice²ã

˵˵Ϊʲô±à³Ì¼Óservice²ã

 2013/7/10 12:35:43  am_danger  ³ÌÐòÔ±¾ãÀÖ²¿  ÎÒÒªÆÀÂÛ(0)
  • ÕªÒª£º°¥£¬½ñÌìcodeviewµÄʱºò°¤ÅúÁË£¬Ô­ÒòºÜ¼òµ¥£¬¾ÍÊDZ¾È˵ıàÂë·ç¸ñ²»·ûºÏËûÃǹ«Ë¾µÄ½á¹¹·Ö²ã£¨±¾È˸ÕÀ´Õâ¸ö¹«Ë¾Ò»¸öÔ£©£¬ÏÂÃæ¾ßÌå˵һϾßÌåÔ­Òò¡£¹«Ë¾µÄ±àÂë·ç¸ñÊÇ·ÖΪËIJãµÄpo¡¢dao¡¢service¡¢control¡£poÊÇÊý¾Ý¿âÖбí¶ÔÓ¦µÄʵÌåÀ࣬Õâ¸öºÁÎÞÒÉÒ壬daoÊǶÔpoµÄÊý¾Ý¿â²Ù×÷½Ó¿Ú(¼´Êý¾Ý¿âµÄÔöɾ¸Ä²é)Õâ¸öҲûÓÐÒìÒ飬ÎÊÌâ³öÔÚÁËserviceºÍcontrol²ã¡£ÎÒ·¢ÏÖ¹«Ë¾Ð´µÄservice²ã¶¼ÊÇαservice£¬ÆäÖÐдµÄ½Ó¿ÚÊdzàÂãÂãµÄ¶Ôdao²ãµÄÕÕ°á
  • ±êÇ©£ºService ʲô Ϊ Ϊʲô ±à³Ì

     °¥£¬½ñÌìcode viewµÄʱºò°¤ÅúÁË£¬Ô­ÒòºÜ¼òµ¥£¬¾ÍÊDZ¾È˵ıàÂë·ç¸ñ²»·ûºÏËûÃǹ«Ë¾µÄ½á¹¹·Ö²ã£¨±¾È˸ÕÀ´Õâ¸ö¹«Ë¾Ò»¸öÔ£©£¬ÏÂÃæ¾ßÌå˵һϾßÌåÔ­Òò¡£
¹«Ë¾µÄ±àÂë·ç¸ñÊÇ·ÖΪËIJãµÄpo¡¢dao¡¢service¡¢control¡£poÊÇÊý¾Ý¿âÖбí¶ÔÓ¦µÄʵÌåÀ࣬Õâ¸öºÁÎÞÒÉÒ壬daoÊǶÔpoµÄÊý¾Ý¿â²Ù×÷½Ó¿Ú(¼´Êý¾Ý¿âµÄÔöɾ¸Ä²é)Õâ¸öҲû     ÓÐÒìÒ飬ÎÊÌâ³öÔÚÁËserviceºÍcontrol²ã¡£ÎÒ·¢ÏÖ¹«Ë¾Ð´µÄservice²ã¶¼ÊÇαservice£¬ÆäÖÐдµÄ½Ó¿ÚÊdzàÂãÂãµÄ¶Ôdao²ãµÄÕհᣬ¶øcontrol²ãÀïÃ澹Ȼ³öÏÖÁ˺ܶàÒµÎñÂß¼­´úÂ룬ÈçÏ£¨ÎÒ¾ÍÖ±½ÓÌù³öservice½Ó¿ÚµÄʵÏÖ´úÂëÁË£©
@Service("areaService")
public  class AreaServiceImpl implements IAreaService{

@Autowired
@Qualifier("areaDao")
  private IAreaDao areaDao;
   public  List<Area> getByTeaClass(Long teaId,Long classId){
        return areaDao.findByTeaClass(teaId,Long classId);
}
public void save(Area area){
         areaDao.save(area);
}
}
@Controller("areaAction")
public class AreaAction extends BaseAction {
@Autowired
@Qualifier("areaService")
private IAreaService areaService;
public ActionForward doAdd(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
           String areaName=request.getParameter("areaName");
           Area area = new Area();
           area.setAreaName(areaName);
          areaService.save(area);
          return mapping.findForward("success");
}
}
µ±È»ÎÒÖ»ÊÇΪÁËÑÝʾһÏ´úÂ룬ÒÔÉÏдµÄ±È½Ï¼òµ¥£¬ÆäÖеÄÒì³£´¦ÀíÒÔ¼°ÈÕÖ¾¶¼Ã»Ð´£¬ÒÔÉϾÍÊǹ«Ë¾µÄ´úÂëд·¨¡£ÎÒÈÏΪÒÔÉÏ´úÂëµÄservice²ãÍêȫûÓг䵱ÕæÕýservice²ãµÄÔðÈΣ¬Ö»ÊÇÕÕ°áÁËdao²ãµÄ·½·¨£¬È»ºóÔÙactionÖе÷Ó㬷´¶øÔÚcontrolÖнøÐÐÁ˸÷ÖÖÂß¼­µÄ´¦Àí£¨µ±È»ÔÚmvcÈý²ã¼Ü¹¹ÀÕâÑùдÊǺÁÎÞÒÉÒåµÄ£¬µ«ÊÇÏÖÔÚ¼ÓÈëÁËservice²ã£©¡£
¶øÎÒµÄservice²ãÊÇ´¦ÀíÒµÎñÂß¼­µÄ£¬²¢ÇÒ°üÀ¨¸÷ÖÖÂß¼­µÄ´¦Àí£¬²¢Çҳе£actionÖпØÖÆÁ÷³ÌµÄ²¿·Ö£¬ÈÃactionÖ»¹ØÐÄҪתµ½µÄλÖã¬ÏÂÃæÌù³öÎҵĴúÂë
@Service("areaService")
public  class AreaServiceImpl implements IAreaService{

@Autowired
@Qualifier("areaDao")
private IAreaDao areaDao;
public HttpServletRequest save(HttpServletRequest request){ 
           String areaName=request.getParameter("areaName");
          Area area = new Area();
          area.setAreaName(areaName);
          areaDao.save(area);
}
}
@Controller("areaAction")
public class AreaAction extends BaseAction {
@Autowired
@Qualifier("areaService")
private IAreaService areaService;
public ActionForward doAdd(ActionMapping mapping, ActionForm form,
           HttpServletRequest request, HttpServletResponse response) { 
            areaService.save(request);
           return mapping.findForward("success");
    }
}
ÒÔÉÏÊÇÎÒµÄд·¨£¬ÏÂÃæ˵һ˵ÎÒÕâÑùдµÄÏë·¨:
            Ê×ÏÈÀ´ËµÔÚMVC¿ò¼ÜÖÐcontroller³Ðµ£ÁËÁ½ÖÖÔðÈΣ¬Ò»¸öÊÇÂß¼­Á÷³ÌµÄ¿ØÖÆ£¬»¹ÓÐÒ»¸öÊÇÒ³ÃæµÄÌøת£¬Èç¹û°´ÕÕÉÏÃ湫˾µÄд·¨µÄ»°£¬ÊƱØcontrollerÖеĴúÂëÁ¿»áºÜ´ó£¬ÔÙ¼ÓÉÏV²ã£¬Õâ¾Íµ¼ÖÂÁËMVC¿ò¼ÜµÄÁ½Í·´ó£¬ÖмäϸµÄ½á¹û£¬¶øÉÏÃ湫˾µÄд·¨¼ÓÉÏserviceÒ»µãÓô¦¶¼Ã»ÓУ¬ÍêÈ«¿ÉÒÔ²»ÓÃдservice²ã¡£¶øµÚ¶þÖÖд·¨ÄØ£¬service½«ËùÓеÄÒµÎñÂß¼­µÄ»îÈ«¶¼À¿ÁËÏÂÀ´£¬°üÀ¨´ÓÇëÇóÖлñÈ¡Êý¾Ý£¬È»ºó½«Êý¾Ý·â×°³ÉʵÌåÀ࣬Ȼºó½«ÊµÌåÀà³Ö¾Ã»¯£¬ËùÓеÄÒµÎñÂß¼­¶¼ÔÚservice²ãÀïÃæȥʵÏÖ£¬ÈÃcontrollerÖ»¹ØÐÄÌøת£¬ÕâÒ²±ÜÃâÁËÁ½Í·´óÖмäϸµÄ¾ÖÃæ¡£ÆäʵµÚ¶þÖÖд·¨ÊÇΪcontroller·Öѹ£¬½«Ô­À´controllerÖÐÒµÎñÁ÷ת²¿·Ö¶¼Äõ½service²ãȥʵÏÖ¡£
ÎÒÈÏΪÕâÑù¼Óservice²ã²ÅÓбØÒª£¬·ñÔò¸ù±¾Ã»±ØÒª¼Óservice²ã£¬Ö»ÐèmvcÈý²ã¾Í¿ÉÒÔÁË¡£
          ÎÒÀí½âµÄserviceÓÐÁ½ÖÖ¹¦ÄÜ£¬Ò»¸öÊÇÍêÈ«´¦ÀíÒµÎñÂß¼­£¬»¹ÓÐÒ»¸öÊÇÏòÍâÌṩ½Ó¿Ú£¬Èç¹û°´ÕÕµÚÒ»ÖÖд·¨µÄ»°£¬ÎҸоõÕæû±ØÒª¼Óservice²ã¡£





·¢±íÆÀÂÛ
Óû§Ãû: ÄäÃû