用springboot+mybatis-plus写接口返回json数据

MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在,mybatis-plus用起来真是方便。

我们来看看如何用springboot+mybatis-plus写接口返回json数据吧!

数据库Student表

CREATE TABLE `student` (
  `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '学号',
  `name` varchar(255) NOT NULL COMMENT '姓名',
  `the_class` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '班级',
  `branch` varchar(255) DEFAULT NULL COMMENT '学院',
  `sex` varchar(255) DEFAULT NULL COMMENT '性别',
  `major` varchar(255) DEFAULT NULL COMMENT '专业',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

插入数据

INSERT INTO STUDENT VALUES(111,'小杰','20软工7班','人工智能学院','男','软件工程'),(112,'小凯','20软工7班','人工智能学院','男','软件工程'),(113,'小海','20软工7班','人工智能学院','男','软件工程'),(114,'小波','20软工7班','人工智能学院','男','软件工程'),(115,'小伟','20软工7班','人工智能学院','男','软件工程'),(116,'小明','20软工7班','人工智能学院','男','软件工程');

创建项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

pmo.xml的依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

Student实体类

package com.xmj.entity;
import lombok.Data;

@Data
public class Student {
    private Integer id;
    private String name;
    private String sex;
    private String theClass;
    private String branch;
    private String major;
}

StudentMapper

package com.xmj.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmj.entity.Student;

public interface StudentMapper extends BaseMapper<Student> {
}

StudentService

package com.xmj.service;

import com.xmj.entity.Student;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface StudentServie {
    public List<Student> getAllStudents(Integer page,Integer limit);
}

StudentServiceImpl

package com.xmj.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmj.entity.Student;
import com.xmj.mapper.StudentMapper;
import com.xmj.service.StudentServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentServie {
    @Autowired(required = false)
    private StudentMapper studentMapper;
    @Override
    public List<Student> getAllStudents(Integer page,Integer limit) {
        //定义分页对象
        IPage<Student> studentPage = new Page<>(page,limit);
        //根据分页对象执行数据库查询,之后获取其其他分页数据.
        IPage<Student> result = studentMapper.selectPage(studentPage,null);
        //获取分页后的结果
        List<Student> students = result.getRecords();
        return students;
    }
}

StudentController

package com.xmj.controller;

import com.xmj.entity.Student;
import com.xmj.service.impl.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private StudentServiceImpl studentService;

    @RequestMapping("/data/students")
    @ResponseBody
    public List<Student> getStudents(){
        return studentService.getAllStudents(1,10);
    }
}

MybatisPlusDemoApplication启动类

package com.xmj;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.xmj.mapper")
@SpringBootApplication
public class MybatisPlusDemoApplication{

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusDemoApplication.class, args);
    }

}

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/你的数据库名?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
    username: 用户名
    password: 密码
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    thymeleaf:
      prefix: classpath:/templates
      suffix: .html

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
  port: 8081

在这里插入图片描述

  • 25
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Spring Boot和MyBatis-Plus是现代Java应用开发中的两个流行框架组合,它们一起简化了企业级应用程序的快速搭建。以下是一个基本的Spring Boot + MyBatis-Plus的整体开发流程: 1. **项目初始化**: - 创建一个新的Spring Boot项目(使用Maven或Gradle构建工具)。 - 添加Spring Boot Web、Spring Data JPA和MyBatis-Plus的依赖。 2. **数据库配置**: - 配置数据源(如HikariCP, Druid, 或者Spring Boot内置的DataSource)。 - 定义数据库连接池和JDBC驱动。 3. **实体类映射**: - 创建Java实体类(Entity),它们将对应数据库表结构。 - 使用MyBatis-Plus的Model Generator工具自动生成基础CRUD操作的代码。 4. **Mapper接口生成**: - 通过MyBatis-Plus的全局配置文件(GlobalConfig),指定Mapper接口生成的位置。 5. **Service层编**: - 实现业务逻辑的服务类(Service),这些类通常会依赖于MyBatis-Plus的Mapper接口进行数据库操作。 6. **Repository接口和实现**: - 如果需要,可以创建Repository接口,继承自MyBatis-Plus的BaseMapper,进一步定制化查询。 7. **Controller层处理**: - 创建RESTful API控制器(Controller),调用Service层的方法,并返回JSON响应给客户端。 8. **配置数据初始化**: - 编Spring Boot的启动类(Application)中的main方法,可能包含一些数据初始化操作。 9. **测试**: - 使用单元测试(JUnit, TestNG等)确保各个组件正常工作,如Service和Mapper接口的测试。 10. **部署**: - 部署项目到生产环境,如Tomcat, Jetty, 或者云服务器上的Web容器。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰咖啡iii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值