*在mapper.xml中写sql语句错误:更新多个字段时需要判断,加入
语句
<if></if>
<if test="courseId !=null">
:
course_id=#{courseId}
</if> ,
这样来用java语法拼接到sql语句中;要注意sql语句中不能用!=null
来判断非空,要用is not null;
test="courseId 1=null"
为java语法,不要写成了test="course_id != null"
.
另外, 更新多个字段使用<set></set>
,将要更新的字段以及判断语句包裹进去,在外层加上where 对应的条件*
<!--修改课程信息-->
<update id="updateCourseByCourseId" parameterType="Course">
update course
<set>
<if test="**courseName** != null ">
course_name = #{**courseName**},
</if>
<if test="teacherId != null ">
teacher_id = #{teacherId},
</if>
<if test="courseTime != null ">
course_time = #{courseTime},
</if>
<if test="courseRoom != null ">
course_room = #{courseRoom},
</if>
<if test="courseWeek != null ">
course_week = #{courseWeek},
</if>
<if test="courseType != null ">
course_type = #{courseType},
</if>
<if test="collegeId != null ">
college_id = #{collegeId},
</if>
<if test="courseScore != null ">
course_score = #{courseScore},
</if>
</set>
where course_id =#{courseId}
</update>