老师,setCategoryName的方法我和老师的有点小差别,老师帮我看一下这样写是否有问题

来源:7-2 添加分类和更新分类名字功能开发

wendy_z

2018-01-04

老师,setCategoryName的方法我是这么写的,和老师的有点小差别,我倒是看着没有啥问题,老师帮我看一下:

我是根据categoryId进行了判断

public ServerResponse<Category> setCategoryName(Integer categoryId,String categoryName){
   if (categoryId==null || StringUtils.isBlank(categoryName)){
       return ServerResponse.createByErrorMessage("品类id和名称均不能为空");
   }
   Category category = categoryMapper.selectByPrimaryKey(categoryId);
   if(category==null){
       return ServerResponse.createByErrorMessage("此id的品类不存在");
   }

   category.setName(categoryName);
   int resultCount = categoryMapper.updateByPrimaryKey(category);
   if (resultCount>0){
       return ServerResponse.createBySuccessMessage("更新品类名称成功");
   }
   return ServerResponse.createByErrorMessage("更新品类名称失败");
}


老师的是:Category category = new Category();
category.setId(categoryId);
category.setName(categoryName);


还有我发现更新名字这里应该是用selectByPrimaryKey吧,这样才有createtime和updatetime,否则用updateByPrimaryKeySelective方法的话,数据库中更新的数据没有这个时间戳。

写回答

2回答

星辰Iron

2018-01-05

你好同学,像你这种更新方式也是可以的,另外老师的那个updatetime的if判断是需要去掉的,不然无法实时更新updatetime。你可以参考下面这个链接,老师也提及相关内容了,希望能对你有所帮助。

登录状态修改密码,发现updateByPrimaryKeySelective接口存在缺陷


0
1
wendy_z
非常感谢!
2018-01-08
共1条回复

Geely

2018-01-06

你好,同学,仔细看了你的问题,很好的问题。把这两个逻辑细分判断是OK的。如星辰_兼信同学回答的是,这个我们可以优化一下updateByPrimaryKeySelective这个方法里的sql,里面的时间判断是需要优化一下滴


0
3
Geely
回复
wendy_z
的确是的,sql语句越少越好~
2018-01-09
共3条回复

从0开始 独立完成企业级Java电商网站服务端开发

前后端分离,数据库接口设计,架构设计,功能开发,上线运维

9430 学习 · 8789 问题

查看课程