教你如何寫springboot接口?
首先要明白數(shù)據(jù)的流通方向:

數(shù)據(jù)的觸發(fā)是前端請求后端引起的,遵循傳統(tǒng)的mvc規(guī)范的話 我們需要pojo mapper service controller 四個層次,Pojo 是于數(shù)據(jù)庫中字段直接對應(yīng)的
在線搭建一個springboot項目
https://start.spring.io/
其中需要加入的四個依賴

點擊確定 把沒有用的文件刪除 最后保留一下兩個:

開始編寫接口實現(xiàn)
pon.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ?? ?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> ?? ?<modelVersion>4.0.0</modelVersion> ?? ?<parent> ?? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ?<artifactId>spring-boot-starter-parent</artifactId> ?? ??? ?<version>2.6.2</version> ?? ??? ?<relativePath/> <!-- lookup parent from repository --> ?? ?</parent> ?? ?<groupId>com.example</groupId> ?? ?<artifactId>demo</artifactId> ?? ?<version>0.0.1-SNAPSHOT</version> ?? ?<name>demo</name> ?? ?<description>Demo project for Spring Boot</description> ?? ?<properties> ?? ??? ?<java.version>1.8</java.version> ?? ?</properties> ?? ?<dependencies> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ?<artifactId>spring-boot-starter-web</artifactId> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.mybatis.spring.boot</groupId> ?? ??? ??? ?<artifactId>mybatis-spring-boot-starter</artifactId> ?? ??? ??? ?<version>2.2.1</version> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>mysql</groupId> ?? ??? ??? ?<artifactId>mysql-connector-java</artifactId> ?? ??? ??? ?<scope>runtime</scope> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.projectlombok</groupId> ?? ??? ??? ?<artifactId>lombok</artifactId> ?? ??? ??? ?<optional>true</optional> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ?<artifactId>spring-boot-starter-test</artifactId> ?? ??? ??? ?<scope>test</scope> ?? ??? ?</dependency> ?? ?</dependencies> ?? ?<build> ?? ??? ?<plugins> ?? ??? ??? ?<plugin> ?? ??? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ??? ?<artifactId>spring-boot-maven-plugin</artifactId> ?? ??? ??? ??? ?<configuration> ?? ??? ??? ??? ??? ?<excludes> ?? ??? ??? ??? ??? ??? ?<exclude> ?? ??? ??? ??? ??? ??? ??? ?<groupId>org.projectlombok</groupId> ?? ??? ??? ??? ??? ??? ??? ?<artifactId>lombok</artifactId> ?? ??? ??? ??? ??? ??? ?</exclude> ?? ??? ??? ??? ??? ?</excludes> ?? ??? ??? ??? ?</configuration> ?? ??? ??? ?</plugin> ?? ??? ?</plugins> ?? ?</build> </project>
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 不需要寫get和set方法簡化代碼
<dependency> ? ? <groupId>org.projectlombok</groupId> ? ? <artifactId>lombok</artifactId> ? ? <version>1.16.10</version> ? ? <scope>provided</scope> </dependency>
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<User> 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<User> 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<User> getUser() {
? ? ? ? return userMapper.findAll();
? ? }
? ? @PostMapping
? ? public String addUser(@RequestBody User user){
? ? ? ? //把前端傳過來的數(shù)據(jù)轉(zhuǎn)化為user實體類的對象插入到數(shù)據(jù)庫中
? ? ? ? userMapper.save(user);
? ? ? ? return "success";
? ? }
? ? @PutMapping
? ? public String updateUser(@RequestBody User user){
? ? ? ? userMapper.updateById(user);
? ? ? ? return "success";
? ? }
? ? @DeleteMapping("/{id}") ?//一一對相應(yīng)的關(guān)系
? ? public String deleteUser(@PathVariable("id") Long id){
? ? ? ? //注解是循序json回傳帶有id
? ? ? ? userMapper.deleteById(id);
? ? ? ? return "success";
? ? }
? ? @GetMapping("/{id}") ?//把返回的結(jié)果 返回出來 包裝成一個user對象
? ? public User findById(@PathVariable("id") Long id){
? ? ? ? //注解是循序json回傳帶有id
? ? ? ? return userMapper.findById(id);
? ? }
? ? @GetMapping("/page")
? ? public Page<User> findByPage(@RequestParam(defaultValue = "1") Integer pageNum,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?@RequestParam(defaultValue = "10") Integer pageSize) {
? ? ? ? Integer offset = (pageNum - 1) * pageSize;
? ? ? ? List<User> userData = userMapper.findByPage(offset, pageSize);
? ? ? ? Page<User> page = new Page<>();
? ? ? ? page.setData(userData);
? ? ? ? Integer total = userMapper.countUser();
? ? ? ? page.setTotal(total);
? ? ? ? page.setPageNum(pageNum);
? ? ? ? page.setPageSize(pageSize);
? ? ? ? return page;
? ? }
}注意 :在實現(xiàn)過程中需要抓啟動類中添加 掃描mapper的注解
以前就是對接口的增刪改查 和分頁查詢的實現(xiàn)
實現(xiàn)過程:

刪除實現(xiàn):

分頁查詢:

到此這篇關(guān)于教你如何寫springboot接口 的文章就介紹到這了,更多相關(guān)寫springboot接口 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java?數(shù)組越界判斷和獲取數(shù)組長度的實現(xiàn)方式
這篇文章主要介紹了java?數(shù)組越界判斷和獲取數(shù)組長度的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Springboot使用JustAuth實現(xiàn)各種第三方登陸
本文主要介紹了Springboot使用JustAuth實現(xiàn)各種第三方登陸,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
基于spring boot 2和shiro實現(xiàn)身份驗證案例
這篇文章主要介紹了基于spring boot 2和shiro實現(xiàn)身份驗證案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
JAVA基于靜態(tài)數(shù)組實現(xiàn)棧的基本原理與用法詳解
這篇文章主要介紹了JAVA基于靜態(tài)數(shù)組實現(xiàn)棧的基本原理與用法,結(jié)合實例形式詳細(xì)分析了JAVA基于靜態(tài)數(shù)組實現(xiàn)棧相關(guān)原理、用法與操作注意事項,需要的朋友可以參考下2020-03-03
SpringBoot使用freemarker導(dǎo)出word文件方法詳解
這篇文章主要介紹了SpringBoot使用freemarker導(dǎo)出word文件方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11
Assert.assertNotNull()斷言是否是空問題
這篇文章主要介紹了Assert.assertNotNull()斷言是否是空問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
使用IntelliJ IDEA 進(jìn)行代碼對比的方法(兩種方法)
這篇文章給大家?guī)砹藘煞NIntelliJ IDEA 進(jìn)行代碼對比的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-01-01

