mybatis sql注解中加入判断条件
LZ执行的是修改操作,传过来的值是null,就不进行修改,以前用的是xml格式的,现在看一下注解的
test = 填写的是实体类的属性字段
@Update("<script> " + "update training_history set " + "<if test=uid!=null> uid = #{uid},</if>" + "<if test=prepare!=null> prepare=#{prepare} ,</if> " + "<if test=start!=null> start=#{start} ,</if> " + "<if test=end!=null> end=#{end}, </if>" + "<if test=results!=null> results=#{results},</if> " + "id = #{id} where id = #{id} " + "</script>") fun updateById(trainingHistory:TrainingHistory): Int
以上是根据主键id修改记录,由于这里的字段属性都是字符串,所以做null判断,这里根据自己需求改,注意的是 id = #{id},这里的id本来是不需要修改的,只是为了防止前面最后一个条件不满足时导致的sql语句语法错误,是自己的一种解决方式而已
切记,使用script标签后,sql语句里面不能包含 大于(>),小于(<),不等于(<>),解决 将 < 号换成 ‘<’; > 号 换成’>’;