百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

MyBatis-Plus 不支持联表?一个依赖轻松搞定,非常牛逼!

wptr33 2024-12-15 17:12 31 浏览

1、什么是Mybatis-plus-join?

Mybatis-plus-join是网上的大佬为解决Mybatis-plus做多表连接查询吃力而开发的一个工具,它能基于Mybatisplus进行功能升级,使其不再受限于单表查询,其中本人总结有以下几点:

  • 对Mybatis-plus进行功能升级 ,提高开发效率;
  • 使用方法与Mybatis-plus一样 ,学习成本低;
  • 增加了 多表连接查询 功能,摆脱xml模式多表连接;

2、下载Mybatis-plus-join

Mybatis-plus-join目前发布在Gitee上,直接将其克隆/下载到本地即可:

下载地址:

https://gitee.com/best_handsome/mybatis-plus-join

注意:需要 Mybatis-plus version >= 3.4.0

下载结果如图:

3、使用Maven将其打包成jar包

3.1、使用idea打包

1.按照下图进行打包操作:

2.打包成功后,jar包存在的目录,找到jar包并复制到自己的项目中:

3.将打出来的jar包引入工程模块中,步骤如图(如果跳出版本问题,则点击更新即可):

4.项目中引用Mybatis-plus-join的pom.xml配置

在pom.xml中引入该依赖:

<!--mybatis-plus-join-->
<dependency>
 <groupId>com.github.yulichang</groupId>
 <artifactId>mybatis-plus-join</artifactId>
 <version>1.1.6</version>
</dependency>

引入效果(检查时提示更新就更新部分环境即可)

5、让mybatis-plus-join在DataScopeSqlInjector中生效

1.修改DataScopeSqlInjector中的继承类为:MPJSqlInjector

6、启动类排除MPJSqlInjector.class

排除MPJSql注入器的语句

@SpringBootApplication(exclude = {MPJSqlInjector.class})

1.在主启动类中排除MPJ的sql注入器

2.如果自定义了配置类,则在配置类中添加排除

7.检查启用mybatis-plus-join是否成功

  1. 重启启动类
  2. 正常启动则说明配置成功,请转到第8步
  3. 如果出现下列错误,说明第6步没有配置好,要检查
***************************
APPLICATION FAILED TO START
***************************
Description:
file [D:package\admin\mapper\SysOauthClientDetailsMapper.class] required a single bean, but 2 were found:
 - dataScopeSqlInjector: defined by method 'dataScopeSqlInjector' in class path resource [package/common/data/mybatis/MybatisPlusConfiguration.class]
 - com.github.yulichang.injector.MPJSqlInjector: defined in null
 - 
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

8、在业务中使用mybatis-plus-join的准备

使用

  • mapper继承MPJBaseMapper (必选)
  • service继承MPJBaseService (可选)
  • serviceImpl继承MPJBaseServiceImpl (可选)

继承效果如下

  1. mapper
  1. serviceImpl

9.使用mybatis-plus-join进行多表查询(例子)

1.DTO类

package *.admin.api.dto;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Data
public class ObOutPoolDto  {
 /**
  * 表1:物品表
  */
 private String name;  //物品名称
 private String standard; //物品规格
 private String unit;  //物品单位
 private BigDecimal price;  //物品价格
 private BigDecimal count;//物品库存数量
 /**
  * 表2:订单表
  */
 private String drawNumber; //表单流水号
 public Integer poolId; //库存id
 private Integer needCount;//出库数量
 private String person;//申请人
 private LocalDateTime takeTime;//任务时间
 private String department;//部门
 private String updatedBy;//更新人
 private LocalDateTime updatedTime;//更新时间
 private Integer id;//id
 private String remark;//备注
}

2.serviceImpl

package *
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 出库商品
 * @author 
 * @date 2021-04-23 14:25:26
 */
@Service
public class ObOutPoolItemServiceImpl extends MPJBaseServiceImpl<ObOutPoolItemMapper, ObOutPoolItem> implements ObOutPoolItemService {
 // 定义一个枚举类型
 enum  Signal {
  FINISH("完成",1),ORDER("订单",2);
  private String name;
  private int index;
  // 构造方法
  private Signal(String name,int index) {
   this.name = name;
   this.index = index;
  }
 }

 /**
  * 外部jar包支持的连接查询并分页,弥补mytabisplus的不足
  * 分页查询
  */
 @Override
 public IPage<ObOutPoolDto> mypage(Page page, ObOutPoolItem obOutPoolItem, String[] createdTimeArr){
  IPage<ObOutPoolDto> page1 = baseMapper.selectJoinPage(
    page,   //分页参数
    ObOutPoolDto.class,   //返回的DTO层数据,
    mpjLambdaWrapper(MPJ(),obOutPoolItem,createdTimeArr,Signal.FINISH.name));  //MPJWrapper
  return page1;
 }

 /**
  * 按照订单号查询商品
  * @param orderNumber
  * @return
  */
 @Override
 public List<ObOutPoolDto> queryByOrderNumber(String orderNumber){
  List<ObOutPoolDto> list = baseMapper.selectJoinList(
    ObOutPoolDto.class,
    mpjLambdaWrapper(MPJ(),new ObOutPoolItem() ,new String[0] ,Signal.ORDER.name,orderNumber));
  return list;
 }

 //基础连接查询MPJ对象
 @Override
 public MPJLambdaWrapper<ObOutPoolDto> MPJ(){
  MPJLambdaWrapper<ObOutPoolDto> mpj =  new MPJLambdaWrapper<ObOutPoolDto>()
    .selectAll(ObOutPoolItem.class)    //查询表1所有内容
    .selectAll(ObPool.class)           //查询表2所有内容
    .leftJoin(ObPool.class,ObPool::getId,ObOutPoolItem::getPoolId)  //左连接表1,条件为表1库存id 等于 表2 id
         .leftJoin(ObOutPool.class,ObOutPool::getDrawNumber,ObOutPoolItem::getDrawNumber);  //左连接表2,用订单号匹配
  return mpj;
 }
 /**
  * 根据需求模糊生成条件
  *后期生成泛型使用类替代此方法
  */
 private MPJLambdaWrapper<ObOutPoolDto> mpjLambdaWrapper(
   MPJLambdaWrapper<ObOutPoolDto> mpj, ObOutPoolItem obOutPoolItem,String[] createdTimeArr,String... status
 ){

  //模糊查询部门
  if(!(obOutPoolItem.getDepartment()==null)){
   mpj.likeRight(ObOutPoolItem::getDepartment,
     obOutPoolItem.getDepartment());
  }
  // 模糊匹配时间
  if(createdTimeArr != null && createdTimeArr.length > 0 && createdTimeArr[0].length() > 0){
   mpj.ge(ObOutPoolItem::getCreatedTime,createdTimeArr[0])
     .le(ObOutPoolItem::getCreatedTime,createdTimeArr[1]);
  }
  //完成与订单号查询
  if(status[0].equals(Signal.FINISH.name)){
              mpj.eq(ObOutPool::getTaskStatus, TaskStatusEnum.COMPLETED.getStatus());  //完成条件
  }else if (status[0].equals(Signal.ORDER.name)){
   mpj.eq(ObOutPool::getDrawNumber,status[1]);
  }
  return mpj;
 }

}

3.接口返回结果

10、mybatis-plus-join更多使用方法请参看Gitee

https://gitee.com/best_handsome/mybatis-plus-join

来源:blog.csdn.net/qq_44140499/article/details/116517163

相关推荐

redis的八种使用场景

前言:redis是我们工作开发中,经常要打交道的,下面对redis的使用场景做总结介绍也是对redis举报的功能做梳理。缓存Redis最常见的用途是作为缓存,用于加速应用程序的响应速度。...

基于Redis的3种分布式ID生成策略

在分布式系统设计中,全局唯一ID是一个基础而关键的组件。随着业务规模扩大和系统架构向微服务演进,传统的单机自增ID已无法满足需求。高并发、高可用的分布式ID生成方案成为构建可靠分布式系统的必要条件。R...

基于OpenWrt系统路由器的模式切换与网页设计

摘要:目前商用WiFi路由器已应用到多个领域,商家通过给用户提供一个稳定免费WiFi热点达到吸引客户、提升服务的目标。传统路由器自带的Luci界面提供了工厂模式的Web界面,用户可通过该界面配置路...

这篇文章教你看明白 nginx-ingress 控制器

主机nginx一般nginx做主机反向代理(网关)有以下配置...

如何用redis实现注册中心

一句话总结使用Redis实现注册中心:服务注册...

爱可可老师24小时热门分享(2020.5.10)

No1.看自己以前写的代码是种什么体验?No2.DooM-chip!国外网友SylvainLefebvre自制的无CPU、无操作码、无指令计数器...No3.我认为CS学位可以更好,如...

Apportable:拯救程序员,IOS一秒变安卓

摘要:还在为了跨平台使用cocos2d-x吗,拯救objc程序员的奇葩来了,ApportableSDK:FreeAndroidsupportforcocos2d-iPhone。App...

JAVA实现超买超卖方案汇总,那个最适合你,一篇文章彻底讲透

以下是几种Java实现超买超卖问题的核心解决方案及代码示例,针对高并发场景下的库存扣减问题:方案一:Redis原子操作+Lua脚本(推荐)//使用Redis+Lua保证原子性publicbo...

3月26日更新 快速施法自动施法可独立设置

2016年3月26日DOTA2有一个79.6MB的更新主要是针对自动施法和快速施法的调整本来内容不多不少朋友都有自动施法和快速施法的困扰英文更新日志一些视觉BUG修复就不翻译了主要翻译自动施...

Redis 是如何提供服务的

在刚刚接触Redis的时候,最想要知道的是一个’setnameJhon’命令到达Redis服务器的时候,它是如何返回’OK’的?里面命令处理的流程如何,具体细节怎么样?你一定有问过自己...

lua _G、_VERSION使用

到这里我们已经把lua基础库中的函数介绍完了,除了函数外基础库中还有两个常量,一个是_G,另一个是_VERSION。_G是基础库本身,指向自己,这个变量很有意思,可以无限引用自己,最后得到的还是自己,...

China&#39;s top diplomat to chair third China-Pacific Island countries foreign ministers&#39; meeting

BEIJING,May21(Xinhua)--ChineseForeignMinisterWangYi,alsoamemberofthePoliticalBureau...

移动工作交流工具Lua推出Insights数据分析产品

Lua是一个适用于各种职业人士的移动交流平台,它在今天推出了一项叫做Insights的全新功能。Insights是一个数据平台,客户可以在上面实时看到员工之间的交流情况,并分析这些情况对公司发展的影响...

Redis 7新武器:用Redis Stack实现向量搜索的极限压测

当传统关系型数据库还在为向量相似度搜索的性能挣扎时,Redis7的RedisStack...

Nginx/OpenResty详解,Nginx Lua编程,重定向与内部子请求

重定向与内部子请求Nginx的rewrite指令不仅可以在Nginx内部的server、location之间进行跳转,还可以进行外部链接的重定向。通过ngx_lua模块的Lua函数除了能实现Nginx...