mybatis-plus联表查询实践

代码联表前提条件:引入依赖 com.github.yulichang.base;

@MapperBean("smart")
public interface SysAuthMapper extends MPJBaseMapper<SysAuth> {}

示例一:三表联表

MPJLambdaWrapper<SysUser> mpjWrapper = new MPJLambdaWrapper<>();
        mpjWrapper.selectAll(SysUser.class)
                .leftJoin(SysUserRole.class, SysUserRole::getUserId, SysUser::getGuid)
                .leftJoin(SysRoleAuth.class,SysRoleAuth::getRoleId,SysUserRole::getRoleId)
                .eq(SysUser::getEnable,1)
                .eq(SysUser::getState, AuditStateEnum.AUDITED.getValue())
                .and(m->m.eq(SysRoleAuth::getAuthId,authId)
                        .or()
                        .eq(SysUser::getUserType, UserTypeEnum.PMU.name()))
                .distinct();
        List<SysUser> sysUsers = sysUserMapper.selectJoinList(SysUser.class, mpjWrapper);```

示例二:自定义联表查询结果

MPJLambdaWrapper<SysAuth> mpjWrapper = new MPJLambdaWrapper<>();
        mpjWrapper.selectAs(SysAuth::getGuid,SysAuthOperationDTO::getAuthId)
                .selectAs(SysOperation::getGuid,SysAuthOperationDTO::getOperationId)
                .selectAs(SysOperation::getOperationId,SysAuthOperationDTO::getOperationSign)
                .leftJoin(SysAuthOperation.class,SysAuthOperation::getAuthId,SysAuth::getGuid)
                .leftJoin(SysOperation.class,SysOperation::getGuid,SysAuthOperation::getOperationId);
        List<SysAuthOperationDTO> sysAuthOperations = sysAuthMapper.selectJoinList(SysAuthOperationDTO.class, mpjWrapper);

示例三:groupBy和having的使用(副表in的数据都要包含在查询结果)

 MPJLambdaWrapper<SysUser> wrapper = new MPJLambdaWrapper<>();
 if(!CollectionUtil.isEmpty(userVo.getRoleIds())){
            //根据角色id条件查询
            wrapper.leftJoin(SysUserRole.class, SysUserRole::getUserId, SysUser::getGuid)
                    .in(SysUserRole::getRoleId,userVo.getRoleIds())
                    .groupBy(SysUser::getAccount)
                    .having("count(*) >="+userVo.getRoleIds().size());
        }
        wrapper.selectAll(SysUser.class)
                .ne(StrUtil.isNotEmpty(guid), SysUser::getGuid, guid)
                .ne(StrUtil.isNotEmpty(adminGuid), SysUser::getGuid, adminGuid)
                .like(StrUtil.isNotEmpty(userVo.getDeptId()), SysUser::getDeptId, userVo.getDeptId())
                .like(StrUtil.isNotEmpty(userVo.getAccount()), SysUser::getAccount, userVo.getAccount())
                .like(StrUtil.isNotEmpty(userVo.getUserName()), SysUser::getUserName, userVo.getUserName())
                .eq(StrUtil.isNotEmpty(userVo.getUserType()), SysUser::getUserType, userVo.getUserType())
                .eq(StrUtil.isNotEmpty(userVo.getState()), SysUser::getState, userVo.getState())
                .like(StrUtil.isNotEmpty(userVo.getEmail()), SysUser::getEmail, userVo.getEmail())
                .like(StrUtil.isNotEmpty(userVo.getPhoneNumber()), SysUser::getPhoneNumber, userVo.getPhoneNumber())
                .eq(!StringUtils.isNullOrEmpty(userVo.getSex()), SysUser::getSex, userVo.getSex())
                .eq(userVo.getEnable() != null, SysUser::getEnable, userVo.getEnable())
                .eq(SysUser::getDeleted, 0)
                .orderByDesc(SysUser::getCreateTime);
        return wrapper;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值