首先,声明,这个是我用来记录学习的,不是教学,本人菜鸟

1. 首先建一个spring的项目,选择java的版本,jdk等等参数 

spring boot app接口 spring boot怎么写接口_spring

2.选择依赖,如图所示的四个依赖 ,然后点击create即创建项目成功

spring boot app接口 spring boot怎么写接口_User_02

 

 3.成功后如下:

spring boot app接口 spring boot怎么写接口_User_03

4.配置数据库

配置自己的参数即可 

spring boot app接口 spring boot怎么写接口_开发语言_04

spring boot app接口 spring boot怎么写接口_spring boot app接口_05

 

5.建文件夹如下

spring boot app接口 spring boot怎么写接口_spring boot app接口_06

 

6.demo/entity/User.java

引入lombok,可以不用写get/set方法

package com.example.demo.entity;import lombok.Data;@Datapublic class User { private Integer id; private Integer age; private String name; private String address; private String sex; private String phone; }


7. demo/controller/userController.java

package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {
    @Select("select * from user")
    List<User> findAll();
}

8.demo/controller/userController.java

package com.example.demo.controller;
import java.util.List;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/User")
public class userController {
    @Resource
    UserMapper UserMapper;
    @GetMapping("/all")
    public String getUser()
    {
        return "user";
    }
    @GetMapping("/XX")
    public List<User> getData()
    {
        return  UserMapper.findAll();
    }
}

 10.然后执行成功后,在本地访问

访问url: http://localhost:8080/User/XX

spring boot app接口 spring boot怎么写接口_spring boot app接口_07

note: 数据库配置要和user里边的字段保持一致

spring boot app接口 spring boot怎么写接口_User_08