set
if
dao
int updateStudent(Student student);
mapper
<update id="updateStudent" parameterType="student">
update `student`
<set>
<if test="name != null">
`name` = #{name},
</if>
<if test="age != null">
`age` = #{age},
</if>
<if test="gender != null">
`gender` = #{gender,typeHandler=genderTypeHandler}
</if>
</set>
where `id` = #{id}
</update>
test
@Test
public void test() {
SqlSession session = factory.openSession();
try {
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
int id = 10001;
String name = "hu";
int age = 18;
GenderEnum gender = GenderEnum.getGender(1);
Student student = new Student(id,name,age,gender);
int affectedRows = studentMapper.updateStudent(student);
System.out.println(affectedRows);
System.out.println(studentMapper.selectStudentById(id));
} finally {
session.commit();
session.close();
}
}