在项目中进行新增后,根据返回主键在更新数据,但是使用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,没有赋值成功
求大神解惑,到底哪方面有问题
<p>你确认你的表是自增Id没问题吧??</p>
<div class="ref">
你确认你的表是自增Id没问题吧??
不是一般sql语句中的id都不需要insert的吗
<p>最大问题是在于id不需要insert啊,其次推荐你采用第二种方法,第三种有并发风险</p>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。