基于mybatis-plus实现多表联查功能(mybatis-plus-join-boot-starter)
使用方法
安装
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-boot-starter</artifactId>
<version>1.3.8</version>
</dependency>
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;
@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;
@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;
@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;
public interface OpProductService extends MPJBaseService<OpProduct> {
List<ProductInfoVo> queryAllProductLambda();
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;
@Service
@Slf4j
public class OpProductServiceImpl extends MPJBaseServiceImpl<OpProductMapper,OpProduct> implements OpProductService {
@Autowired
private OpProductMapper productMapper;
@Override
public List<ProductInfoVo> queryAllProductLambda() {
MPJLambdaWrapper mpjLambdaWrapper = new MPJLambdaWrapper<ProductInfoVo>()
.selectAll(OpProductInfo.class)
.select(OpProduct::getId)
.selectAs(OpProduct::getType,ProductInfoVo::getType)
.leftJoin(OpProductInfo.class, OpProductInfo::getProductId, OpProduct::getId)
;
return productMapper.selectJoinList(ProductInfoVo.class, mpjLambdaWrapper);
}
@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")
);
}
@Override
public IPage<ProductInfoVo> queryPageProduct(Integer pageNo, Integer pageCount) {
MPJLambdaWrapper lambdaWrapper = new MPJLambdaWrapper<ProductInfoVo>()
.selectAll(OpProduct.class)
.selectAll(OpProductInfo.class)
.leftJoin(OpProductInfo.class, OpProductInfo::getProductId, OpProduct::getId);
Page<ProductInfoVo> productInfoVoPage = new Page<>(pageNo, pageCount);
return productMapper.selectJoinPage(productInfoVoPage,ProductInfoVo.class,lambdaWrapper);
}
}
sql脚本
;
;
;
;
;
;
;
;
CREATE DATABASE IF NOT EXISTS `mybatis_join` ;
USE `mybatis_join`;
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;
DELETE FROM `product`;
INSERT INTO `product` (`id`, `type`) VALUES
('adjladjlsjldsvldvlkndkscldsncds', 'ios'),
('fdjdsjfdsjdsjfvdsjjdjfdjdsjcdsvds;\'vd;v', 'Android');
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;
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);
;
;
;
;
;
测试类
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