多表联查时使用mybatis-plus的LambdaQueryWrapper 多表相连下实现条件查询

MyBatisPlus分页多表关联查询 - SegmentFault 思否

下面的都是单表,如果要在多表联查的基础上使用mybatis-plus提供的LambdaQueryWrapper等查询条件包裹器对象,参看上面的链接

 

总而言之,在service使用LambdaQueryWrapper拼接条件查询后,其中的关键 lambda执行方法需要执行我们自己在mapper接口定义的方法,然后在方法上使用注解引入自定义的sql语句 但是不需要写条件部分 where部分——直接引用LambdaQueryWrapper对象给出的就行

public interface GameQueryMapper extends BaseMapper<GameQuery> {
    String querySql = "SELECT a.*, b.org_id FROM t_game AS a LEFT JOIN t_game_game_org AS b ON b.game_id = a.id ";
    String wrapperSql = "SELECT * from ( " + querySql + " ) AS q ${ew.customSqlSegment}";
    /**
     * 分页查询
     */
    @Select(wrapperSql)
    Page<GameQuery> page(Page page, @Param("ew") Wrapper queryWrapper);

    /**
     * 普通查询
     */
    @Select(wrapperSql)
    List<GameQuery> list(@Param("ew") Wrapper queryWrapper);

    /**
     * 单独查询
     */
    @Select(wrapperSql)
    QyyOrgQuery one(@Param("ew") Wrapper queryWrapper);
}

public interface GameQueryMapper extends { 
String querySq1 
"SELECT b.org_id FROM t_game AS a LEFT JOIN t_game_game.org AS b C 
String wrapperSq1 
"SELECT from ( 
+ querySq1 + 
' ) AS q ${ew.customSq1Segment}",• 
@Se1ect(wrapperSq1) 
Page<GameQuery> page(Page page, wrapper queryldrapper); 
@Se1ect(wrapperSq1) 
Wrapper queryWrapper); 
@Select(wrapperSq1) 
QyyOrgQuery one(@Param("ew") Wrapper queryWrapper) ;

mapper 
1 
List<Emp10yee> selectByGenderAndAge( Short I') String age ) ;


我个人的项目中的实际应用

service接口的方法

public interface ISa1esOrderConnectionService extends BaseService<Sa1esOrderConnection> { 
PageResu1t<Sa1esOrderConnectionVO> 
salesOrderConnectionCriteria)

 

service实现类

@Overrid4 
public PageResu1t<Sa1esOrderConnectionVO> searchPage(Sa1esOrderConnectionCriteria salesOrderConnectionCriteria) { 
mybatis-plu La m bdaQu eryWrap pe 
new . lambda() ; 
LambdaQueryWrapper<Sa1esOrderConnection> lambda 
if (salesOrderConnectionCriteria . getOrderNumber() != null) { 
lambda . eq(Sa1esOrderConnection: : getOrderNumber, salesOrderConnectionCriteria . getOrderNumber()); 
if (salesOrderConnectionCriteria . getOrderType() ! 
null) { 
lambda . eq(Sa1esOrderConnection : : getOrderType , 
salesOrderConnectionCriteria . getOrderType ( ) ) ; 
PageHe1per . startpage( salesOrderConnectionCriteria . getCurrentPage ( ) , 
salesOrderConnectionCriteria . getPageSize ( ) ) 
salesOrderConnectionMapper . list(lambda) ; 
List<Sa1esOrderConnection> salesOrderConnections 
Pagelnfo<Sa1esOrderConnection> pagelnfo 
pagelnfo. getTota1() ; 
long total 
new Pagelnfo<>(sa1esOrderConnections) 
salesOrde rConnectionT ransfer . set ( salesOrderConnections ) 
List<Sa1esOrderConnectionVO> salesOrderConnectionVOS 
return new salesOrderConnectionVOS) ;

 

mapper接口

public interface SalesOrderConnectionMapper extends BaseMapper<Sa1esOrderConnection> { 
String querySqL 
"select sl.*,s3.pay_type,s3.Shipping_method from sales order as sl LEFT JOIN sales _ consignee as s3 on sl.consignee=s3.id" ; 
) AS q ${ew. customSq1Segment}" ; 
String wrapperSqL 
- "SELECT * from 
+ querySqL + 
(querysql) (${ew.cust msqISegment)) 
* @param queryWrappe 
@Se1ect ( ) 
List<Sa1esOrderConnection> " ew") Wrapper queryWrapper) ;

posted @ 2021-10-05 00:00  夏·舍  阅读(16384)  评论(1编辑  收藏  举报