设计表结构

添加假数据

把项目收起来

全局搜索现有的项目查询接口

system/notice/list

找到controller文件点击圆钮,查看这个文件在项目的什么位置

然后依次进入service  >  serviceImpl > mapper > mapper.xml > 实体

全部定位到文件所在位置

行业规范,一个接口,必须要有这6个文件,分别为

controller > service  >  serviceImpl > mapper > mapper.xml > 实体

新建一个controller,和其他接口controller同一层级创建

同理,service  >  serviceImpl > mapper > mapper.xml > 实体也是跟之前接口同级目录下创建

 

controller

package com.ruoyi.web.controller.system;

import com.ruoyi.system.domain.Student;
import com.ruoyi.system.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("student/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 查询所有学生信息
     */
    @GetMapping("list")
    public Map<String,Object> list(){
        List<Student> lst = studentService.list();
        Map<String,Object> map = new HashMap<>();
        map.put("data",lst);
        return map;
    }
}

service

package com.ruoyi.system.service;

import com.ruoyi.system.domain.Student;

import java.util.List;

public interface StudentService {
    List<Student> list();
}

serviceImpl

package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Student;
import com.ruoyi.system.mapper.StudentMapper;
import com.ruoyi.system.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public List<Student> list() {
        return studentMapper.list();
    }
}

mapper

package com.ruoyi.system.mapper;

import com.ruoyi.system.domain.Student;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface StudentMapper {
    List<Student> list();
}

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.system.mapper.StudentMapper">

    <resultMap type="Student" id="SysUserResult">
        <id     property="name"       column="name"      />
        <result property="age"       column="age"      />
    </resultMap>

    <select id="list" resultMap="SysUserResult">
        SELECT * FROM `student`
    </select>


</mapper>

实体

package com.ruoyi.system.domain;

public class Student {

    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

postman测试接口