mapper层属于持久层的接口,负责数据访问层操作

service负责具体的业务逻辑操作

controller接受请求响应数据

| 层级         | 关键词         | 如果缺失的后果                                     |
| ---------- | ----------- | ------------------------------------------- |
| Mapper     | **数据访问**    | 业务代码被 SQL 绑架,换数据库=重写系统                      |
| Service    | **事务+业务编排** | Controller 变“大泥球”,复用、测试、维护统统崩溃              |
| Controller | **协议适配**    | Service 被迫关心 HTTP、gRPC、JSON、权限、限流,无法沉淀纯领域逻辑 |
 

形象解释,转自知乎:https://www.zhihu.com/question/431911268

一句话:  
Mapper 只对接数据库,Service 管事务和业务拼装,Controller 管接口协议;三层拆开才易维护、易测试、易替换。

如果去饭店吃饭,不可能找厨师点菜吧?

小工负责食材存取,洗菜切肉;厨师负责烹饪;跑堂负责接待食客并与后厨沟通。这就是精典的分层体现。

如果一个厨师既负责跑堂,又负责烹饪。那这个饭店的管理一定非常混乱吧。

  • 小工就是DAO,从食材库里(数据源)取出食材(原始数据),进行简单处理(数据对象化)。
  • 厨师就是Service,找到小工(DAO),获取各种半成品(对象化数据),加工成顾客需要的菜肴(最终数据)。
  • 跑堂就是Controller,负责接单(提交数据)上菜(响应数据),是顾客与后厨间的媒介(提供用户与后台程序的接口)。

各司其职(高内聚),轻松协作(低耦合),就是分层思想的目标。

@Autowired 是 Spring 的自动装配注解,作用:告诉 Spring 按类型把容器里的 Bean 注入到这个字段/构造器/方法里

1.首先实现Controller

DeptController.java
 

package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeptCotroller {
    @Autowired
    private DeptService deptService;//Service接口

    @RequestMapping("/dpts")//请求路径
    public Result list(){
        System.out.println("查询全部部门信息");
        List<Dept> deptlist = deptService.findAll();
        return Result.success(deptlist);
    }
}


2.DeptService.java

package com.itheima.service;


import com.itheima.pojo.Dept;

import java.util.List;

public interface DeptService {
    /**
     * 查询所有部门
     */

    List<Dept> findAll();
}

3.DeptServiceImpl.java

把 Service 拆成 接口(Service) + 实现(ServiceImpl) 并不是“多此一举”,而是为了 解耦可替换

  • Service 接口:定义“能干什么”。

  • ServiceImpl:定义“怎么干”。
    拆开后,调用方只关心“能干什么”,不关心“怎么干”,系统才真正做到 可扩展、可测试、低耦合

package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptSeviceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }
}

4.DeptMapper.java

package com.itheima.mapper;

import com.itheima.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DeptMapper {
    /**
     * 查询所有数据
     * @return
     */
    @Select("select id, name,create_time,update_time from dept order by update_time desc")
    List<Dept> findAll();
}

浏览器里输入 http://localhost:8080/depts 后,Spring Boot 会:

  1. 根据路径 /depts 找到 DeptController.list() 方法;

  2. 执行 deptService.findAll() 查询数据库;

  3. 把结果封装成 Result 对象,Spring 自动转成 JSON 返回给你。

只要控制台已经打印出

查询全部部门信息

并且浏览器/Postman 得到一段 JSON(而不是 404 白页),就说明接口已正常跑通。

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐