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班','人工智能学院','男','软件工程');
加载依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
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);
}
}
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:
username: root
password: 123456
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
|