| | |
| | | // column, temp[0], cls != null ? cls.getSimpleName() : "null", requestInfo); |
| | | continue; |
| | | } |
| | | // 检查实体类字段的 @TableField 注解,如果字段是保留关键字,使用注解中指定的列名(带反引号) |
| | | String dbColumn = getDbColumnName(column); |
| | | boolean asc = temp.length == 1 || !temp[temp.length - 1].toLowerCase().equals(ORDER_DESC_VALUE); |
| | | orders.add(new OrderItem(column, asc)); |
| | | orders.add(new OrderItem(dbColumn, asc)); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取数据库列名,如果是保留关键字则添加反引号 |
| | | * @param fieldName 字段名(下划线格式) |
| | | * @return 数据库列名 |
| | | */ |
| | | private String getDbColumnName(String fieldName) { |
| | | if (cls == null) { |
| | | // 如果没有实体类,检查是否是常见的保留关键字 |
| | | return wrapReservedKeyword(fieldName); |
| | | } |
| | | |
| | | // 查找实体类中对应的字段 |
| | | for (Field field : Cools.getAllFields(cls)) { |
| | | String column = Utils.toSymbolCase(field.getName(), '_'); |
| | | if (column.equals(fieldName)) { |
| | | // 检查是否有 @TableField 注解 |
| | | if (field.isAnnotationPresent(TableField.class)) { |
| | | TableField annotation = field.getAnnotation(TableField.class); |
| | | String value = annotation.value(); |
| | | // 如果注解值不为空,使用注解值(可能已经包含反引号) |
| | | if (!Cools.isEmpty(value) && !value.equals(fieldName)) { |
| | | return value; |
| | | } |
| | | } |
| | | // 如果没有注解或注解值为空,检查是否是保留关键字 |
| | | return wrapReservedKeyword(fieldName); |
| | | } |
| | | } |
| | | |
| | | // 如果找不到字段,检查是否是保留关键字 |
| | | return wrapReservedKeyword(fieldName); |
| | | } |
| | | |
| | | /** |
| | | * 如果是 MySQL 保留关键字,则添加反引号 |
| | | * @param column 列名 |
| | | * @return 处理后的列名 |
| | | */ |
| | | private String wrapReservedKeyword(String column) { |
| | | // MySQL 常见保留关键字列表 |
| | | Set<String> reservedKeywords = new HashSet<>(Arrays.asList( |
| | | "row", "type", "length", "channel", "status", "order", "group", "key", "index", |
| | | "table", "database", "user", "select", "insert", "update", "delete", "from", "where" |
| | | )); |
| | | |
| | | if (reservedKeywords.contains(column.toLowerCase())) { |
| | | return "`" + column + "`"; |
| | | } |
| | | return column; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前请求信息(路径和方法) |
| | | * @return 请求信息字符串,格式:HTTP方法 请求路径 |
| | | */ |