我在写批量修改的时候遇到的问题,我这里给了@Param但是还是会出现这个问题。其他代码都没问题
majorMapper.java
List<Major> batchUpdateMajor(@Param("majorList") List<Major> majorList)
majorMapper.xml
<update id="batchUpdateMajor" parameterType="com.guli.collegemanagetrailing.entity.Major">
UPDATE `major`
<trim prefix="SET" suffixOverrides=",">
<if test="major.majorName != null and major.majorName != ''">
`major_name` = #{major.majorName},
</if>
<if test="major.collegeId != null">
`college_id` = #{major.collegeId},
</if>
<if test="major.number != null">
`number` = #{major.number},
</if>
<if test="major.status != null">
`status` = #{major.status},
</if>
</trim>
<where>
<foreach collection="majorList" item="major" separator="OR">
<if test="major.id != null">
`id` = #{major.id}
</if>
</foreach>
</where>
</update>
collegeServiceImpl.java
@Override
@Transactional
public void deleteCollege(Integer id) {
if (NumberUtils.isEmpty(id)) {
throw new CustomException(ResultEnum.notParamError.getCode(),ResultEnum.notParamError.getMessage());
}
College collegeById = collegeMapper.getCollegeById(id);
if (ObjectUtils.isEmpty(collegeById)) {
throw new CustomException(ResultEnum.collegeNotExist.getCode(),ResultEnum.collegeNotExist.getMessage());
}
// 删除学院前先查询有没有专业绑定,如果有专业绑定当前学院则把专业的collegeId改为0
List<Major> majorListById = collegeMapper.getMajorListById(id);
if (!ObjectUtils.isEmpty(majorListById) || !Objects.equals(majorListById.size(),StatusCode.userStatus.getStatus())) {
// 批量修改专业的学院id
for (Major major : majorListById) {
major.setCollegeId(StatusCode.userStatus.getStatus());
}
majorMapper.batchUpdateMajor(majorListById);
}
Integer deleteCollege = collegeMapper.deleteCollege(id);
if (!Objects.equals(deleteCollege,StatusCode.recordCount.getStatus())) {
throw new CustomException(ResultEnum.deleteAllUnSuccessful.getCode(),ResultEnum.deleteUnSuccessful.getMessage());
}
}
你这入参是个list,xml中的parameterType也应该是个list啊,这个xml中的sql并不能自动遍历list,它只是被动接受参数,还是要自己处理的。
得自己遍历list,在遍历中组织sql
【以下回答由 GPT 生成】
根据提供的代码和描述,这个问题出现在使用@Param注解时,参数未找到。给出的代码中,存在一个方法batchUpdateMajor
,该方法接收一个List\<Major>类型的参数majorList
。但是在xml文件中定义的update语句中,指定的参数类型是单个Major对象,而不是List\<Major>对象。所以应该将parameterType修改为List\<Major>。
<update id="batchUpdateMajor" parameterType="java.util.List">
修改majorMapper.xml文件中的parameterType属性为java.util.List
,并且保证List中的元素类型与List声明的泛型类型一致。
<!-- majorMapper.xml -->
<update id="batchUpdateMajor" parameterType="java.util.List">
UPDATE `major`
<trim prefix="SET" suffixOverrides=",">
<if test="major.majorName != null and major.majorName != ''">
`major_name` = #{major.majorName},
</if>
<if test="major.collegeId != null">
`college_id` = #{major.collegeId},
</if>
<if test="major.number != null">
`number` = #{major.number},
</if>
<if test="major.status != null">
`status` = #{major.status},
</if>
</trim>
<where>
<foreach collection="majorList" item="major" separator="OR">
<if test="
这样修改之后,就可以正常使用@Param注解来传递参数了。