一、首先你需要分页插件、简单的接口响应类,进入下面的传送门即可
二、需要一个最终返回的VO类
UserPageVO
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.util.Date;
/**
* @author DBC
* @date 2022/4/29 11:50
*/
@Data
public class UserPageVO {
private Integer id;
private String name;
private String age;
private String devName;
private Date devTime;
}
三、数据库很简单,如下图
用户表
部门表
准备工作结束,接下来就是直接使用了,我们看第一种:XML形式实现
XML形式
mapper中添加以下代码
Page<UserPageVO> selectuserlistpage(Page<UserPageVO> tPage, int id);
<select id="selectuserlistpage" resultType="com.example.test05.demo.model.UserPageVO">
select * from tb_user u
left join
tb_dev d on u.dev_id = d.id
where u.id = #{id}
</select>
如上,很简单的sql语句
Controller测试一波
/**
* 综合测试
*
* @param page 页码
* @param size 个数
* @return
*/
@GetMapping("test")
public JsonData getAllQuestionWithStudentByPage(int page, int size, int id) {
Page<UserPageVO> questionStudent = tbUserMapper.selectuserlistpage(new Page<>(page, size), id);
if (questionStudent.getRecords().size() == 0) {
return JsonData.buildError("数据为空");
} else {
return JsonData.buildSuccess(questionStudent);
}
} 我们看第二种:注解形式实现
注解形式
mapper中添加以下代码
@Select("select * from tb_user u left join tb_dev d on u.dev_id = d.id where u.id = #{id}")
Page<UserPageVO> selectuserlistpage2(Page<UserPageVO> tPage, int id); 什么都不需要了,直接Controller测试即可
/**
* 综合测试
*
* @param page 页码
* @param size 个数
* @return
*/
@GetMapping("test2")
public JsonData getAllQuestionWithStudentByPage2(int page, int size, int id) {
Page<UserPageVO> questionStudent = tbUserMapper.selectuserlistpage2(new Page<>(page, size), id);
if (questionStudent.getRecords().size() == 0) {
return JsonData.buildError("数据为空");
} else {
return JsonData.buildSuccess(questionStudent);
}
} 两种方式,喜欢哪一种都可以使用,可以具体看你们公司的规范要求!各有利弊,百度自己查查就好
让我们结合上面这篇文章来操作一下
表变化
首先我们的表有了一点点的变化,多了一个表tb_order(订单表),还有部门表和订单表的id都改为了如下图对应的字段,具体原因下面说
结果输出json
点击查看完整内容
{
"code": 0,
"data": {
"records": [
{
"id": 1,
"name": "DBC",
"age": "12",
"devName": "北京部门",
"devTime": "2022-04-21T03:48:15.000+00:00",
"list": [
{
"id": 1,
"order": "冰箱",
"userId": 1
},
{
"id": 2,
"order": "空调",
"userId": 1
},
{
"id": 3,
"order": "电脑",
"userId": 1
}
]
},
{
"id": 3,
"name": "DBC1",
"age": null,
"devName": null,
"devTime": null,
"list": []
},
{
"id": 4,
"name": "DBC2",
"age": null,
"devName": null,
"devTime": null,
"list": []
},
{
"id": 5,
"name": "DBC3",
"age": null,
"devName": null,
"devTime": null,
"list": []
},
{
"id": 6,
"name": "DBC4",
"age": null,
"devName": null,
"devTime": null,
"list": []
},
{
"id": 7,
"name": "DBC5",
"age": null,
"devName": null,
"devTime": null,
"list": []
},
{
"id": 8,
"name": "DBC",
"age": "123",
"devName": null,
"devTime": null,
"list": []
},
{
"id": 9,
"name": "DBC",
"age": "1",
"devName": null,
"devTime": null,
"list": []
}
],
"total": 8,
"size": 10,
"current": 1,
"orders": [],
"optimizeCountSql": true,
"hitCount": false,
"countId": null,
"maxLimit": null,
"searchCount": true,
"pages": 1
},
"msg": "成功"
} 本文作者为DBC,转载请注明。



