MyBatis-Plus联表查询(Mybatis-Plus-Join)

  1. 简介
    支持连表查询的mybatis-plus,mybatis-plus风格的连表操作提供 wrapper.leftJoin() wrapper.rightJoin()等操作
  2. pom.xml
    
        com.github.yulichang
        mybatis-plus-join-boot-starter
        1.4.4
    
    
    注意: mybatis plus version >= 3.3.0
  3. 代码生成(MybatisX)
    参考:https://blog.csdn.net/tongxin_tongmeng/article/details/128627980
  4. 引入方式
    1.将代码生成的mapper改为继承MPJBaseMapper (必选)
    
    2.将代码生成的service继承MPJBaseService (可选)
    
    3.将代码生成的serviceImpl继承MPJBaseServiceImpl (可选)
    
    注意:需要连接的表都进行代码生成和上述继承类变更。
  5. 项目实战
    1.原始SQL
    SELECT
       bbb.symbol_id,
       aaa.intepn,
       aaa.componen_id,
       ccc.supppn,
       ddd.symbolinctimestamp
    FROM
       librarygraphic bbb
       LEFT JOIN componen aaa ON bbb.componen_id = aaa.componen_id
       LEFT JOIN supppart ccc ON aaa.componen_id = ccc.componen_id
       LEFT JOIN logicsymbol ddd ON bbb.symbol_id = ddd.symbol_id
    WHERE
       bbb.symbol_id IS NOT NULL
       AND aaa.intepn IS NOT NULL
       AND ddd.symbolinctimestamp IS NOT NULL
       AND aaa.grpname='Connector'
       AND aaa.latest = 1
    
    2.视图实体(封装查询结果)
    @Data
    public class LibrarygraphicBo {
    
        private String symbolId;
    
        private String intepn;
    
        private String componenId;
    
        private String supppn;
    
        private String symbolinctimestamp;
    
    }
    
    3.Mybatis-Plus-Join
    MPJLambdaWrapper wrapper = new MPJLambdaWrapper()
            .selectAs(Librarygraphic::getSymbolId, LibrarygraphicBo::getSymbolId)
            .selectAs(Componen::getIntepn, LibrarygraphicBo::getIntepn)
            .selectAs(Componen::getComponenId, LibrarygraphicBo::getComponenId)
            .selectAs(Supppart::getSupppn, LibrarygraphicBo::getSupppn)
            .selectAs(Logicsymbol::getSymbolinctimestamp, LibrarygraphicBo::getSymbolinctimestamp)
            .leftJoin(Componen.class, Componen::getComponenId, Librarygraphic::getComponenId)
            .leftJoin(Supppart.class, Supppart::getComponenId, Componen::getComponenId)
            .leftJoin(Logicsymbol.class, Logicsymbol::getSymbolId, Librarygraphic::getSymbolId)
            .isNotNull(Librarygraphic::getSymbolId)
            .isNotNull(Componen::getIntepn)
            .isNotNull(Logicsymbol::getSymbolinctimestamp)
            .eq(Componen::getGrpname, "Connector")
            .eq(Componen::getLatest, 1);
    
    List boList = librarygraphicMapper.selectJoinList(LibrarygraphicBo.class, wrapper);
    
    注意:最终查询结果封装到了LibrarygraphicBo类型的集合
  6. 在线文档
    https://gitee.com/best_handsome/mybatis-plus-join

你可能感兴趣的:(MyBatis-Plus,mybatis,mybatis-plus,MyBatis-Plus联表)