spring-boot-api-project-seed icon indicating copy to clipboard operation
spring-boot-api-project-seed copied to clipboard

生成的代码增删改查建议 返回影响的行数

Open 0000005 opened this issue 7 years ago • 9 comments

0000005 avatar Sep 05 '17 00:09 0000005

我觉得没必要吧,只要没有出现异常,肯定是成功的,你怎么看?

lihengming avatar Sep 05 '17 02:09 lihengming

   public void save(T model) {
        mapper.insertSelective(model);
    }

    public void save(List<T> models) {
        mapper.insertList(models);
    }

    public void deleteById(Integer id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void deleteByIds(String ids) {
        mapper.deleteByIds(ids);
    }

    public void update(T model) {
        mapper.updateByPrimaryKeySelective(model);
    }

你说的是这些吧?Mapper确实是会返回影响的行数,我把它省略了,因为在使用中从来没用到过。

lihengming avatar Sep 05 '17 02:09 lihengming

你这么说好像也有点道理,这就意味着。如果影响的行数为0,那么必定会抛异常?

0000005 avatar Sep 05 '17 02:09 0000005

如果没有异常是正确的,影响行数为0不一定出异常吧。比如按照条件修改的sql.

------------------ 原始邮件 ------------------ 发件人: "JerryYin";[email protected]; 发送时间: 2017年9月5日(星期二) 上午10:12 收件人: "lihengming/spring-boot-api-project-seed"[email protected]; 抄送: "Subscribed"[email protected]; 主题: Re: [lihengming/spring-boot-api-project-seed] 生成的代码增删改查建议 返回影响的行数 (#49)

你这么说好像也有点道理,这就意味着。如果影响的行数为0,那么必定会抛异常?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

lj-example avatar Sep 05 '17 02:09 lj-example

是的,那这个影响行数对调用者来说就有意义了。 调用者可以知道按照某个条件是否 执行成功了。

0000005 avatar Sep 05 '17 02:09 0000005

加上是很容易的,关键在于有多少人会去处理这个返回值?每次调用 Service 的CRUD方法都会去判断一下影响的记录数?

lihengming avatar Sep 05 '17 02:09 lihengming

这个在实际业务中,场景确实有的

michael-ancestor avatar Sep 12 '17 09:09 michael-ancestor

返回影响行数还是很有必要的,可以避免代码中锁的使用,如更新订单状态(update ... set status = 'success' where status = 'wait_paying'),更新成功(影响行数=1)执行后续的操作,否则表示订单已经被更新过,直接return

xiongzhao1217 avatar Mar 07 '18 04:03 xiongzhao1217

更新一个条件不存在的时候就是0吧,感觉是需要拿返回值来作为下一步业务处理的依据

Huptzzz avatar Mar 28 '20 06:03 Huptzzz