前端异步提交数据报错Error getting generated key or setting result to parameter object及解决方法

出问题的功能为:前端用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 id="add" useGeneratedKeys="true" keyProperty="id">
        insert into tb_brand (brand_name, company_name, ordered, description, status)
        values (#{brandName},#{companyName},#{ordered},#{description},#{status});
    </insert>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值