您的位置:首页 > 其它

最简单的 MyBatis Plus 的多表联接、分页查询实现方法

2019-07-03 10:53 579 查看

转载url:
https://blog.csdn.net/larger5/article/details/82082819

版权声明:有问题欢迎留言,转载请声明出处                        https://blog.csdn.net/larger5/article/details/82082819                    </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-cd6c485e8b.css">
<div id="content_views" class="markdown_views prism-github-gist">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<h1><a name="t0"></a><a id="_0"></a>一、前言</h1>

最近在加强 ITAEM 团队的一个 app 项目——学生教师学习交流平台
人员组成:安卓 + 前端 + 后台
后台 DAO 层借鉴了华工其他软件开发团队,使用了新颖强大的

MyBatisPlus
框架,里边有一个类似百度贴吧的发帖子的功能:
![这里写图片描述](https://img-blog.csdn.net/20180826164333911?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xhcmdlcjU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
而如果设计表,应为

  • 帖子表 t_post
    - id
    - title 标题
    - content 内容
    - xx
    - user_id 用户外键
  • 用户表 t_user id
  • name 帖子发起者名字
  • xx

示例图中红色框中的内容为

t_user
表的字段
name

而要实现上面显示帖子,就要用到关联查询了,而且帖子很多,必须用分页查询,

那么,怎么通过 MyBatisPlus 来实现关联、分页查询呢 ?很简单,往下看。

二、需求、数据库表设计

这是个人 app 项目中 v1.0 版本的部分表。
![这里写图片描述](https://img-blog.csdn.net/20180826165727789?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xhcmdlcjU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
需求:显示帖子

  • 要帖子基本内容如时间、帖子内容等,即
    t_question
    表的内容全部要,
  • 同时还要发帖子的人名字,即
    t_student
    的字段
    name

三、代码结构

为了写这篇文章,抽取了该 app 项目中的部分代码,彼此相互关系如下图
![这里写图片描述](https://img-blog.csdn.net/20180826171008366?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xhcmdlcjU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

四、代码实现

1、代码已经放到 github 上了,若对本文的代码有疑问可以去 github 上查看详情:
https://github.com/larger5/MyBatisPlus_page_tables.git

2、

entity、mapper、service、controller
使用了 MyBatisPlus 的代码生成器,自动生成大部分基础的代码,操作方法见之前的文章:
在 SpringBoot 中引入 MyBatisPlus 之 常规操作

1.实体

① Question

// import 省略

@TableName(“t_question”)
public class Question implements Serializable {

<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">long</span> serialVersionUID <span class="token operator">=</span> <span class="token number">1</span>L<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"问答主键id"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@TableId</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"id"</span><span class="token punctuation">,</span> type <span class="token operator">=</span> IdType<span class="token punctuation">.</span>AUTO<span class="token punctuation">)</span>
<span class="token keyword">private</span> Integer id<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"学生外键id"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@TableField</span><span class="token punctuation">(</span><span class="token string">"student_id"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> Integer studentId<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"问题内容"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> String content<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"问题发布时间,发布的时候后台自动生成"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> Date date<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"问题悬赏的积分"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> Integer value<span class="token punctuation">;</span>

<span class="token comment">// getter、setter 省略</span>

}

  • 1 7ff7
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

② Student

// import 省略

@TableName(“t_student”)
public class Student implements Serializable {

<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">long</span> serialVersionUID <span class="token operator">=</span> <span class="token number">1</span>L<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"学生主键id"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@TableId</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"id"</span><span class="token punctuation">,</span> type <span class="token operator">=</span> IdType<span class="token punctuation">.</span>AUTO<span class="token punctuation">)</span>
<span class="token keyword">private</span> Integer id<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"学生名称"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> String name<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"学生密码"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> String password<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"学生积分数"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> Integer points<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"学生邮件地址"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> String email<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"学生手机号码"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> String phone<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"学生学号"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> String num<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"学生真实姓名"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@TableField</span><span class="token punctuation">(</span><span class="token string">"true_name"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> String trueName<span class="token punctuation">;</span>

<span class="token comment">// getter、setter 省略</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

2.mapper

① StudentMapper

// import 省略public interface StudentMapper extends BaseMapper<Student> {
}
  • 1
  • 2
  • 3
[/code]

② QuestionMapper

// import 省略public interface QuestionMapper extends BaseMapper<Question> {
/**
*
* @param page 翻页对象,可以作为 xml 参数直接使用,传递参数 Page 即自动分页
* @return
*/
@Select("SELECT t_question.*,t_student.`name` FROM t_question,t_student WHERE t_question.student_id=t_student.id")
List<QuestionStudentVO> getQuestionStudent(Pagination page);

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3、service

① StudentService

// import 省略public interface StudentService extends IService<Student> {
}
  • 1
  • 2
  • 3
[/code]

② QuestionService

// import 省略public interface QuestionService extends IService<Question> {
Page<span class="token generics function"><span class="token punctuation">&lt;</span>QuestionStudentVO<span class="token punctuation">&gt;</span></span> <span class="token function">getQuestionStudent</span><span class="token punctuation">(</span>Page<span class="token generics function"><span class="token punctuation">&lt;</span>QuestionStudentVO<span class="token punctuation">&gt;</span></span> page<span class="token punctuation">)</span><span class="token punctuation">;</span>
[/code]

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4、serviceImpl

① StudentServiceImpl

// import 省略@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

}

  • 1
  • 2
  • 3
  • 4
  • 5

② QuestionServiceImpl

// 省略 import

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> Page<span class="token generics function"><span class="token punctuation">&lt;</span>QuestionStudentVO<span class="token punctuation">&gt;</span></span> <span class="token function">getQuestionStudent</span><span class="token punctuation">(</span>Page<span class="token generics function"><span class="token punctuation">&lt;</span>QuestionStudentVO<span class="token punctuation">&gt;</span></span> page<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> page<span class="token punctuation">.</span><span class="token function">setRecords</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>baseMapper<span class="token punctuation">.</span><span class="token function">getQuestionStudent</span><span class="token punctuation">(</span>page<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

5、controller

// 省略 import

@RestController
@RequestMapping("/common")
@EnableSwagger2
public class CommonController {

<span class="token annotation punctuation">@Autowired</span>
QuestionService questionService<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Autowired</span>
StudentService studentService<span class="token punctuation">;</span>

<span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/getAllQuestionByPage/{page}/{size}"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> Map<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span> Object<span class="token punctuation">&gt;</span></span> <span class="token function">getAllQuestionByPage</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span> Integer page<span class="token punctuation">,</span> <span class="token annotation punctuation">@PathVariable</span> Integer size<span class="token punctuation">)</span> <span class="token punctuation">{</span>
Map<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span> Object<span class="token punctuation">&gt;</span></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Page<span class="token generics function"><span class="token punctuation">&lt;</span>Question<span class="token punctuation">&gt;</span></span> questionPage <span class="token operator">=</span> questionService<span class="token punctuation">.</span><span class="token function">selectPage</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Page</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token punctuation">(</span>page<span class="token punctuation">,</span> size<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>questionPage<span class="token punctuation">.</span><span class="token function">getRecords</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"code"</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"code"</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">,</span> questionPage<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> map<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/getAllQuestionWithStudentByPage/{page}/{size}"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> Map<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span> Object<span class="token punctuation">&gt;</span></span> <span class="token function">getAllQuestionWithStudentByPage</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span> Integer page<span class="token punctuation">,</span> <span class="token annotation punctuation">@PathVariable</span> Integer size<span class="token punctuation">)</span> <span class="token punctuation">{</span>
Map<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span> Object<span class="token punctuation">&gt;</span></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Page<span class="token generics function"><span class="token punctuation">&lt;</span>QuestionStudentVO<span class="token punctuation">&gt;</span></span> questionStudent <span class="token operator">=</span> questionService<span class="token punctuation">.</span><span class="token function">getQuestionStudent</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Page</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token punctuation">(</span>page<span class="token punctuation">,</span> size<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>questionStudent<span class="token punctuation">.</span><span class="token function">getRecords</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"code"</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"code"</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">,</span> questionStudent<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> map<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

6、MyBatisPlus 配置

// 省略 import

@EnableTransactionManagement
@Configuration
@MapperScan(“com.cun.app.mapper”)
public class MybatisPlusConfig {

<span class="token comment">/**
* 分页插件
*/</span>
<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> PaginationInterceptor <span class="token function">paginationInterceptor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">PaginationInterceptor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
* 打印 sql
*/</span>
<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> PerformanceInterceptor <span class="token function">performanceInterceptor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
PerformanceInterceptor performanceInterceptor <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PerformanceInterceptor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//格式化sql语句</span>
Properties properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
properties<span class="token punctuation">.</span><span class="token function">setProperty</span><span class="token punctuation">(</span><span class="token string">"format"</span><span class="token punctuation">,</span> <span class="token string">"true"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
performanceInterceptor<span class="token punctuation">.</span><span class="token function">setProperties</span><span class="token punctuation">(</span>properties<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> performanceInterceptor<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

7、关联查询 VO 对象

// import 省略

public class QuestionStudentVO implements Serializable {

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"问答主键id"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@TableId</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"id"</span><span class="token punctuation">,</span> type <span class="token operator">=</span> IdType<span class="token punctuation">.</span>AUTO<span class="token punctuation">)</span>
<span class="token keyword">private</span> Integer id<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"学生外键id"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@TableField</span><span class="token punctuation">(</span><span class="token string">"student_id"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> Integer studentId<span class="token punctuation">;</span>

<span class="token keyword">private</span> String name<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"问题内容"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> String content<span class="token punctuation">;</span>

<span class="token annotat
4000
ion punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"问题发布时间,发布的时候后台自动生成"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> Date date<span class="token punctuation">;</span>

<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"问题悬赏的积分"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> Integer value<span class="token punctuation">;</span>

<span class="token comment">// getter、setter 省略</span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

五、测试接口

![这里写图片描述](https://img-blog.csdn.net/20180826180214880?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xhcmdlcjU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

1、没有关联的分页查询接口

http://localhost/common/getAllQuestionByPage/1/2

① json 输出

{
"code": 200,
"data": {
"total": 10,
"size": 2,
"current": 1,
"records": [
{
"id": 1,
"studentId": 3,
"content": "唐代,渝州城里,有一个性格开朗、乐观的小伙子,名叫景天。",
"date": 1534497561000,
"value": 5
},
{
"id": 2,
"studentId": 1,
"content": "雪见从小父母双亡,由爷爷唐坤抚养成人。",
"date": 1533201716000,
"value": 20
}
],
"pages": 5
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
[/code]

② sql 执行

![这里写图片描述](https://img-blog.csdn.net/20180826180051903?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xhcmdlcjU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

2、多表关联、分页查询接口

http://localhost/common/getAllQuestionWithStudentByPage/1/2

① json 输出

{
"code": 200,
"data": {
"total": 10,
"size": 2,
"current": 1,
"records": [
{
"id": 1,
"studentId": 3,
"name": "vv",
"content": "唐代,渝州城里,有一个性格开朗、乐观的小伙子,名叫景天。",
"date": 1534497561000,
"value": 5
},
{
"id": 2,
"studentId": 1,
"name": "cun",
"content": "雪见从小父母双亡,由爷爷唐坤抚养成人。",
"date": 1533201716000,
"value": 20
}
],
"pages": 5
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
[/code]

② sql 执行

![这里写图片描述](https://img-blog.csdn.net/20180826180518501?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xhcmdlcjU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

六、小结

写本文的原因:

  • ①网上有做法不合时宜的文章(自定义page类、配置版)
  • ②官方文档使用的是配置版的,笔者采用注解版的
MyBatis 配置版 MyBatis 注解版
① 动态 sql 灵活、② xml 格式的 sql,可拓展性好 ① 少一个设置,少一个错误爆发点、② 代码清晰优雅

当然,智者见智仁者见仁

参考资料:
MyBatisPlus 官方文档:分页插件:方式一 、传参区分模式【推荐】

</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet">
</div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: