<update parameterType="Student">
update students
<set>
<if test="name != null">name=#{name},</if>
<if test="email != null">email=#{email},</if>
<if test="phone != null">phone=#{phone},</if>
</set>
where stud_id=#{studId}
</update>
这里,如果<if>条件返回了任何文本内容,<set>将会插入set关键字和其文本内容,并且会剔除将末尾的逗号","。
测试方法:
@Test
public void test_updateStudent(){
SqlSession sqlSession = null;
try {
sqlSession = MyBatisSqlSessionFactory.openSession();
DynamicSqlMapper mapper = sqlSession.getMapper(DynamicSqlMapper.class);
Student student = new Student();
student.setStudId(45);
student.setEmail("xx@linuxidc.com");
mapper.updateStudent(student);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx