怎么写springboot接口
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,这篇文章将为大家详细讲解有关怎么写springboot接口 ,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。首先要明白数据的流通方向:数据的触发是前端请求后端引起的,
千家信息网最后更新 2025年11月14日怎么写springboot接口
这篇文章将为大家详细讲解有关怎么写springboot接口 ,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
首先要明白数据的流通方向:

数据的触发是前端请求后端引起的,遵循传统的mvc规范的话 我们需要pojo mapper service controller 四个层次,Pojo 是于数据库中字段直接对应的
在线搭建一个springboot项目
https://start.spring.io/
其中需要加入的四个依赖
点击确定 把没有用的文件删除 最后保留一下两个:
在此处添加jdk的版本:
开始编写接口实现
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: 123456server: port: 8001
持久层:
package com.example.demo.entity;import lombok.Data;@Datapublic class User { private Integer id; private String name; private String address; private Integer age; private String sex; private String phone;}这里我们引入了 lombok 不需要写get和set方法简化代码
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的注解
以前就是对接口的增删改查 和分页查询的实现
实现过程:
快速写出插入语句
插入实现 模拟前端想后端发送json数据
更新测试:
删除实现:
删除是要注意 id的一一对应
分页查询:
分页查询 参数1 第几页 参数2 一页有多少个数据
关于"怎么写springboot接口 "这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
数据
接口
前端
注解
篇文章
查询
参数
对象
数据库
更多
过程
不错
实用
一一对应
两个
代码
传统
内容
字段
实体
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
oracle系统数据库表
暴雪服务器忙
sql语句创建数据库和修改
荣成软件开发公司
宝山区第三方软件开发售后服务
软件开发合同税务局报备
衡山县网络安全和信息化
潜伏小说软件开发
网络安全法律问题研究
服务器一直自动启动
社区网络安全共建项目
表格列里找出相同的数据库
多媒体通信网络技术有哪些
java数据库连接用什么包
软件开发工程师的电脑配置
奥特曼传奇英雄聊天服务器
依图的服务器公司
计算机网络安全论文6000
网络安全法违法所得金额
魔法觉醒地区排名是本服务器吗
宣化区网络安全教育日
网盘间迁移数据库
配置与管理web服务器总结
excel炒股软件开发
表格列里找出相同的数据库
大型软件开发困难原因
网络技术岗 工作计划
卧虎藏龙2哪个服务器
饥荒服务器启动的原因
软件开发与程序设计语言