电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

最簡單的MyBatis Plus的多表聯接、分頁查詢實現方法

瀏覽:39日期:2023-10-21 18:07:10

一、前言

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

最簡單的MyBatis Plus的多表聯接、分頁查詢實現方法

而如果設計表,應為

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

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

那么,怎么通過 MyBatisPlus 來實現關聯、分頁查詢呢 ?很簡單,往下看。

二、需求、數據庫表設計

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

最簡單的MyBatis Plus的多表聯接、分頁查詢實現方法

需求:顯示帖子

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

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

三、代碼結構

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

最簡單的MyBatis Plus的多表聯接、分頁查詢實現方法

四、代碼實現

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 { 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 = '問題發布時間,發布的時候后臺自動生成') 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 = '學生積分數') 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 參數直接使用,傳遞參數 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 省略@Servicepublic class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {}

② QuestionServiceImpl

// 省略 import@Servicepublic 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')@EnableSwagger2public 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、關聯查詢 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 = '問題發布時間,發布的時候后臺自動生成') private Date date; @ApiModelProperty(value = '問題懸賞的積分') private Integer value;// getter、setter 省略

五、測試接口

最簡單的MyBatis Plus的多表聯接、分頁查詢實現方法

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 }}

② sql 執行

最簡單的MyBatis Plus的多表聯接、分頁查詢實現方法

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 }}

② sql 執行

最簡單的MyBatis Plus的多表聯接、分頁查詢實現方法

六、小結

寫本文的原因:

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

MyBatis 配置版 MyBatis 注解版 ① 動態 sql 靈活、② xml 格式的 sql,可拓展性好 ① 少一個設置,少一個錯誤爆發點、② 代碼清晰優雅

當然,智者見智仁者見仁

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

到此這篇關于最簡單的MyBatis Plus的多表聯接、分頁查詢實現方法的文章就介紹到這了,更多相關MyBatis Plus多表聯接、分頁查詢內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 吉林污水处理公司,长春工业污水处理设备,净水设备-长春易洁环保科技有限公司 | 京港视通报道-质量走进大江南北-京港视通传媒[北京]有限公司 | 密封圈_泛塞封_格莱圈-[东莞市国昊密封圈科技有限公司]专注密封圈定制生产厂家 | 防伪溯源|防窜货|微信二维码营销|兆信_行业内领先的防伪防窜货数字化营销解决方案供应商 | 线粒体膜电位荧光探针-细胞膜-标记二抗-上海复申生物科技有限公司 | 煤矿人员精确定位系统_矿用无线通信系统_煤矿广播系统 | AGV无人叉车_激光叉车AGV_仓储AGV小车_AGV无人搬运车-南昌IKV机器人有限公司[官网] | 无轨电动平车_轨道平车_蓄电池电动平车★尽在新乡百特智能转运设备有限公司 | 智能终端_RTU_dcm_北斗星空自动化科技| 耐酸泵,耐腐蚀真空泵,耐酸真空泵-淄博华舜耐腐蚀真空泵有限公司 精密模具-双色注塑模具加工-深圳铭洋宇通 | 葡萄酒灌装机-食用油灌装机-液体肥灌装设备厂家_青州惠联灌装机械 | SMN-1/SMN-A ABB抽屉开关柜触头夹紧力检测仪-SMN-B/SMN-C-上海徐吉 | 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 100_150_200_250_300_350_400公斤压力空气压缩机-舰艇航天配套厂家 | 便民信息网_家电维修,家电清洗,开锁换锁,本地家政公司 | 模温机-油温机-电加热导热油炉-工业冷水机「欧诺智能」 | 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 云南丰泰挖掘机修理厂-挖掘机维修,翻新,再制造的大型企业-云南丰泰工程机械维修有限公司 | 中高频感应加热设备|高频淬火设备|超音频感应加热电源|不锈钢管光亮退火机|真空管烤消设备 - 郑州蓝硕工业炉设备有限公司 | [官网]叛逆孩子管教_戒网瘾学校_全封闭问题青少年素质教育_新起点青少年特训学校 | HEYL硬度计量泵-荧光法在线溶解氧仪-净时测控技术(上海)有限公司 | 山东臭氧发生器,臭氧发生器厂家-山东瑞华环保设备 | UV-1800紫外光度计-紫外可见光度计厂家-翱艺仪器(上海)有限公司 | 泥沙分离_泥沙分离设备_泥砂分离机_洛阳隆中重工机械有限公司 | 镀锌方管,无缝方管,伸缩套管,方矩管_山东重鑫致胜金属制品有限公司 | 除甲醛公司-甲醛检测治理-杭州创绿家环保科技有限公司-室内空气净化十大品牌 | 艺术涂料|木纹漆施工|稻草漆厂家|马来漆|石桦奴|水泥漆|选加河南天工涂料 | 智能汉显全自动量热仪_微机全自动胶质层指数测定仪-鹤壁市科达仪器仪表有限公司 | 阿里巴巴诚信通温州、台州、宁波、嘉兴授权渠道商-浙江联欣科技提供阿里会员办理 | 粘度计NDJ-5S,粘度计NDJ-8S,越平水分测定仪-上海右一仪器有限公司 | 陕西鹏展科技有限公司 | 伺服电机维修、驱动器维修「安川|三菱|松下」伺服维修公司-深圳华创益 | T恤衫定做,企业文化衫制作订做,广告T恤POLO衫定制厂家[源头工厂]-【汉诚T恤定制网】 | 净化车间_洁净厂房_净化公司_净化厂房_无尘室工程_洁净工程装修|改造|施工-深圳净化公司 | 劳动法网-专业的劳动法和劳动争议仲裁服务网 | 消电检公司,消电检价格,北京消电检报告-北京设施检测公司-亿杰(北京)消防工程有限公司 | 成都珞石机械 - 模温机、油温机、油加热器生产厂家 | 涡街流量计_LUGB智能管道式高温防爆蒸汽温压补偿计量表-江苏凯铭仪表有限公司 | 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 | 云南成人高考网| 海外整合营销-独立站营销-社交媒体运营_广州甲壳虫跨境网络服务 焊管生产线_焊管机组_轧辊模具_焊管设备_焊管设备厂家_石家庄翔昱机械 |