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班','人工智能学院','男','软件工程');加载依赖
Student实体类com.baomidou mybatis-plus-boot-starter3.4.0 org.projectlombok lomboktrue mysql mysql-connector-javaruntime org.springframework.boot spring-boot-starter-web
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 baseMapperStudentService{ }
package com.xmj.service; import com.xmj.entity.Student; import org.springframework.stereotype.Service; import java.util.List; @Service public interface StudentServie { public ListStudentServiceImplgetAllStudents(Integer page,Integer limit); }
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 ListStudentControllergetAllStudents(Integer page,Integer limit) { //定义分页对象 IPage studentPage = new Page<>(page,limit); //根据分页对象执行数据库查询,之后获取其其他分页数据. IPage result = studentMapper.selectPage(studentPage,null); //获取分页后的结果 List students = result.getRecords(); return students; } }
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 Listapplication.ymlgetStudents(){ return studentService.getAllStudents(1,10); } }
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/你的数据库名?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8 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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)