教你如何写springboot接口

首先要明白数据的流通方向:

教你如何写springboot接口_第1张图片

数据的触发是前端请求后端引起的,遵循传统的mvc规范的话 我们需要pojo mapper service controller 四个层次,Pojo 是于数据库中字段直接对应的

在线搭建一个springboot项目

https://start.spring.io/

其中需要加入的四个依赖

教你如何写springboot接口_第2张图片

点击确定 把没有用的文件删除 最后保留一下两个:

教你如何写springboot接口_第3张图片

开始编写接口实现

pon.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.2
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.1
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
server:
  port: 8001

持久层:

package com.example.demo.entity;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String name;
    private String address;
    private Integer age;
    private String sex;
    private String phone;

}

这里我们引入了 lombok 不需要写getset方法简化代码


    org.projectlombok
    lombok
    1.16.10
    provided

mapper层

package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

public interface UserMapper {

    @Select("select * from user")
    List findAll();

    @Update("INSERT INTO `user` (`name`, `address`, `age`, `sex`, `phone`) VALUES (#{name},#{address},#{age},#{sex},#{phone});")
    @Transactional
    void save(User user);


    @Update("update user set name=#{name} , address=#{address}, age=#{age}, sex=#{sex},phone=#{phone} where id =#{id}")
    @Transactional
    void updateById(User user);


    @Delete("delete from user where id =#{id}")
    @Transactional
    void deleteById(Long id);

    @Select("select * from user where id =#{id}")
    User findById(Long id);


    @Select("select * from user limit #{offset},#{pageSize}")
    List findByPage(Integer offset, Integer pageSize);

    @Select("select count(id) from user")
    Integer countUser();
}

controller

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.vo.Page;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    
    @Resource
    UserMapper userMapper;
    @GetMapping
    public List getUser() {
        return userMapper.findAll();
    }

    @PostMapping
    public String addUser(@RequestBody User user){
        //把前端传过来的数据转化为user实体类的对象插入到数据库中
        userMapper.save(user);
        return "success";


    }
    @PutMapping
    public String updateUser(@RequestBody User user){
        userMapper.updateById(user);
        return "success";
    }

    @DeleteMapping("/{id}")  //一一对相应的关系
    public String deleteUser(@PathVariable("id") Long id){
        //注解是循序json回传带有id
        userMapper.deleteById(id);
        return "success";
    }
    @GetMapping("/{id}")  //把返回的结果 返回出来 包装成一个user对象
    public User findById(@PathVariable("id") Long id){
        //注解是循序json回传带有id
        return userMapper.findById(id);
    }

    @GetMapping("/page")
    public Page findByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        Integer offset = (pageNum - 1) * pageSize;
        List userData = userMapper.findByPage(offset, pageSize);
        Page page = new Page<>();
        page.setData(userData);

        Integer total = userMapper.countUser();
        page.setTotal(total);
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        return page;
    }

}

注意 :在实现过程中需要抓启动类中添加 扫描mapper的注解

以前就是对接口的增删改查 和分页查询的实现

实现过程:

教你如何写springboot接口_第4张图片

删除实现:

教你如何写springboot接口_第5张图片

分页查询:

教你如何写springboot接口_第6张图片

到此这篇关于教你如何写springboot接口 的文章就介绍到这了,更多相关写springboot接口 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(教你如何写springboot接口)