mybatis 插入数据获取自增列id的两种方式




	
	
		insert into users(name, password) values(#{name}, #{password})	
	
	
	
	
		insert into users(name, password) values(#{name}, #{password})	
	

	
		
		
		
			select LAST_INSERT_ID()
		
		insert into users(name, password) values(#{name}, #{password})	
	

参考:https://blog.csdn.net/happylife_haha/article/details/51993350

插入数据获取自增Id报错

nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.binding.BindingException: Parameter 'listId' not found. Available parameters are [metricDataRecoveryVo, param1]; nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.binding.BindingException: Parameter 'listId' not found. Available parameters are [metricDataRecoveryVo, param1] error

示例代码:

public void insertMetricDataRecovery(
            @Param("metricDataRecoveryVo") MetricDataRecoveryVo metricDataRecoveryVo);


		insert into ims_metric_data_recovery_list(recovery_date,start_time,end_time,from_source,to_source,id_range,id_list) 
		values(#{metricDataRecoveryVo.recoveryDate},#{metricDataRecoveryVo.startTime},#{metricDataRecoveryVo.endTime},#{metricDataRecoveryVo.fromSource}
		,#{metricDataRecoveryVo.toSource},#{metricDataRecoveryVo.idRange},#{metricDataRecoveryVo.idList})
	

发现是keyPropety设置的问题。改为keyProperty="metricDataRecoveryVo.listId"就可以。我认为这和接口定义了注解有关,如果不用@Param来指定名称,可能就没有问题(待验证)。

参考:https://blog.csdn.net/yanyaming920817/article/details/50393684

你可能感兴趣的:(mybatis 插入数据获取自增列id的两种方式)