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

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐