开发者社区> 问答> 正文

mybaits3.4 返回主键失败(Error getting generate?400报错

在项目中进行新增后,根据返回主键在更新数据,但是使用mybaits的主键返回。直接报错了

 

1.第一种实现方式 ,有人说返回的是一个对象,所以设置keyProperty="对象.字段"

 <insert id="insert" parameterType="cn.com.cathayins.dal.model.DocRecordDO" useGeneratedKeys="true" keyProperty="record.id">
      
    insert into t_doc_record (id, doc_no, product_code, 
      channel_code, schema_code, agreement_print_type, 
      doc_process_cd, doc_inter_method, doc_type_cd, 
      doc_template_code, doc_email_code, doc_path, 
      doc_status_cd, doc_count, gmt_create, is_valid, 
      gmt_modified, tenant_code, doc_xml)
    values (#{id,jdbcType=BIGINT}, #{docNo,jdbcType=VARCHAR}, #{productCode,jdbcType=VARCHAR}, 
      #{channelCode,jdbcType=BIGINT}, #{schemaCode,jdbcType=VARCHAR}, #{agreementPrintType,jdbcType=INTEGER}, 
      #{docProcessCd,jdbcType=TINYINT}, #{docInterMethod,jdbcType=VARCHAR}, #{docTypeCd,jdbcType=TINYINT}, 
      #{docTemplateCode,jdbcType=VARCHAR}, #{docEmailCode,jdbcType=VARCHAR}, #{docPath,jdbcType=VARCHAR}, 
      #{docStatusCd,jdbcType=TINYINT}, #{docCount,jdbcType=INTEGER}, now(), #{isValid,jdbcType=TINYINT}, 
      now(), #{tenantCode,jdbcType=VARCHAR}, #{docXml,jdbcType=LONGVARCHAR})
  </insert>

 

2.第二种实现方式

 <insert id="insert" parameterType="cn.com.cathayins.dal.model.DocRecordDO" useGeneratedKeys="true" keyProperty="id">
      
    insert into t_doc_record (id, doc_no, product_code, 
      channel_code, schema_code, agreement_print_type, 
      doc_process_cd, doc_inter_method, doc_type_cd, 
      doc_template_code, doc_email_code, doc_path, 
      doc_status_cd, doc_count, gmt_create, is_valid, 
      gmt_modified, tenant_code, doc_xml)
    values (#{id,jdbcType=BIGINT}, #{docNo,jdbcType=VARCHAR}, #{productCode,jdbcType=VARCHAR}, 
      #{channelCode,jdbcType=BIGINT}, #{schemaCode,jdbcType=VARCHAR}, #{agreementPrintType,jdbcType=INTEGER}, 
      #{docProcessCd,jdbcType=TINYINT}, #{docInterMethod,jdbcType=VARCHAR}, #{docTypeCd,jdbcType=TINYINT}, 
      #{docTemplateCode,jdbcType=VARCHAR}, #{docEmailCode,jdbcType=VARCHAR}, #{docPath,jdbcType=VARCHAR}, 
      #{docStatusCd,jdbcType=TINYINT}, #{docCount,jdbcType=INTEGER}, now(), #{isValid,jdbcType=TINYINT}, 
      now(), #{tenantCode,jdbcType=VARCHAR}, #{docXml,jdbcType=LONGVARCHAR})
  </insert>

 

3.第三种 

 

 <insert id="insert" parameterType="cn.com.cathayins.dal.model.DocRecordDO">
    <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
          SELECT LAST_INSERT_ID()
      </selectKey> 
      
    insert into t_doc_record (id, doc_no, product_code, 
      channel_code, schema_code, agreement_print_type, 
      doc_process_cd, doc_inter_method, doc_type_cd, 
      doc_template_code, doc_email_code, doc_path, 
      doc_status_cd, doc_count, gmt_create, is_valid, 
      gmt_modified, tenant_code, doc_xml)
    values (#{id,jdbcType=BIGINT}, #{docNo,jdbcType=VARCHAR}, #{productCode,jdbcType=VARCHAR}, 
      #{channelCode,jdbcType=BIGINT}, #{schemaCode,jdbcType=VARCHAR}, #{agreementPrintType,jdbcType=INTEGER}, 
      #{docProcessCd,jdbcType=TINYINT}, #{docInterMethod,jdbcType=VARCHAR}, #{docTypeCd,jdbcType=TINYINT}, 
      #{docTemplateCode,jdbcType=VARCHAR}, #{docEmailCode,jdbcType=VARCHAR}, #{docPath,jdbcType=VARCHAR}, 
      #{docStatusCd,jdbcType=TINYINT}, #{docCount,jdbcType=INTEGER}, now(), #{isValid,jdbcType=TINYINT}, 
      now(), #{tenantCode,jdbcType=VARCHAR}, #{docXml,jdbcType=LONGVARCHAR})
  </insert>

 

错误信息

 Preparing: insert into t_doc_record (id, doc_no, product_code, channel_code, schema_code, agreement_print_type, doc_process_cd, doc_inter_method, doc_type_cd, doc_template_code, doc_email_code, doc_path, doc_status_cd, doc_count, gmt_create, is_valid, gmt_modified, tenant_code, doc_xml) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), ?, now(), ?, ?) 
==> Parameters: null, 005(String), 000(String), null, null, 1(Integer), null, docPrintSyn(String), 1(Integer), 005(String), null, null, 0(Integer), 1(Integer), 1(Integer), null, java.io.StringReader@1555bde(StringReader)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@13673e2]
11:39:20.156 [http-nio-8080-exec-1] ERROR [,] [,,,,,,] [] cn.com.cathayins.component.DocPrintComponent -  ==== INSERT t_doc_record  error ==for org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: java.lang.NullPointerException
 

综上, 1,2直接报错。。3没错,但是对象里面的id值仍为null,没有赋值成功

 

求大神解惑,到底哪方面有问题

 

 

展开
收起
爱吃鱼的程序员 2020-06-06 10:19:10 767 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
                        <p>你确认你的表是自增Id没问题吧??</p>
                    
    
                        <div class="ref">
    

    引用来自“蓝水晶飞机”的评论

    你确认你的表是自增Id没问题吧??

                            不是一般sql语句中的id都不需要insert的吗
                        
    
                        <p>最大问题是在于id不需要insert啊,其次推荐你采用第二种方法,第三种有并发风险</p>
    
    2020-06-06 10:19:29
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载