报错详情:
java.sql.SQLSyntaxErrorException: Column 'xxx' specified twice
示例:
...
BeanUtils.copyProperties(bo, po);
// 此处故意画蛇添足一个租户id
po.setTenatId = 1000;
if (dutyMapper.insert(po) == 0) {
throw new InvalidSqlOperationException();
}
...
报错提示如下:
...
### The error occurred while setting parameters
### SQL: INSERT INTO sc_duty (duty_name, duty_desc, tenant_id, create_by, create_time, update_by, update_time, tenant_id) VALUES (?, ?, ?, ?, ?, ?, ?, 1000)
### Cause: java.sql.SQLSyntaxErrorException: Column 'tenant_id' specified twice;
...
报错原因:
看其中的SQL:
INSERT INTO sc_duty (duty_name, duty_desc, tenant_id, create_by, create_time, update_by, update_time, tenant_id) VALUES (?, ?, ?, ?, ?, ?, ?, 1000)
其中tenant_id
出现了两次,第一次为上文画蛇添足的结果,第二次,也就是自动设置为1000的,为MyBatisPlus
开启多租户SQL解析器后自动从会话中取得。
解决方法:
将本来由MyBatisPlus
自动进行进行装配的字段设为null
值,或找出导致该字段被赋值的地方进行修改。
上文多余的tenant_id
是自己set的,而实际开发中更多的可能是由某个业务类之间的字段copy导致。