SpringBoot 사례 - 부서 관리 - 쿼리

 페이지 프로토타입 보기 및 요구 사항 확인

페이지 프로토타입

 수요 분석

인터페이스 문서 읽기

인터페이스 문서에 대한 링크는 다음과 같습니다.

https://docs.qq.com/doc/DUkRiTWVaUmFVck9N

아이디어 분석

사용자가 요청을 보내면 해당 Controller 클래스에서 처리하고, Controller 클래스는 서비스를 호출하여 부서 조회 기능을 구현하고, 해당 서비스 비즈니스 레이어는 해당 매퍼 인터페이스를 호출하여 매퍼를 통해 select * from dept를 실행한다. 데이터베이스를 쿼리하는 인터페이스; SQL 문은 쿼리 결과를 서비스로 반환하고, 서비스는 쿼리 결과를 Controller 클래스로 반환하고, 컨트롤러는 쿼리 결과를 통합된 쿼리 결과 Result 클래스에 캡슐화하고 마지막으로 전면에 응답합니다. 끝.

기능적 인터페이스 개발

 컨트롤 레이어(컨트롤러)

구체적인 코드는 다음과 같습니다

package com.example.tlias.controller;

import com.example.tlias.pojo.Dept;
import com.example.tlias.pojo.Result;
import com.example.tlias.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.logging.Logger;

@RestController
@Slf4j // 日志注解
public class DeptController {
    @Autowired
    // 注入service对象
    private DeptService deptService;

    // 获取日志记录对象
    // todo 查询部门信息
    // 指定请求路径及方式
    //    @RequestMapping(value = "/depts", method = RequestMethod.GET)
    // 上述注解的简化
    @GetMapping("depts")
    public Result list() {
        log.info("查询全部部门数据");
        // 调用service查询部门信息
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }
}

비즈니스 계층(서비스)

구체적인 코드는 다음과 같습니다

서비스 인터페이스

package com.example.tlias.service;

import com.example.tlias.pojo.Dept;

import java.util.List;

public interface DeptService {
    /**
     * 查询全部部门数据
     *
     * @return
     */
    List<Dept> list();
}

 서비스 인터페이스 구현 클래스

package com.example.tlias.service.impl;

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

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }
}

지속성 계층(매퍼)

package com.example.tlias.mapper;

import com.example.tlias.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 * from dept")
    List<Dept> list();
}

인터페이스 테스트

인터페이스 테스트에 Postman을 사용하고 먼저 SpringBoot 프로젝트를 시작한 다음 Postman에서 해당 요청을 보냅니다.

구체적인 연산 결과는 다음과 같다.

{
    "code": 1,
    "msg": "success",
    "data": [
        {
            "id": 1,
            "name": "学工部",
            "createTime": "2023-08-07T15:44:50",
            "updateTime": "2023-08-07T15:44:50"
        },
        {
            "id": 2,
            "name": "教研部",
            "createTime": "2023-08-07T15:44:50",
            "updateTime": "2023-08-07T15:44:50"
        },
        {
            "id": 3,
            "name": "咨询部",
            "createTime": "2023-08-07T15:44:50",
            "updateTime": "2023-08-07T15:44:50"
        },
        {
            "id": 4,
            "name": "就业部",
            "createTime": "2023-08-07T15:44:50",
            "updateTime": "2023-08-07T15:44:50"
        },
        {
            "id": 5,
            "name": "人事部",
            "createTime": "2023-08-07T15:44:50",
            "updateTime": "2023-08-07T15:44:50"
        }
    ]
}

제어 클래스에서 사용되는 주석은 @RestController(@Controller` 및 `@ResponseBody` 주석의 조합)이므로 제어 클래스는 프런트 엔드로 반환된 결과를 JSON 형식의 데이터로 자동 변환합니다. 테스트 성공
 

추천

출처blog.csdn.net/weixin_64939936/article/details/132200090