最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

最簡單的MyBatis Plus的多表聯(lián)接、分頁查詢實現(xiàn)方法

 更新時間:2020年11月02日 11:44:08   作者:IT小村  
這篇文章主要介紹了最簡單的MyBatis Plus的多表聯(lián)接、分頁查詢實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、前言

最近在加強 ITAEM 團隊的一個 app 項目——學生教師學習交流平臺
人員組成:安卓 + 前端 + 后臺
后臺 DAO 層借鑒了華工其他軟件開發(fā)團隊,使用了新穎強大的 MyBatisPlus 框架,里邊有一個類似百度貼吧的發(fā)帖子的功能:

這里寫圖片描述

而如果設計表,應為

帖子表 t_post
- id
- title 標題
- content 內容
- xx
- user_id 用戶外鍵
用戶表 t_user
+ id
+ name 帖子發(fā)起者名字
+ xx

示例圖中紅色框中的內容為 t_user 表的字段 name
而要實現(xiàn)上面顯示帖子,就要用到關聯(lián)查詢了,而且帖子很多,必須用分頁查詢,

那么,怎么通過 MyBatisPlus 來實現(xiàn)關聯(lián)、分頁查詢呢 ?很簡單,往下看。

二、需求、數(shù)據(jù)庫表設計

這是個人 app 項目中 v1.0 版本的部分表。

這里寫圖片描述

需求:顯示帖子

要帖子基本內容如時間、帖子內容等,即 t_question 表的內容全部要,

同時還要發(fā)帖子的人名字,即 t_student 的字段 name

三、代碼結構

為了寫這篇文章,抽取了該 app 項目中的部分代碼,彼此相互關系如下圖

這里寫圖片描述

四、代碼實現(xiàn)

1、代碼已經放到 github 上了,若對本文的代碼有疑問可以去 github 上查看詳情:
https://github.com/larger5/MyBatisPlus_page_tables.git

2、entity、mapper、service、controller 使用了 MyBatisPlus 的代碼生成器,自動生成大部分基礎的代碼,操作方法見之前的文章:
在 SpringBoot 中引入 MyBatisPlus 之 常規(guī)操作

1.實體

① Question

// import 省略

@TableName("t_question")
public class Question implements Serializable {

 private static final long serialVersionUID = 1L;

 @ApiModelProperty(value = "問答主鍵id")
 @TableId(value = "id", type = IdType.AUTO)
 private Integer id;

 @ApiModelProperty(value = "學生外鍵id")
 @TableField("student_id")
 private Integer studentId;

 @ApiModelProperty(value = "問題內容")
 private String content;

 @ApiModelProperty(value = "問題發(fā)布時間,發(fā)布的時候后臺自動生成")
 private Date date;

 @ApiModelProperty(value = "問題懸賞的積分")
 private Integer value;

	// getter、setter 省略
}

② Student

// import 省略

@TableName("t_student")
public class Student implements Serializable {

 private static final long serialVersionUID = 1L;

 @ApiModelProperty(value = "學生主鍵id")
 @TableId(value = "id", type = IdType.AUTO)
 private Integer id;

 @ApiModelProperty(value = "學生名稱")
 private String name;

 @ApiModelProperty(value = "學生密碼")
 private String password;

 @ApiModelProperty(value = "學生積分數(shù)")
 private Integer points;

 @ApiModelProperty(value = "學生郵件地址")
 private String email;

 @ApiModelProperty(value = "學生手機號碼")
 private String phone;

 @ApiModelProperty(value = "學生學號")
 private String num;

 @ApiModelProperty(value = "學生真實姓名")
 @TableField("true_name")
 private String trueName;

	// getter、setter 省略
}

2.mapper

① StudentMapper

// import 省略
public interface StudentMapper extends BaseMapper<Student> {
}

② QuestionMapper

// import 省略
public interface QuestionMapper extends BaseMapper<Question> {
 /**
  *
  * @param page 翻頁對象,可以作為 xml 參數(shù)直接使用,傳遞參數(shù) 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);

}

3、service

① StudentService

// import 省略
public interface StudentService extends IService<Student> {
}

② QuestionService

// import 省略
public interface QuestionService extends IService<Question> {

 Page<QuestionStudentVO> getQuestionStudent(Page<QuestionStudentVO> page);

}

4、serviceImpl

① StudentServiceImpl

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

}

② QuestionServiceImpl

// 省略 import

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

 @Override
 public Page<QuestionStudentVO> getQuestionStudent(Page<QuestionStudentVO> page) {
  return page.setRecords(this.baseMapper.getQuestionStudent(page));
 }

}

5、controller

// 省略 import

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

 @Autowired
 QuestionService questionService;

 @Autowired
 StudentService studentService;

 @GetMapping("/getAllQuestionByPage/{page}/{size}")
 public Map<String, Object> getAllQuestionByPage(@PathVariable Integer page, @PathVariable Integer size) {
  Map<String, Object> map = new HashMap<>();
  Page<Question> questionPage = questionService.selectPage(new Page<>(page, size));
  if (questionPage.getRecords().size() == 0) {
   map.put("code", 400);
  } else {
   map.put("code", 200);
   map.put("data", questionPage);
  }
  return map;
 }

 @GetMapping("/getAllQuestionWithStudentByPage/{page}/{size}")
 public Map<String, Object> getAllQuestionWithStudentByPage(@PathVariable Integer page, @PathVariable Integer size) {
  Map<String, Object> map = new HashMap<>();
  Page<QuestionStudentVO> questionStudent = questionService.getQuestionStudent(new Page<>(page, size));
  if (questionStudent.getRecords().size() == 0) {
   map.put("code", 400);
  } else {
   map.put("code", 200);
   map.put("data", questionStudent);
  }
  return map;
 }

}

6、MyBatisPlus 配置

// 省略 import

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

 /**
  * 分頁插件
  */
 @Bean
 public PaginationInterceptor paginationInterceptor() {
  return new PaginationInterceptor();
 }

 /**
  * 打印 sql
  */
 @Bean
 public PerformanceInterceptor performanceInterceptor() {
  PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
  //格式化sql語句
  Properties properties = new Properties();
  properties.setProperty("format", "true");
  performanceInterceptor.setProperties(properties);
  return performanceInterceptor;
 }
}

7、關聯(lián)查詢 VO 對象

// import 省略

public class QuestionStudentVO implements Serializable {

 @ApiModelProperty(value = "問答主鍵id")
 @TableId(value = "id", type = IdType.AUTO)
 private Integer id;

 @ApiModelProperty(value = "學生外鍵id")
 @TableField("student_id")
 private Integer studentId;

 private String name;

 @ApiModelProperty(value = "問題內容")
 private String content;

 @ApiModelProperty(value = "問題發(fā)布時間,發(fā)布的時候后臺自動生成")
 private Date date;

 @ApiModelProperty(value = "問題懸賞的積分")
 private Integer value;

	// getter、setter 省略

五、測試接口

這里寫圖片描述

1、沒有關聯(lián)的分頁查詢接口

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": "雪見從小父母雙亡,由爺爺唐坤撫養(yǎng)成人。",
  "date": 1533201716000,
  "value": 20
  }
 ],
 "pages": 5
 }
}

② sql 執(zhí)行

這里寫圖片描述

2、多表關聯(lián)、分頁查詢接口

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": "雪見從小父母雙亡,由爺爺唐坤撫養(yǎng)成人。",
  "date": 1533201716000,
  "value": 20
  }
 ],
 "pages": 5
 }
}

② sql 執(zhí)行

這里寫圖片描述

六、小結

寫本文的原因:

①網(wǎng)上有做法不合時宜的文章(自定義page類、配置版)②官方文檔使用的是配置版的,筆者采用注解版的

MyBatis 配置版 MyBatis 注解版
① 動態(tài) sql 靈活、② xml 格式的 sql,可拓展性好 ① 少一個設置,少一個錯誤爆發(fā)點、② 代碼清晰優(yōu)雅

當然,智者見智仁者見仁

參考資料:
MyBatisPlus 官方文檔:分頁插件:方式一 、傳參區(qū)分模式【推薦】

到此這篇關于最簡單的MyBatis Plus的多表聯(lián)接、分頁查詢實現(xiàn)方法的文章就介紹到這了,更多相關MyBatis Plus多表聯(lián)接、分頁查詢內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • MapStruct @Mapping注解之處理映射中的Null值方式

    MapStruct @Mapping注解之處理映射中的Null值方式

    這篇文章主要介紹了MapStruct @Mapping注解之處理映射中的Null值方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • java可變參數(shù)使用示例

    java可變參數(shù)使用示例

    這篇文章主要介紹了java可變參數(shù)使用示例,需要的朋友可以參考下
    2014-04-04
  • Java訪問者設計模式詳細講解

    Java訪問者設計模式詳細講解

    大多數(shù)情況下你不需要訪問者模式,但當一旦需要訪問者模式時,那就是真的需要它了,這是設計模式創(chuàng)始人的原話。可以看出應用場景比較少,但需要它的時候是不可或缺的,這篇文章就開始學習最后一個設計模式——訪問者模式
    2022-11-11
  • Java中16條的代碼規(guī)范

    Java中16條的代碼規(guī)范

    如何更規(guī)范化編寫Java 代碼的重要性想必毋需多言,其中最重要的幾點當屬提高代碼性能、使代碼遠離Bug、令代碼更優(yōu)雅,
    2021-07-07
  • mybatis的Configuration詳解

    mybatis的Configuration詳解

    這篇文章主要介紹了mybatis的Configuration詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Kotlin傳遞可變長參數(shù)給Java可變參數(shù)實例代碼

    Kotlin傳遞可變長參數(shù)給Java可變參數(shù)實例代碼

    這篇文章主要介紹了Kotlin傳遞可變長參數(shù)給Java可變參數(shù)實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • FeignClient支持運行時動態(tài)指定URL方式

    FeignClient支持運行時動態(tài)指定URL方式

    在實際開發(fā)中,我們經常通過FeignClient接口調用三方API,當面對不同的環(huán)境對應不同的地址時,可以通過配置文件和占位符來切換,但在同一個環(huán)境中需要調用不同地址的相同接口時,這種方法就失效了,此時,可以通過實現(xiàn)RequestInterceptor接口來動態(tài)切換地址
    2024-11-11
  • java把字符串寫入文件里的簡單方法分享

    java把字符串寫入文件里的簡單方法分享

    這篇文章主要介紹了java把字符串寫入到文件里的簡單方法,這是跟一個外國朋友學的代碼,需要的朋友可以參考下
    2014-03-03
  • 詳解Java8中CompletableFuture類的使用

    詳解Java8中CompletableFuture類的使用

    Java?8中引入了CompletableFuture類,它是一種方便的異步編程工具,可以處理各種異步操作,本文將詳細介紹CompletableFuture的使用方式,希望對大家有所幫助
    2023-04-04
  • 淺談JVM之java class文件的密碼本

    淺談JVM之java class文件的密碼本

    一切的一切都是從javac開始的。從那一刻開始,java文件就從我們肉眼可分辨的文本文件,變成了冷冰冰的二進制文件。變成了二進制文件是不是意味著我們無法再深入的去了解java class文件了呢?答案是否定的。本文將詳細介紹JVM之java class文件的密碼本。
    2021-06-06

最新評論

株洲县| 灵山县| 易门县| 平顶山市| 孝昌县| 彝良县| 利津县| 法库县| 内黄县| 桂阳县| 西昌市| 专栏| 嘉鱼县| 凤台县| 定结县| 冕宁县| 新兴县| 孝昌县| 巨野县| 额尔古纳市| 海兴县| 马关县| 永春县| 黑龙江省| 资源县| 平泉县| 格尔木市| 天门市| 浠水县| 庆阳市| 杭锦旗| 鄱阳县| 手游| 密云县| 临西县| 西乌| 龙南县| 利川市| 射阳县| 新宁县| 北票市|