慕桂英3094912
2019-06-03 23:38
/** * @author Mib_du * @creat 2019-06-03-9:55 */ @Service public class ItemServiceImpl implements ItemService { //使用validator校验输入参数 @Autowired private ValidatorImpl validator; @Autowired private ItemDOMapper itemDOMapper; @Autowired private ItemStockDOMapper itemStockDOMapper; //itemmodel->itemdataobject private ItemDO itemDOconvertFromItemModel(ItemModel itemModel) { if(itemModel == null) return null; ItemDO itemDO = new ItemDO(); BeanUtils.copyProperties(itemModel,itemDO); itemDO.setPrice(itemModel.getPrice().doubleValue()); return itemDO; } //itemstockmodel->itemdataobject private ItemStockDO convertFromItemModel(ItemModel itemModel){ if(itemModel == null) return null; ItemStockDO itemStockDO = new ItemStockDO(); itemStockDO.setItemId(itemModel.getId()); itemStockDO.setStock(itemModel.getStock()); return itemStockDO; } @Override @Transactional public ItemModel createItem(ItemModel itemModel) throws BusinessException { //校验入参 ValidationResult result = validator.validate(itemModel); if(result.isHasErrors()){ throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrorMsg()); } //转化itemmodel->dataobject ItemDO itemDO = this.itemDOconvertFromItemModel(itemModel); //写入数据库 itemDOMapper.insertSelective(itemDO); itemModel.setId(itemDO.getId()); ItemStockDO itemStockDO = this.convertFromItemModel(itemModel); itemStockDOMapper.insertSelective(itemStockDO); //返回创建完成的对象 return this.getItemById(itemModel.getId()); } @Override public List<ItemModel> listItem(Integer id) { return null; } @Override public ItemModel getItemById(Integer id) { ItemDO itemDO = itemDOMapper.selectByPrimaryKey(id); if(itemDO == null) return null; //操作获得库存数量 ItemStockDO itemStockDO = itemStockDOMapper.selectByItemId(itemDO.getId()); //将dataobject->model ItemModel itemModel = convertFromDataObject(itemDO,itemStockDO); return itemModel; } //dataobject --> model private ItemModel convertFromDataObject(ItemDO itemDO , ItemStockDO itemStockDO){ ItemModel itemModel = new ItemModel(); BeanUtils.copyProperties(itemDO,itemModel); itemModel.setPrice(new BigDecimal(itemDO.getPrice())); itemModel.setStock(itemStockDO.getStock()); return itemModel; } }
执行到
itemDOMapper.insertSelective(itemDO);
这行就会报错,浏览器会返回下面这个错误信息:
下面这个是报错的时候报错信息:
我调试了一下,发现断点之前的数据都是传进itemDO里了的,但是就是在执行inSertselective(itemDO)这句话时就会报错了。
有哪位大佬能帮我解答下吗 谢谢!!
妈哟我也是这个错误,折腾了一晚上了,谢谢楼主
哎....我加了个try-catch捕获了一下这句话的异常,发现异常信息里输出的是数据库里的sales字段没有初始值,打开数据库一看果然sales字段没有设置默认初始值为0,设置好后再运行,OK,完美~
真是个深刻的教训啊,不会debug找这个小错误找了一晚上.....
前端代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="static/assets/global/plugins/bootstrap/css/bootstrap.min.css"/> <link rel="stylesheet" type="text/css" href="static/assets/global/css/components.css"/> <link rel="stylesheet" type="text/css" href="static/assets/admin/pages/css/login.css"/> <script type="text/javascript" src="static/assets/global/plugins/jquery-1.11.0.min.js"></script> <title></title> </head> <body class="login"> <div class="content"> <h3 class="form-title">创建商品</h3> <div class="form-group"> <label class="control-label">商品名</label> <div> <input class="form-control" type="text" placeholder="商品名" id="title" name="title"/> </div> </div> <div class="form-group"> <label class="control-label">商品描述</label> <div> <input class="form-control" type="text" placeholder="商品描述" id="description" name="description"/> </div> </div> <div class="form-group"> <label class="control-label">价格</label> <div> <input class="form-control" type="text" placeholder="价格" id="price" name="price"/> </div> </div> <div class="form-group"> <label class="control-label">图片</label> <div> <input class="form-control" type="text" placeholder="图片" id="imgUrl" name="imgUrl"/> </div> </div> <div class="form-group"> <label class="control-label">库存</label> <div> <input class="form-control" type="text" placeholder="库存" id="stock" name="stock"/> </div> <div class="form-actions"> <button class="btn blue" id="create" type="submit"> 创建商品 </button> </div> </div> </body> <script> jQuery(document).ready(function(){ //绑定otp的click事件用于向后端发送获取手机号验证码的请求 $("#create").on("click",function(){ var title = $("#title").val(); var description = $("#description").val(); var imgUrl = $("#imgUrl").val(); var price = $("#price").val(); var stock = $("#stock").val(); if(title==null || title==""){ alert("商品名不能为空!"); return false; } if(description==null || description==""){ alert("描述不能为空!"); return false; } if(imgUrl==null || imgUrl==""){ alert("图片不能为空!"); return false; } if(price==null || price==""){ alert("价格不能为空!"); return false; } if(stock==null || stock==""){ alert("库存不能为空!"); return false; } $.ajax({ type:"POST", contenttype:"application/x-www-form-urlencoded", url:"http://localhost:8090/item/create", data:{ "title":title, "description":description, "imgUrl":imgUrl, "price":price, "stock":stock }, xhrFields:{withCredentials:true}, success:function(data){ if(data.status =="success"){ alert("创建成功!"); }else{ alert("创建失败!原因是"+data.data.errMsg); } }, error:function(data){ alert("创建失败!原因为"+data.responseText); } }); return false; }); }); </script> </html>
SpringBoot构建电商基础秒杀项目
49064 学习 · 954 问题
相似问题