services层和controller层

services层

我的理解,services层是编写逻辑代码语句最多的一个层,非常重要,在实际的项目中,负责调用Dao层中的mybatis,在我的项目中它调用的是这两个文件

举例代码如下

javascript 复制代码
package com.example.sfdeliverysystem.service;
import com.example.sfdeliverysystem.dao.mybatis.StashMapper;
import com.example.sfdeliverysystem.model.StashPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service//加载service方法的包
@Slf4j//加载log方法的包
public class StashService {//创建一个方法
    private StashMapper stashMapper;//调用接口并创建StashMapper类型的对象,这个类是Dao层的接口里的
        /**
     * 新增Stash
     * @param stashPo
     */
    public int insertStash(StashPO stashPo) {   //调用接口中的insertStash方法,
    //这个是方法名,方法体在StashMapper.xml里边定义了
       try{//实际写项目的时候每个方法体里边都应该写try和
          //TODO 后续应该在mybatis中加上返回值,确定是否插入成功(1成功,0失败)
          stashMapper.insertStash(stashPo);//用stashMapper.来调用它体内的方法
          return 1;
       }catch (NullPointerException e){//如果出错,终止try的运行,进入catch分支运行方法体
          log.error("有部分属性为空,{0}",e);
          return 0;
        }
    };
    /**
     * 查询Stash,返回我查询的值,成功返回查询的值,失败就不用返回了
     * @return selectStash()
     */
    public  StashPO selectStash() {//定义返回值类型为StashPO,调用接口中的selectStash方法
        StashPO stashpo = new StashPO();//新建局部变量,为了承接selectStash方法的返回值(结果)
        try {
          stashpo = stashMapper.selectStash();//承接selectStash方法的返回值(结果)
        }catch (Exception e) {
            log.error("有部分属性查询不到,{0}", e); //有部分属性查询不到nullException
        }
        return stashpo;
    };
    /**
     * 更新Stash,成功返回1,失败返回0
     * @param stashPo
     * @return
     */
    public int updateStash(StashPO stashPo){
            try{
                stashMapper.updateStash(stashPo);
                return 1;
            }catch (Exception e) {
                log.error("有部分属性无法更新,{0}", e);
                return 0;
            }
    };
    /**
     * 删除Stash,成功返回1,失败返回0
     * @param cid
     * @return
     */
    public int deleteStash(String cid) {
        try{
            stashMapper.deleteStash(cid);
            return 1;
        }catch (Exception e) {
            log.error("有部分属性无法删除,{0}", e);
            return 0;
        }
    };
}

controller层

我的理解,controller层,是用来处理页面和services层的逻辑关系的一个层,下面由代码举例:

javascript 复制代码
package com.example.sfdeliverysystem.controller;
import com.example.sfdeliverysystem.dao.mybatis.StashMapper;
import com.example.sfdeliverysystem.model.StashPO;
import com.example.sfdeliverysystem.service.StashService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RequestMapping("/StashPO")//将请求和处理请求的控制器方法关联起来,建立映射关系,里边写实体类名
@RestController
//定义一个StashController类,里边写对应的增删改查的方法,方法体为调用services层中的方法
public class StashController {
    private StashService stashService;//新建两个局部变量,还是主要为了调用和承接返回值
    private int i;
@GetMapping("/{insert}")//绿色的是在url中为了让其可以被识别的路径关键字,自己设置的
public int insertStash(StashPO stashPo){//依旧是调用接口中的方法然后承接其返回值,然后输出本身方法体的返回值
    i = stashService.insertStash(stashPo);
    return i;
    };
    @GetMapping("/{select}")
    public  StashPO selectStash() {
        StashPO stashpo = new StashPO();
        try{
           stashpo = stashService.selectStash();
        }catch (Exception e) {
            log.error("有部分属性查询不到,{0}", e);
        }
        return stashpo;
    };
    @GetMapping("/{update}")
    public int updateStash(StashPO stashPo){
        i = stashService.updateStash(stashPo);
            return i;
        }
    @GetMapping("/{delete}")
    public int deleteStash(String cid) {
        i = stashService.deleteStash(cid);
            return i;
    };
};
相关推荐
卓怡学长1 分钟前
w192基于springboot“考研情报站”微信小程序设计与实现
java·spring boot·spring·微信小程序·intellij-idea
Dawson Zhu6 分钟前
大语言模型对齐与微调:原理解析与工程实践
人工智能·语言模型·架构·aigc·agi
wuminyu12 分钟前
JEP491中synchronized关键字引发的平台线程钉住解决方法简介
java·linux·c语言·jvm·c++
姜鱼问生27 分钟前
uptime-kuma 从 docker run 迁移到 compose:数据卷的坑
安全·架构
weixin_3077791331 分钟前
从“人操作仪器”到“Agent 自主发现”:OPL 金属材料自驱动实验室 MVP 技术论述
开发语言·人工智能·算法·架构
对讲机数码科普41 分钟前
危化厂区防爆专网通信建设实践:从合规框架到验收清单
运维·网络·架构
硅基手札1 小时前
ROS2 全面解析 - 从架构到源码
单片机·架构·机器人
ting94520001 小时前
深度拆解|1752vc Pitch Deck Analyzer 底层技术架构与核心算法原理
人工智能·算法·架构
早睡早起不秃头&1 小时前
【架构】- 移动机器人系统的插件化:让定位算法独立演进
算法·架构
anew___1 小时前
《从零手写操作系统 (20):ELF加载器——让OS读懂现代编译器》
java·服务器·前端