set

set标签用于更新语句,当同事要更新多个字段时,我们需要留意当前是否是最后一个set,避免在后面出现,符号,使用set标签后可自动去除最后的逗号

mapper:

<update id="updateProductTest" parameterType="products"> update products <set> <if test="pname != null and pname != ''"> pname = #{pname}, </if> <if test="price != null and price > 0"> price = #{price}, </if> <if test="pdate != null"> pdate = #{pdate}, </if> <if test="cid != null and cid != ''"> cid = #{cid}, </if> </set> where pid = #{pid} </update>

测试代码:

@Test public void updateTest2(){ SqlSession session = factory.openSession(); ProductsMapper mapper = session.getMapper(ProductsMapper.class); //获取已有对象 Products product = mapper.selectProductById(7); product.setPname("云南小土豆"); product.setPrice(10.5f); //执行更新 mapper.updateProductTest(product); System.out.println(product); session.commit(); session.close(); }

 

原文地址:https://www.cnblogs.com/huaobin/p/14162722.html