重学后端(2)-SpringBoot后端

本文介绍了如何创建一个SpringBoot项目,并引入Mybatis-Plus和Gson库。通过步骤展示了从创建项目到配置依赖,再到编写实体类、Mapper接口和Controller,实现增删改查功能的过程。提供了详细的代码示例,帮助开发者快速理解和实践。
摘要由CSDN通过智能技术生成

1. 创建Spring Boot项目

在这里插入图片描述
如果start.spring.io连接失败, 可以尝试改成start.springboot.io
在这里插入图片描述

2.maven引入一下常见包

如果idea没有自动识别为maven项目, 右键'pom.xml'->'Add as Maven Project'

mybatis-plus和gson

<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.9</version>
</dependency>

3.建立实体类和分层页面

com.example.back
  -pojo.Student
  -mapper.StudentMapper
  -controller.StudentController

在启动类BackApplication中, 添加@MapperScan("com.example.back.mapper") 注解.

①.Student实体类

@Data  // Lombok注解
public class Student {
    private Long id;
    private String name;
    private Integer age;
    private Integer chi;
    private Integer math;
    private Integer english;
}

②. mapper映射类

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

public interface StudentMapper extends BaseMapper<Student> {
}

③. 书写controller接口

@Autowired
private StudentMapper mapper;
private Gson gson = new Gson();

@GetMapping("/getAll")
public String getAll() {

    return gson.toJson(mapper.selectList(null)) ;
}

@PostMapping("/update")
public void update(@RequestBody Student student){
    mapper.updateById(student);
}

@PostMapping("/insert")
public void insert(@RequestBody Student student) {
    mapper.insert(student);
}

@PostMapping("/delete")
public void delete(@RequestBody Student student){
    mapper.deleteById(student.getId());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值