如果自定义xxMapper继承了mybatis-plus的BaseMapper时,xxMapper中不能有insert()方法,因为BaseMapper中就有这个方法。
1.如果对应的xxMapper.xml中有
2.如果对应的xxMapper.xml中没有
① service.insert(object) ,其中object不需要传id,mybatis-plus会自动生成。需要注意的是,如果表里主键是ID,model中id属性不需要加@TableId
,如果表里主键是ORG_ID,model中id属性不需要加@TableId
或者@TableId(type= IdType.ID_WORKER_STR)
表里id是bigint时,model中主键字段是@TableId
表里id是varchar时,model中主键字段是@TableId(type= IdType.ID_WORKER_STR)
② 而继承了BaseMapper的xxMapper,在调用insert(object)时,其中object也不需要传id,mybatis-plus会自动生成
遇到的问题:
①做项目时候,遇到一个insert始终报id为空,检查过了继承了BaseMapper,最后发现是因为BaseMapper
②insert报错:
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails
刚开始以为是插入的字段不全,因为xxMapper.xml中的insert方法是使用工具自动生成的,是将所有的字段都插入而且不会对该字段作判空判断。其实不是因为这个原因,而是数据库表作了一个外键限制,当插入的
BUILD_ID、COMMUNITY_ID或者XIAOQU_ID
字段的值在相关联的表里不存在的时候就会报错。
{
"retCode": {
"status": "9999",
"msg": "\r\n### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`renkou`.`pb_resi_base`, CONSTRAINT `FK_PB_RESI_RELATIONS_PB_SOCI_` FOREIGN KEY (`RESIDENT_ID`) REFERENCES `pb_soci_insurance` (`SOCIAL_INSURANCE_ID`))\r\n### The error may involve com.cicdi.servertemplate.modules.baselibrary.dao.PbResiBaseMapper.insert-Inline\r\n### The error occurred while setting parameters\r\n### SQL: insert into pb_resi_base (RESIDENT_ID, FAMILY_NAME, NAME, EVER_USED_NAME, ENGLISH_NAME, COUNTRY_ID, GENDER_ID, NATIONALITY_ID, NATIVE_PLACE, CARD_TYPE_ID, CARD_NO, BIRTH_DATE, BIRTH_PLACE, CERT_ID, CARD_ISSUE_DT, CERT_EFF_DT, CERT_EXP_DT, CHANGE_DATE, ISSUING_UNIT, MARITAL_STATUS_ID, POLITICAL_AFFILIATION_ID, EDUCATION, HUJI_ID, RESIDENT_FLAG_ID, DWELL_KIND, WORK_ID, GATBZ, IS_DELETE, DATA_SOURCE_ID, LOCAL_POLICE_CODE, ADM_DISTRICT_ID, ADM_STREET_ID, COMMUNITY_ID, XIAOQU_ID, BUILD_ID, SRC_UPDATE_TIME, FG_UPDATE_TIME, UPDATE_TIME, HOUSE_ID, MOBILEPHONE, HUJI_CHARATER, TELEPHONE, ISMOVEOUT, RELATION, DOORADDRESS, JMTYPE, RESIDENCE_ADDR, IS_ALIVE ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )\r\n### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`renkou`.`pb_resi_base`, CONSTRAINT `FK_PB_RESI_RELATIONS_PB_SOCI_` FOREIGN KEY (`RESIDENT_ID`) REFERENCES `pb_soci_insurance` (`SOCIAL_INSURANCE_ID`))\n; SQL []; Cannot add or update a child row: a foreign key constraint fails (`renkou`.`pb_resi_base`, CONSTRAINT `FK_PB_RESI_RELATIONS_PB_SOCI_` FOREIGN KEY (`RESIDENT_ID`) REFERENCES `pb_soci_insurance` (`SOCIAL_INSURANCE_ID`)); nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`renkou`.`pb_resi_base`, CONSTRAINT `FK_PB_RESI_RELATIONS_PB_SOCI_` FOREIGN KEY (`RESIDENT_ID`) REFERENCES `pb_soci_insurance` (`SOCIAL_INSURANCE_ID`))",
"logId": "E1544172824826"
},
"data": null
}