Mybatis-plus-join是网上的大佬为解决Mybatis-plus做多表连接查询吃力而开发的一个工具,它能基于Mybatisplus进行功能升级,使其不再受限于单表查询,其中本人总结有以下几点:
Mybatis-plus-join目前发布在Gitee上,直接将其克隆/下载到本地即可:
下载地址:
https://gitee.com/best_handsome/mybatis-plus-join
注意:需要 Mybatis-plus version >= 3.4.0
下载结果如图:
1.按照下图进行打包操作:
2.打包成功后,jar包存在的目录,找到jar包并复制到自己的项目中:
3.将打出来的jar包引入工程模块中,步骤如图(如果跳出版本问题,则点击更新即可):
在pom.xml中引入该依赖:
<!--mybatis-plus-join--><dependency><groupId>com.github.yulichang</groupId><artifactId>mybatis-plus-join</artifactId><version>1.1.6</version></dependency>
引入效果(检查时提示更新就更新部分环境即可)
1.修改DataScopeSqlInjector中的继承类为:MPJSqlInjector
排除MPJSql注入器的语句
@SpringBootApplication(exclude = {MPJSqlInjector.class})
1.在主启动类中排除MPJ的sql注入器
2.如果自定义了配置类,则在配置类中添加排除
***************************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
使用
继承效果如下
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;@Datapublic 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; //库存idprivate Integer needCount;//出库数量private String person;//申请人private LocalDateTime takeTime;//任务时间private String department;//部门private String updatedBy;//更新人private LocalDateTime updatedTime;//更新时间private Integer id;//idprivate 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*/@Servicepublic 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的不足* 分页查询*/@Overridepublic 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)); //MPJWrapperreturn page1;}/*** 按照订单号查询商品* @param orderNumber* @return*/@Overridepublic 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对象@Overridepublic 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.接口返回结果
https://gitee.com/best_handsome/mybatis-plus-join
来源:blog.csdn.net/qq_44140499/article/details/116517163
本文由梁桂钊于2021-11-22发表在梁桂钊的博客,如有疑问,请联系我们。
本文链接:https://720ui.com/6745.html