Mybatis的 mapper.xml 中 update 语句使用 if 标签判断对像属性是否为空值。

 

  UserDTO是传过来参数的类型,userDTO是在mapperDao接口中给更新方法的参数起的别名。

   mapperDao.java

  int updata(@Param("userDTO") UserDTO userDTO);

mapper.xml

  <update id="updata" parameterType="UserDTO">
  UPDATE
    table u
  <set>
    <if test=" userDTO.age!=null and userDTO.age !='' ">
      u.identity = #{userDTO.age},
    </if>
    <if test=" userDTO.name !=null and userDTO.name !='' ">
      u.name = #{userDTO.name},
    </if>
      </set>
     <where>
            u.id = #{userDTO.id}
    </where>
</update>

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-15
  • 2021-09-20
  • 2022-02-19
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
相关资源
相似解决方案