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
@@ -85,10 +85,11 @@ public interface BaseService<T> {
/**
* 自定义SQL查询VO对象
*
* @param sql
* @param clazz
* @return 味大无需多盐
* @param <C>
* @return 味大无需多盐
*/
<C> C fetchVO(Sql sql, Class<C> clazz);
@@ -672,7 +673,6 @@ public interface BaseService<T> {
*/
<C> Pagination<C> listPageVO(PageForm pageForm, Sql sql, Class<C> clazz);
/**
* 自定义查询,并返回当前实体类对象
*
@@ -21,6 +21,7 @@ import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.service.EntityService;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -895,15 +896,32 @@ public class BaseServiceImpl<T> extends EntityService<T> implements BaseService<
@Override
public <C> Pagination<C> listPageVO(PageForm pageForm, Sql sql, Class<C> clazz) {
// Integer pageNumber = pageForm.getPageNumber();
// Integer pageSize = pageForm.getPageSize();
// Pager pager = this.dao().createPager(pageNumber, pageSize);
// pager.setRecordCount((int) Daos.queryCount(this.dao(), sql));// 记录数需手动设置
// sql.setPager(pager);
// sql.setCallback(new QueryVoCallback(clazz));
// this.dao().execute(sql);
// List<C> list = sql.getList(clazz);
// return new Pagination<>(pageNumber, pageSize, pager.getRecordCount(), list);
Integer pageNumber = pageForm.getPageNumber();
Integer pageSize = pageForm.getPageSize();
Pager pager = this.dao().createPager(pageNumber, pageSize);
pager.setRecordCount((int) Daos.queryCount(this.dao(), sql));// 记录数需手动设置
sql.setPager(pager);
sql.setCallback(new QueryVoCallback(clazz));
sql.setCallback(Sqls.callback.maps());
this.dao().execute(sql);
List<C> list = sql.getList(clazz);
return new Pagination<>(pageNumber, pageSize, pager.getRecordCount(), list);
List<Map> list = sql.getList(Map.class);
try {
C c = clazz.getDeclaredConstructor().newInstance();
List<C> voList = list.stream().map(map -> BeanUtil.fillBeanWithMapIgnoreCase(map, c, true)).toList();
return new Pagination(pageNumber, pageSize, pager.getRecordCount(), voList);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
@@ -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;
@@ -0,0 +1,184 @@
package com.budwk.app.base.utils;
import java.lang.reflect.Field;
import java.util.*;
/**
* 字段映射工具类
* 支持多种命名规范的字段匹配和转换
*/
public class FieldMappingUtil {
/**
* 多种方式获取字段,支持不同命名规范
* @param clazz 类
* @param columnName 列名或字段名
* @return Field对象,找不到返回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);
if (!camelName.equals(columnName)) {
field = getFieldDirectly(clazz, camelName);
if (field != null) {
return field;
}
}
// 3. 下划线命名匹配
String underlineName = camelToUnderline(columnName);
if (!underlineName.equals(columnName)) {
field = getFieldDirectly(clazz, underlineName);
if (field != null) {
return field;
}
}
// 4. 忽略大小写匹配
field = getFieldIgnoreCase(clazz, columnName);
if (field != null) {
return field;
}
// 5. 驼峰转下划线后忽略大小写匹配
if (!underlineName.equals(columnName)) {
field = getFieldIgnoreCase(clazz, underlineName);
if (field != null) {
return field;
}
}
// 6. 下划线转驼峰后忽略大小写匹配
if (!camelName.equals(columnName)) {
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) {
String fieldNameLower = field.getName().toLowerCase();
if (fieldNameLower.equals(fieldName.toLowerCase()) ||
fieldNameLower.equals(camelName.toLowerCase()) ||
fieldNameLower.equals(underlineName.toLowerCase())) {
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();
}
}
@@ -0,0 +1,59 @@
package com.budwk.app.base.utils;
import cn.hutool.core.util.StrUtil;
import com.budwk.app.sys.models.Sys_menu;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 菜单工具类
*/
public class SysMenuUtil {
/**
* 前端菜单数据
*
* @param menus
* @param parentId
* @return
*/
public static List<Sys_menu> createTreeMenus(List<Sys_menu> menus, String parentId) {
List<Sys_menu> filterMenus = menus.stream().filter(v -> StringUtils.defaultString(v.getParentId(), "").equals(StringUtils.defaultString(parentId, ""))).collect(Collectors.toList());
for (Sys_menu menu : filterMenus) {
List<Sys_menu> childMenus = createTreeMenus(menus, menu.getId());
menu.setChildren(childMenus);
}
return new ArrayList<>(filterMenus);
}
/**
* 获取菜单的根菜单
* @param menus
* @param menu
* @return
*/
public static Sys_menu getRootMenu(List<Sys_menu> menus, Sys_menu menu) {
// 如果当前菜单已经是根菜单(parentId为null),直接返回
if (StrUtil.isBlank(menu.getParentId())) {
return menu;
}
// 查找当前菜单的父菜单
Sys_menu parentMenu = menus.stream()
.filter(m -> m.getId().equals(menu.getParentId()))
.findFirst()
.orElse(null);
// 如果找不到父菜单,说明数据有问题,返回当前菜单
if (parentMenu == null) {
return menu;
}
// 递归查找父菜单的根菜单
return getRootMenu(menus, parentMenu);
}
}