伙伴匹配系统踩坑

首先自我介绍,我是一名刚加入星球一个多月的前端同学,今年专升本大四,目标秋招找到一份前端的工作,最近在跟着鱼皮哥做伙伴匹配系统,后端是下载的鱼皮哥仓库里的,然后做到了队伍搜索那里发现搜索数据库中没有的队伍时报sql错误:

Resolved [org.springframework.jdbc.BadSqlGrammarException: <EOL><EOL>### Error querying database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 4<EOL><EOL>### The error may exist in com/yupi/yupao/mapper/UserTeamMapper.java (best guess)<EOL><EOL>### The error may involve defaultParameterMap<EOL><EOL>### The error occurred while setting parameters<EOL><EOL>### SQL: SELECT id,userId,teamId,joinTime,createTime,updateTime,isDelete FROM user_team WHERE isDelete=0 AND (teamId IN ())<EOL><EOL>### Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 4<EOL>; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 4]

然后按着报错进后端找UserTeamMapper.java,这里是写了接口继承了mybatisplus中的BaseMapper没有错误,这我可就犯难了mybatisplus不是不用写sql语句吗哪来的sql报错,我一个前端菜鸡只了解一些基础的java语法,仅仅是把后端跑起来都用了我两天时间,我不会改啊(qaq),然后百度、问gpt都没能解决,急得我抓耳挠腮,我看着这个报错中的sql语句发现and后 teamId IN ()这里为空然后百度一下mybatisplus中 IN空相关问题发现是没有判空导致报错,这就好办了,加上判空( if (CollectionUtils.isNotEmpty(teamList) ) )再试试,果然加上判空就解决了,这个接口的完整代码:

@GetMapping("/list")
public BaseResponse<List<TeamUserVO>> listTeams(TeamQuery teamQuery, HttpServletRequest request) {
if (teamQuery == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
boolean isAdmin = userService.isAdmin(request);
// 1、查询队伍列表
List<TeamUserVO> teamList = teamService.listTeams(teamQuery, isAdmin);
if (CollectionUtils.isNotEmpty(teamList) ) {
final List<Long> teamIdList = teamList.stream().map(TeamUserVO::getId).collect(Collectors.toList());
// 2、判断当前用户是否已加入队伍
QueryWrapper<UserTeam> userTeamQueryWrapper = new QueryWrapper<>();
try {
User loginUser = userService.getLoginUser(request);
userTeamQueryWrapper.eq("userId", loginUser.getId());
userTeamQueryWrapper.in("teamId", teamIdList);
List<UserTeam> userTeamList = userTeamService.list(userTeamQueryWrapper);
// 已加入的队伍 id 集合
Set<Long> hasJoinTeamIdSet = userTeamList.stream().map(UserTeam::getTeamId).collect(Collectors.toSet());
teamList.forEach(team -> {
boolean hasJoin = hasJoinTeamIdSet.contains(team.getId());
team.setHasJoin(hasJoin);
});
} catch (Exception e) {
}
// 3、查询已加入队伍的人数
QueryWrapper<UserTeam> userTeamJoinQueryWrapper = new QueryWrapper<>();
userTeamJoinQueryWrapper.in("teamId", teamIdList);
List<UserTeam> userTeamList = userTeamService.list(userTeamJoinQueryWrapper);
// 队伍 id => 加入这个队伍的用户列表
Map<Long, List<UserTeam>> teamIdUserTeamList = userTeamList.stream().collect(Collectors.groupingBy(UserTeam::getTeamId));
teamList.forEach(team -> team.setHasJoinNum(teamIdUserTeamList.getOrDefault(team.getId(), new ArrayList<>()).size()));
}
return ResultUtils.success(teamList);
}



0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
下载 APP