Browse Source

处理PRIMARY_KEY_主键索引情况.

nieqiurong 2 tháng trước cách đây
mục cha
commit
690e56ed0d

+ 14 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/index/AbstractMapperMethodHandler.java

@@ -70,4 +70,18 @@ public abstract class AbstractMapperMethodHandler implements IGenerateMapperMeth
             "    }\n";
     }
 
+    /**
+     * 判断当前索引名称是否为主键索引 (索引名为PRIMARY或PRIMARY开头的为主键索引)
+     *
+     * @param indexName 索引名称
+     * @return 是否为主键索引
+     * @since 3.5.12
+     */
+    public boolean isPrimaryKey(String indexName) {
+        // 有些数据库用的PRIMARY_KEY_7
+        String idxTemp = indexName.toUpperCase();
+        return "PRIMARY".equals(idxTemp)
+            || indexName.startsWith("PRIMARY");
+    }
+
 }

+ 2 - 1
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/index/DefaultGenerateMapperLambdaMethodHandler.java

@@ -77,7 +77,8 @@ public class DefaultGenerateMapperLambdaMethodHandler extends AbstractMapperMeth
             if (this.singleIndex && indexSize > 1) {
                 continue;
             }
-            if ("PRIMARY".equals(indexName) && indexSize == 1) {
+            // use byId
+            if (indexSize == 1 && isPrimaryKey(indexName)) {
                 continue;
             }
             Map<String, TableField> tableFieldMap = tableInfo.getTableFieldMap();

+ 2 - 1
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/index/DefaultGenerateMapperMethodHandler.java

@@ -75,7 +75,8 @@ public class DefaultGenerateMapperMethodHandler extends AbstractMapperMethodHand
             if (this.singleIndex && indexSize > 1) {
                 continue;
             }
-            if ("PRIMARY".equals(indexName) && indexSize == 1) {
+            // use byId
+            if (indexSize == 1 && isPrimaryKey(indexName)) {
                 continue;
             }
             List<TableField> tableFieldList = new ArrayList<>();