Bootstrap

基于mybatis-plus实现多表联查功能(mybatis-plus-join-boot-starter)

基于mybatis-plus实现多表联查功能(mybatis-plus-join-boot-starter)

使用方法

安装
  • maven
  <dependency>
      <groupId>com.github.yulichang</groupId>
      <artifactId>mybatis-plus-join-boot-starter</artifactId>
      <version>1.3.8</version>
  </dependency>
  • Gradle
   implementation 'com.github.yulichang:mybatis-plus-join-boot-starter:1.3.8'

使用

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

实体类

package com.gremlin.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;

@Data
@TableName("product")
public class OpProduct implements Serializable {

    @TableId(value = "id", type = IdType.ASSIGN_UUID)
    private String id;
    private String type;

}
package com.gremlin.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;

@Data
@TableName("product_info")
public class OpProductInfo implements Serializable {

    @TableId(value = "id", type = IdType.AUTO)
    private String id;

    private String productId;
    private String name;
    private BigDecimal price;
}

配置类

package com.gremlin.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisConfig {
    /**
     * 分页配置
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

返回类

package com.gremlin.vo;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @className: ProductInfoVo
 * @author: gremlin
 * @version: 1.0.0
 * @description:
 * @date: 2022/11/28 10:46
 */
@Data
@Accessors(chain = true)
public class ProductInfoVo {

    private String id;

    private String type;

    private String name;

    private String price;
}

mapper接口

package com.gremlin.mapper;

import com.github.yulichang.base.MPJBaseMapper;
import com.gremlin.entity.OpProductInfo;
import org.apache.ibatis.annotations.Mapper;

/**
 * @className: OpProductInfoMapper
 * @author: gremlin
 * @version: 1.0.0
 * @description:
 * @date: 2022/11/28 10:47
 */
@Mapper
public interface OpProductInfoMapper extends MPJBaseMapper<OpProductInfo> {
}

package com.gremlin.mapper;

import com.github.yulichang.base.MPJBaseMapper;
import com.gremlin.entity.OpProduct;
import org.apache.ibatis.annotations.Mapper;

/**
 * @className: OpProductMapper
 * @author: gremlin
 * @version: 1.0.0
 * @description:
 * @date: 2022/11/28 10:47
 */
@Mapper
public interface OpProductMapper extends MPJBaseMapper<OpProduct> {
}

service接口

package com.gremlin.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.base.MPJBaseService;
import com.gremlin.entity.OpProduct;
import com.gremlin.vo.ProductInfoVo;

import java.util.List;

/**
 * @className: OpProductService
 * @author: gremlin
 * @version: 1.0.0
 * @description:
 * @date: 2022/11/28 10:51
 */
public interface OpProductService extends MPJBaseService<OpProduct> {

    /**
     * lambda表达式查询
     */
    List<ProductInfoVo> queryAllProductLambda();

    /**
     * 普通QueryWrapper
     */
    List<ProductInfoVo> queryAllProduct();

    /**
     * 分页
     */
    IPage<ProductInfoVo> queryPageProduct(Integer pageNo, Integer pageSize);
}

service实现类

package com.gremlin.service.impl;

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.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gremlin.vo.ProductInfoVo;
import com.gremlin.entity.OpProduct;
import com.gremlin.entity.OpProductInfo;
import com.gremlin.mapper.OpProductMapper;
import com.gremlin.service.OpProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: OpProductServiceImpl
 * @author: gremlin
 * @version: 1.0.0
 * @description:
 * @date: 2022/11/28 10:54
 */
@Service
@Slf4j
public class OpProductServiceImpl extends MPJBaseServiceImpl<OpProductMapper,OpProduct> implements OpProductService {

    @Autowired
    private OpProductMapper productMapper;

    /**
     * lambda表达式查询
     */
    @Override
    public List<ProductInfoVo> queryAllProductLambda() {
        MPJLambdaWrapper mpjLambdaWrapper = new MPJLambdaWrapper<ProductInfoVo>()
                //查询表1的全部字段
                .selectAll(OpProductInfo.class)
                //查询表2的全部字段
                // .selectAll(OpProduct.class)
                //起别名
                .select(OpProduct::getId)
                .selectAs(OpProduct::getType,ProductInfoVo::getType)
                // .selectAs(OpProduct::getType,"type")
                //左查询表2条件 为表1的productId = 表2的id
                .leftJoin(OpProductInfo.class, OpProductInfo::getProductId, OpProduct::getId)
                ;
        return productMapper.selectJoinList(ProductInfoVo.class, mpjLambdaWrapper);
    }

    /**
     * 普通QueryWrapper
     */
    @Override
    public List<ProductInfoVo> queryAllProduct() {
        return productMapper.selectJoinList(ProductInfoVo.class,
                new MPJQueryWrapper<OpProduct>()
                        .selectAll(OpProduct.class)
                        .select("t2.id","t2.product_id as productId","t2.name","t2.price")
                        .leftJoin("product_info AS t2 on t2.product_id = t.id")
                        // .eq("t.status", "3")
                        // .orderByAsc()
        );
    }

    /**
     * 分页
     */
    @Override
    public IPage<ProductInfoVo> queryPageProduct(Integer pageNo, Integer pageCount) {
        MPJLambdaWrapper lambdaWrapper = new MPJLambdaWrapper<ProductInfoVo>()
                //查询表1的全部字段
                .selectAll(OpProduct.class)
                //查询表2的全部字段
                .selectAll(OpProductInfo.class)
                //左查询表2条件为表二的productId=表一的id
                .leftJoin(OpProductInfo.class, OpProductInfo::getProductId, OpProduct::getId);
        Page<ProductInfoVo> productInfoVoPage = new Page<>(pageNo, pageCount);
        return productMapper.selectJoinPage(productInfoVoPage,ProductInfoVo.class,lambdaWrapper);
    }
}

sql脚本

-- --------------------------------------------------------
-- 主机:                           127.0.0.1
-- 服务器版本:                        8.0.31 - MySQL Community Server - GPL
-- 服务器操作系统:                      Win64
-- HeidiSQL 版本:                  12.2.0.6576
-- --------------------------------------------------------

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;


-- 导出 mybatis_join 的数据库结构
CREATE DATABASE IF NOT EXISTS `mybatis_join` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `mybatis_join`;

-- 导出  表 mybatis_join.product 结构
CREATE TABLE IF NOT EXISTS `product` (
  `id` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '产品id',
  `type` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '差评系统类型',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

-- 正在导出表  mybatis_join.product 的数据:~2 rows (大约)
DELETE FROM `product`;
INSERT INTO `product` (`id`, `type`) VALUES
	('adjladjlsjldsvldvlkndkscldsncds', 'ios'),
	('fdjdsjfdsjdsjfvdsjjdjfdjdsjcdsvds;\'vd;v', 'Android');

-- 导出  表 mybatis_join.product_info 结构
CREATE TABLE IF NOT EXISTS `product_info` (
  `id` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '产品信息id',
  `product_id` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '产品id',
  `name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '产品名称',
  `price` decimal(10,4) DEFAULT NULL COMMENT '产品价格',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

-- 正在导出表  mybatis_join.product_info 的数据:~2 rows (大约)
DELETE FROM `product_info`;
INSERT INTO `product_info` (`id`, `product_id`, `name`, `price`) VALUES
	('cdscsdlvlsdvl;sdlvlds', 'adjladjlsjldsvldvlkndkscldsncds', '苹果13', 8.0000),
	('fdjdsjfdsjdsjfvdsjjdjfdjdsjcdsdscvds;\'vd;v', 'fdjdsjfdsjdsjfvdsjjdjfdjdsjcdsvds;\'vd;v', '华为', 8.0000),
	('fdjdsjfdsjdsjfvdsjjdjfdjdsjcdsvds', 'fdjdsjfdsjdsjfvdsjjdjfdjdsjcdsvds;\'vd;v', 'OPPO', 4.0000),
	('vsdmv;ldsmv;dsml;vd', 'adjladjlsjldsvldvlkndkscldsncds', '苹果15', 9.0000);

/*!40103 SET TIME_ZONE=IFNULL(@OLD_TIME_ZONE, 'system') */;
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40111 SET SQL_NOTES=IFNULL(@OLD_SQL_NOTES, 1) */;

测试类

package com.gremlin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gremlin.vo.ProductInfoVo;
import com.gremlin.service.OpProductService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
@Slf4j
class MybatisPlusJoinDemoApplicationTests {

    @Autowired
    private OpProductService productService;

    @Test
    void contextLoads() {
        List<ProductInfoVo> productDTOS = productService.queryAllProduct();
        log.info(productDTOS.toString());
    }

    @Test
    void contextLoads1() {
        List<ProductInfoVo> productDTOS = productService.queryAllProductLambda();
        log.info(productDTOS.toString());
    }
    
    @Test
    void contextLoads2() {
        IPage<ProductInfoVo> productInfoVoIPage = productService.queryPageProduct(1, 1);
        List<ProductInfoVo> records = productInfoVoIPage.getRecords();
        log.info(records.toString());
    }

}

demo git 地址

https://gitee.com/fantasy-starry/mybatis-plus-join.git

;