欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

前端异步提交数据报错Errorgettinggeneratedkeyorsettingresulttoparameterobject及解决方法

时间:2023-06-09
出问题的功能为:前端用ajax(axious)异步添加品牌数据报错

### Error updating database、 Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object、Cause: org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id' in 'java.lang.Class'.### The error may exist in com/example/mapper/BrandMapper.xml### The error may involve defaultParameterMap### The error occurred while setting parameters### SQL: insert into tb_brand (brand_name, company_name, ordered, description, status) values (?,?,?,?,?);### Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object、Cause: org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id' in 'java.lang.Class'.

后台添加代码没问题(注:利用自增,没setId)

BrandService brandService = new BrandService(); Brand brand = new Brand(); brand.setBrandName("波导"); brand.setCompanyName("波导手机"); brand.setOrdered(103); brand.setDescription("手机中的战斗机"); brand.setStatus(0); brandService.addBrand(brand); [DEBUG] 21:16:35.469 [main] c.e.m.B.add - ==> Preparing: insert into tb_brand (brand_name, company_name, ordered, description, status) values (?,?,?,?,?);

前端提交Bean报错,最后发现为模型传递错误

brand: { status: '', companyName: '', brandName: '', id:'', ordered:'', description:'' },

这里id设置为"",才导致参数传递错误 id:"" 应该为这样,将id设为null

brand: { status: '', companyName: '', brandName: '', id:null, ordered:'', description:'' },

就不会报错了 总结id:"",就会把空字符串最终传递给mapper.xml,错误接收id属性,报这样的错误:Error getting generated key

insert into tb_brand (brand_name, company_name, ordered, description, status) values (#{brandName},#{companyName},#{ordered},#{description},#{status});

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。