yulichang / mybatis-plus-join

支持连表查询的mybatis-plus,mybatis-plus风格的连表操作提供wrapper.leftJoin(),wrapper.rightJoin()等操作
https://yulichang.github.io/mybatis-plus-join-doc/
Apache License 2.0
971 stars 113 forks source link

UNION参数错乱 #88

Closed yejiaaqi closed 6 months ago

yejiaaqi commented 6 months ago

当使用.leftJoin(UserAddressDO.class, on -> on .eq(UserAddressDO::getUserId, UserDO::getId) .eq(UserAddressDO::getId, UserDO::getId))多条件是依然会出现参数错乱,不使用不会

yulichang commented 6 months ago

有完整代码吗

yejiaaqi commented 6 months ago

private MPJBaseJoin synthesisWrapper(SynthesisRoomDTO synthesisRoomDTO) { MPJLambdaWrapper friendsWrapper = JoinWrappers.lambda(TRooms.class) .disableSubLogicDel() .leftJoin(TUserFriends.class, on -> on .eq(TUserFriends::getRoomId, TRooms::getRoomId) .eq(TRooms::getType, "3") ) .isNotNull(TUserFriends::getPkId) .eq(TUserFriends::getDeleteFlag, CommonConstant.NO_DELETE_FLAG) .leftJoin(TUsers.class, TUsers::getUserId, TUserFriends::getFriendId) .leftJoin(TMessages.class, TMessages::getMsgId, TRooms::getActiveMsg) .selectAs(TUsers::getAvatarUrl, "avatar") .select("null as name") .selectAs(TUsers::getNickName, "nickname") .select(TUserFriends::getRemarkName) .selectAs(TMessages::getCreatedTime, "activeTime") .selectAs(TMessages::getContent, "msgContent") .selectAs(TMessages::getType, "msgType") .selectAs(TRooms::getType, "type") .selectAs(TRooms::getRoomId, "roomId") .selectAs(TUserFriends::getFriendId, "friendId") .select("null as groupId"); MPJLambdaWrapper groupsWrapper = JoinWrappers.lambda(TRooms.class) .disableSubLogicDel() .leftJoin(TUserGroups.class, on -> on .eq(TUserGroups::getRoomId, TRooms::getRoomId) .eq(TRooms::getType, "3") ) .isNotNull(TUserGroups::getPkId) .eq(TUserGroups::getDeleteFlag, CommonConstant.NO_DELETE_FLAG) .leftJoin(TGroups.class, TGroups::getGroupId, TUserGroups::getGroupId) .leftJoin(TMessages.class, TMessages::getMsgId, TRooms::getActiveMsg) .leftJoin(TUsers.class, TUsers::getUserId, TMessages::getFromUid) .selectAs(TGroups::getAvatarList, "avatar") .selectAs(TGroups::getGroupName, "name") .selectAs(TUsers::getNickName, "nickname") .selectAs(TUserGroups::getRemark, "remarkName") .selectAs(TMessages::getCreatedTime, "activeTime") .selectAs(TMessages::getContent, "msgContent") .selectAs(TMessages::getType, "msgType") .selectAs(TRooms::getType, "type") .selectAs(TRooms::getRoomId, "roomId") .select("null as friendId") .select(TUserGroups::getGroupId); if (null == synthesisRoomDTO) { return friendsWrapper; } if (StrUtil.isNotEmpty(synthesisRoomDTO.getUserId())) { friendsWrapper.eq(TUserFriends::getUserId, synthesisRoomDTO.getUserId()); groupsWrapper.eq(TUserGroups::getUserId, synthesisRoomDTO.getUserId()); } if (ListUtil.isNotEmpty(synthesisRoomDTO.getBlacklist())) { friendsWrapper.notIn(TUserFriends::getUserId, synthesisRoomDTO.getBlacklist()); } friendsWrapper.union(groupsWrapper); return friendsWrapper;

yejiaaqi commented 6 months ago

改成下面就没问题了MPJLambdaWrapper friendsWrapper = JoinWrappers.lambda(TRooms.class) .disableSubLogicDel() .leftJoin(TUserFriends.class, TUserFriends::getRoomId, TRooms::getRoomId) .eq(TRooms::getType, "3") .isNotNull(TUserFriends::getPkId) .eq(TUserFriends::getDeleteFlag, CommonConstant.NO_DELETE_FLAG) .leftJoin(TUsers.class, TUsers::getUserId, TUserFriends::getFriendId) .leftJoin(TMessages.class, TMessages::getMsgId, TRooms::getActiveMsg) .selectAs(TUsers::getAvatarUrl, "avatar") .select("null as name") .selectAs(TUsers::getNickName, "nickname") .select(TUserFriends::getRemarkName) .selectAs(TMessages::getCreatedTime, "activeTime") .selectAs(TMessages::getContent, "msgContent") .selectAs(TMessages::getType, "msgType") .selectAs(TRooms::getType, "type") .selectAs(TRooms::getRoomId, "roomId") .selectAs(TUserFriends::getFriendId, "friendId") .select("null as groupId"); MPJLambdaWrapper groupsWrapper = JoinWrappers.lambda(TRooms.class) .disableSubLogicDel() .leftJoin(TUserGroups.class, TUserGroups::getRoomId, TRooms::getRoomId) .eq(TRooms::getType, "1") .isNotNull(TUserGroups::getPkId) .eq(TUserGroups::getDeleteFlag, CommonConstant.NO_DELETE_FLAG) .leftJoin(TGroups.class, TGroups::getGroupId, TUserGroups::getGroupId) .leftJoin(TMessages.class, TMessages::getMsgId, TRooms::getActiveMsg) .leftJoin(TUsers.class, TUsers::getUserId, TMessages::getFromUid) .selectAs(TGroups::getAvatarList, "avatar") .selectAs(TGroups::getGroupName, "name") .selectAs(TUsers::getNickName, "nickname") .selectAs(TUserGroups::getRemark, "remarkName") .selectAs(TMessages::getCreatedTime, "activeTime") .selectAs(TMessages::getContent, "msgContent") .selectAs(TMessages::getType, "msgType") .selectAs(TRooms::getType, "type") .selectAs(TRooms::getRoomId, "roomId") .select("null as friendId") .select(TUserGroups::getGroupId); if (null == synthesisRoomDTO) { return friendsWrapper; } if (StrUtil.isNotEmpty(synthesisRoomDTO.getUserId())) { friendsWrapper.eq(TUserFriends::getUserId, synthesisRoomDTO.getUserId()); groupsWrapper.eq(TUserGroups::getUserId, synthesisRoomDTO.getUserId()); } if (ListUtil.isNotEmpty(synthesisRoomDTO.getBlacklist())) { friendsWrapper.notIn(TUserFriends::getUserId, synthesisRoomDTO.getBlacklist()); } friendsWrapper.union(groupsWrapper); return friendsWrapper; }

yulichang commented 6 months ago

试一下 1.4.7.1

yejiaaqi commented 6 months ago

好的

yejiaaqi commented 6 months ago

已解决,真棒