This commit is contained in:
那些花儿
2025-07-28 10:48:38 +08:00
parent 030cf7cc4c
commit 3b51ed7a51
450 changed files with 82310 additions and 1213 deletions
@@ -1,3 +1,349 @@
//package com.budwk.app.base.sqlCallback;
//
//import cn.hutool.core.util.ObjectUtil;
//import cn.hutool.core.util.ReflectUtil;
//import cn.hutool.core.util.TypeUtil;
//import com.mysql.cj.MysqlType;
//import org.nutz.dao.DaoException;
//import org.nutz.dao.impl.jdbc.BlobValueAdaptor;
//import org.nutz.dao.jdbc.Jdbcs;
//import org.nutz.dao.pager.ResultSetLooping;
//import org.nutz.dao.sql.Sql;
//import org.nutz.dao.sql.SqlCallback;
//import org.nutz.dao.sql.SqlContext;
//import org.nutz.json.Json;
//
//import java.lang.reflect.Field;
//import java.lang.reflect.Type;
//import java.sql.*;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
//
///**
// * VO 查询回调
// * 如果你的字段是json类型 那么也可以根据VO的类型来做映射
// * todo 暂时只支持List<String>
// * @author zhaoxinyu
// */
//public class QueryVoCallback implements SqlCallback {
//
// protected Class<?> clazz;
//
// public QueryVoCallback(Class<?> clazz) {
// this.clazz = clazz;
// }
//
// @Override
// public Object invoke(Connection connection, ResultSet rs, Sql sql) throws SQLException {
// final ResultSetMetaData meta = rs.getMetaData();
//
//
// ResultSetLooping ing = new ResultSetLooping() {
// protected boolean createObject(int index, ResultSet rs, SqlContext context, int rowCount) {
// try {
// Object obj = clazz.getDeclaredConstructor().newInstance();
// int count = meta.getColumnCount();
//
// for (int i = 1; i <= count; ++i) {
// String columnName = meta.getColumnLabel(i);
// int columnType = meta.getColumnType(i);
// String columnTypeName = meta.getColumnTypeName(i);
//
// // 根据列名找到对应的字段
// Field field = getField(clazz, columnName);
//
// if(ObjectUtil.isNull(field)){
// continue;
// }
//
// Object object = null;
//
// try {
// // 特殊类型处理
// if (columnType == Types.TIMESTAMP || columnType == Types.DATE) {
// object = rs.getTimestamp(i);
// // 如果是Date类型且值为Timestamp,转换为Date
// if (object instanceof Timestamp && field.getType() == Date.class) {
// object = new Date(((Timestamp) object).getTime());
// }
// } else if (columnType == Types.BLOB) {
// object = (new BlobValueAdaptor(Jdbcs.getFilePool())).get(rs, columnName);
// } else if (columnType == Types.CLOB) {
// object = rs.getString(i);
// } else if (columnType == Types.LONGVARCHAR || columnType == Types.LONGNVARCHAR) {
// if ("JSON".equalsIgnoreCase(columnTypeName)) {
// object = parseJsonField(rs, i, field);
// } else {
// object = rs.getObject(i);
// }
// } else if (columnType == Types.ARRAY) {
// object = rs.getArray(i);
// } else {
// object = rs.getObject(i);
// }
//
// // 设置字段值
// if (ObjectUtil.isNotNull(object)) {
// ReflectUtil.setFieldValue(obj, field, object);
// }
//
// } catch (Exception e) {
// // 记录警告日志,但不中断处理
// System.err.println("设置字段值失败: 字段=" + field.getName() + ", 列=" + columnName + ", 错误=" + e.getMessage());
// continue;
// }
//
//// if (columnType == 93 || columnType == 91) {
//// object = rs.getTimestamp(i);
//// } else if (columnType == 2004) {
//// object = (new BlobValueAdaptor(Jdbcs.getFilePool())).get(rs, name);
//// } else if (columnType == 2005) {
//// object = rs.getString(i);
//// } else if (columnType == -1) {
//// if (columnTypeName.equals(MysqlType.JSON.getName())) {
//// try{
//// Class<?> type = field.getType();
////
//// if (type.isAssignableFrom(List.class)) {
//// Type typeArgument = TypeUtil.getTypeArgument(field.getGenericType());
//// if(ObjectUtil.isNotNull(rs.getString(i))){
//// object = Json.fromJsonAsList(Class.forName(typeArgument.getTypeName()), rs.getString(i));
//// }
//// }else if(type.isArray()){
//// if(ObjectUtil.isNotNull(rs.getString(i))){
//// object = Json.fromJsonAsArray(type.getComponentType(), rs.getString(i));
//// }
//// }else{
//// object = Json.fromJson(type, rs.getString(i));
//// }
//// }catch (Exception e){
//// e.printStackTrace();
//// }
//// } else {
//// object = rs.getObject(i);
//// }
//// } else {
//// object = rs.getObject(i);
//// }
//
// //生成clazz类型的实例
// if(ObjectUtil.isNotNull(field)){
// ReflectUtil.setFieldValue(obj, field, object);
// }
// }
// list.add(obj);
// } catch (SQLException e) {
// throw new DaoException(e);
// } catch (Exception e){
// throw new RuntimeException(e);
// }
// return true;
// }
// };
// ing.doLoop(rs, sql.getContext());
// return ing.getList();
// }
//
// /**
// * 解析JSON字段
// */
// private Object parseJsonField(ResultSet rs, int columnIndex, Field field) {
// try {
// String jsonString = rs.getString(columnIndex);
// if (ObjectUtil.isNull(jsonString)) {
// return null;
// }
//
// Class<?> fieldType = field.getType();
//
// // List类型处理
// if (fieldType.isAssignableFrom(List.class)) {
// Type genericType = field.getGenericType();
// Type[] typeArguments = ((java.lang.reflect.ParameterizedType) genericType).getActualTypeArguments();
// if (typeArguments.length > 0) {
// Class<?> elementType = (Class<?>) typeArguments[0];
// return Json.fromJsonAsList(elementType, jsonString);
// }
// }
// // 数组类型处理
// else if (fieldType.isArray()) {
// Class<?> componentType = fieldType.getComponentType();
// return Json.fromJsonAsArray(componentType, jsonString);
// }
// // 普通对象处理
// else {
// return Json.fromJson(fieldType, jsonString);
// }
// } catch (Exception e) {
// System.err.println("JSON解析失败: 字段=" + field.getName() + ", 错误=" + e.getMessage());
// }
// return null;
// }
//
// /**
// * 多种方式获取字段,支持不同命名规范
// */
// public static Field getField(Class<?> clazz, String columnName) {
// if (clazz == null || columnName == null || columnName.isEmpty()) {
// return null;
// }
//
// // 1. 直接匹配(完全相同)
// Field field = getFieldDirectly(clazz, columnName);
// if (field != null) {
// return field;
// }
//
// // 2. 驼峰命名匹配
// String camelName = underlineToCamel(columnName);
// field = getFieldDirectly(clazz, camelName);
// if (field != null) {
// return field;
// }
//
// // 3. 下划线命名匹配
// String underlineName = camelToUnderline(columnName);
// field = getFieldDirectly(clazz, underlineName);
// if (field != null) {
// return field;
// }
//
// // 4. 忽略大小写匹配
// field = getFieldIgnoreCase(clazz, columnName);
// if (field != null) {
// return field;
// }
//
// // 5. 驼峰转下划线后忽略大小写匹配
// field = getFieldIgnoreCase(clazz, underlineName);
// if (field != null) {
// return field;
// }
//
// // 6. 下划线转驼峰后忽略大小写匹配
// field = getFieldIgnoreCase(clazz, camelName);
// if (field != null) {
// return field;
// }
//
// return null;
// }
//
// /**
// * 直接获取字段(完全匹配)
// */
// private static Field getFieldDirectly(Class<?> clazz, String fieldName) {
// try {
// return clazz.getDeclaredField(fieldName);
// } catch (NoSuchFieldException e) {
// // 继续向上查找父类
// Class<?> superclass = clazz.getSuperclass();
// if (superclass != null && superclass != Object.class) {
// return getFieldDirectly(superclass, fieldName);
// }
// return null;
// }
// }
//
// /**
// * 忽略大小写获取字段
// */
// private static Field getFieldIgnoreCase(Class<?> clazz, String fieldName) {
// Field[] fields = getAllFields(clazz);
// for (Field field : fields) {
// if (field.getName().equalsIgnoreCase(fieldName)) {
// return field;
// }
// }
//
// // 再尝试驼峰和下划线转换后忽略大小写
// String camelName = underlineToCamel(fieldName);
// String underlineName = camelToUnderline(fieldName);
//
// for (Field field : fields) {
// if (field.getName().equalsIgnoreCase(camelName) ||
// field.getName().equalsIgnoreCase(underlineName)) {
// return field;
// }
// }
//
// return null;
// }
//
// /**
// * 获取类及其父类的所有字段
// */
// private static Field[] getAllFields(Class<?> clazz) {
// List<Field> fieldList = new ArrayList<>();
// Class<?> currentClass = clazz;
//
// while (currentClass != null && currentClass != Object.class) {
// fieldList.addAll(Arrays.asList(currentClass.getDeclaredFields()));
// currentClass = currentClass.getSuperclass();
// }
//
// return fieldList.toArray(new Field[0]);
// }
//
// /**
// * 下划线转驼峰
// */
// public static String underlineToCamel(String underlineStr) {
// if (underlineStr == null || !underlineStr.contains("_")) {
// return underlineStr;
// }
//
// StringBuilder result = new StringBuilder();
// String[] parts = underlineStr.toLowerCase().split("_");
//
// for (int i = 0; i < parts.length; i++) {
// if (i == 0) {
// result.append(parts[i]);
// } else {
// result.append(capitalize(parts[i]));
// }
// }
//
// return result.toString();
// }
//
// /**
// * 驼峰转下划线
// */
// public static String camelToUnderline(String camelStr) {
// if (camelStr == null || camelStr.isEmpty()) {
// return camelStr;
// }
//
// StringBuilder result = new StringBuilder();
// for (int i = 0; i < camelStr.length(); i++) {
// char c = camelStr.charAt(i);
// if (Character.isUpperCase(c)) {
// if (i > 0) {
// result.append("_");
// }
// result.append(Character.toLowerCase(c));
// } else {
// result.append(c);
// }
// }
// return result.toString();
// }
//
// /**
// * 首字母大写
// */
// private static String capitalize(String str) {
// if (str == null || str.isEmpty()) {
// return str;
// }
// return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
// }
//
//}
package com.budwk.app.base.sqlCallback;
import cn.hutool.core.util.ObjectUtil;