当集成mybatisplus-plus
<dependency>
<groupId>com.github.jeffreyning</groupId>
<artifactId>mybatisplus-plus</artifactId>
<version>1.5.1-RELEASE</version>
</dependency>
且联合主键中有一个字段名称为“id”时
@MppMultiId
@TableField
private String id;
@MppMultiId
@TableField
private String roleId;
调用selectByMultiId方法时会报错:
java.lang.RuntimeException: not found column for id
com.github.jeffreyning.mybatisplus.base.SelectByMultiIdMethod.getCol(SelectByMultiIdMethod.java:32)
com.github.jeffreyning.mybatisplus.base.SelectByMultiIdMethod.createWhere(SelectByMultiIdMethod.java:42)
com.github.jeffreyning.mybatisplus.base.SelectByMultiIdMethod.injectMappedStatement(SelectByMultiIdMethod.java:64)
com.baomidou.mybatisplus.core.injector.AbstractMethod.inject(AbstractMethod.java:67)
//com/baomidou/mybatisplus/core/metadata/TableInfoHelper.class
private static boolean initTableIdWithoutAnnotation(DbConfig dbConfig, TableInfo tableInfo, Field field) {
String property = field.getName();
if ("id".equalsIgnoreCase(property)) { // 对id字段做特殊处理
if (field.getAnnotation(TableField.class) != null) {
logger.warn(String.format("This \"%s\" is the table primary key by default name for `id` in Class: \"%s\",So @TableField will not work!", property, tableInfo.getEntityType().getName()));
}
String column = property;
if (dbConfig.isCapitalMode()) {
column = property.toUpperCase();
}
Class<?> keyType = tableInfo.getReflector().getGetterType(property);
if (keyType.isPrimitive()) {
logger.warn(String.format("This primary key of \"%s\" is primitive !不建议如此请使用包装类 in Class: \"%s\"", property, tableInfo.getEntityType().getName()));
}
tableInfo.setKeyRelated(checkRelated(tableInfo.isUnderCamel(), property, column)).setIdType(dbConfig.getIdType()).setKeyColumn(column).setKeyProperty(property).setKeyType(keyType);
return true;
} else {
return false;
}
}
// com/github/jeffreyning/mybatisplus/base/SelectByMultiIdMethod
private String createWhere(Class<?> modelClass, TableInfo tableInfo) {
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
Field[] fieldArray = modelClass.getDeclaredFields();
Map<String, String> idMap = new HashMap();
Field[] var6 = fieldArray;
int var7 = fieldArray.length;
for(int var8 = 0; var8 < var7; ++var8) {
Field field = var6[var8];
MppMultiId mppMultiId = (MppMultiId)field.getAnnotation(MppMultiId.class);
if (mppMultiId != null) {
String attrName = field.getName();
String colName = this.getCol(fieldList, attrName); // 从FieldList中查字段
idMap.put(attrName, colName);
}
}
if (idMap.isEmpty()) {
logger.info("entity {} not contain MppMultiId anno", modelClass.getName());
return null;
} else {
StringBuilder sb = new StringBuilder("");
idMap.forEach((attrNamex, colNamex) -> {
if (sb.length() > 0) {
sb.append(" and ");
}
sb.append(colNamex).append("=").append("#{").append(attrNamex).append("}");
});
return sb.toString();
}
}
方案1:实体类的联合主键字段不要使用“Id”,换成其他的
@MppMultiId
@TableField("id") // 属性名换掉,指定数据表列名
private String userId;
@MppMultiId
@TableField
private String roleId;
方案2:在无法修改实体类(实例类自动生成出来的)情况下则需要重写TableInfoHelper类,考虑MppMultiId的情况:
属性名称为“id” 且没有MppMultiId注解 且 实体类上没有TableId注解 ,才将id设置为主键 .
private static boolean initTableIdWithoutAnnotation(DbConfig dbConfig, TableInfo tableInfo, Field field) {
String property = field.getName();
// 属性名称为“id” 且没有MppMultiId注解 且 实体类上没有TableId注解 ,才将id设置为主键 .
if ("id".equalsIgnoreCase(property) && field.getAnnotation(MppMultiId.class) == null) {
if (field.getAnnotation(TableField.class) != null) {
logger.warn(String.format("This \"%s\" is the table primary key by default name for `id` in Class: \"%s\",So @TableField will not work!", property, tableInfo.getEntityType().getName()));
}
String column = property;
if (dbConfig.isCapitalMode()) {
column = property.toUpperCase();
}
Class<?> keyType = tableInfo.getReflector().getGetterType(property);
if (keyType.isPrimitive()) {
logger.warn(String.format("This primary key of \"%s\" is primitive !不建议如此请使用包装类 in Class: \"%s\"", property, tableInfo.getEntityType().getName()));
}
tableInfo.setKeyRelated(checkRelated(tableInfo.isUnderCamel(), property, column)).setIdType(dbConfig.getIdType()).setKeyColumn(column).setKeyProperty(property).setKeyType(keyType);
return true;
} else {
return false;
}
}