|
|
@@ -2,6 +2,7 @@ package com.poyee.base.mapper.provider.util;
|
|
|
|
|
|
import com.baomidou.mybatisplus.annotation.TableField;
|
|
|
import com.baomidou.mybatisplus.annotation.TableId;
|
|
|
+import com.baomidou.mybatisplus.annotation.TableName;
|
|
|
import com.poyee.annotation.I18n;
|
|
|
import com.poyee.annotation.db.*;
|
|
|
import com.poyee.base.mapper.provider.BaseProvider;
|
|
|
@@ -61,6 +62,8 @@ public class SelectUtil {
|
|
|
if (Objects.isNull(select)) {
|
|
|
throw new ServiceException("无法定位返回值!");
|
|
|
}
|
|
|
+ parent.req = param;
|
|
|
+ parent.res = select;
|
|
|
if(Objects.equals(parent.dbMethod , SELECT_BY_ID)){
|
|
|
//组装 select by id
|
|
|
return this.checkSelectOneBean(param, select);
|
|
|
@@ -69,7 +72,7 @@ public class SelectUtil {
|
|
|
Class<?> aClass = param.getClass();
|
|
|
// 初始化表信息
|
|
|
TableInfoUtil.init(parent).initTableInfo(aClass);
|
|
|
- parent.superTable = parent.tableInfos.get(aClass.getName());
|
|
|
+ parent.superTable = parent.tableInfos.get(parent.classAlias.get(aClass));
|
|
|
// 获取超类的所有字段处理分页信息
|
|
|
if(parent.isPage) {
|
|
|
long page = 0;
|
|
|
@@ -89,36 +92,8 @@ public class SelectUtil {
|
|
|
}
|
|
|
//设置 分页信息
|
|
|
parent.mPage = new MPage(page, size);
|
|
|
- try {
|
|
|
- //处理排序字段
|
|
|
- Field sidxField = superclass.getDeclaredField("sidx");
|
|
|
- sidxField.setAccessible(true);
|
|
|
- Object sidxObj = sidxField.get(param);
|
|
|
- if(Objects.nonNull(sidxObj) && StringUtils.isNotBlank(String.valueOf(sidxObj))){
|
|
|
- parent.isFrontSort = true;
|
|
|
- Field sortField = superclass.getDeclaredField("sord");
|
|
|
- sortField.setAccessible(true);
|
|
|
- String sort = "asc";
|
|
|
- if(Objects.nonNull(sortField.get(param))){
|
|
|
- sort = sortField.get(param).toString();
|
|
|
- }
|
|
|
- String alias = parent.superTable.getAlias();
|
|
|
- parent.orderByMap.put(Objects.equals(sort,"desc") ? -1 : 0 ,alias +"."+ String.valueOf(sidxObj));
|
|
|
- }
|
|
|
- }catch (NoSuchFieldException | IllegalAccessException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
- //设置 排序信息
|
|
|
- Field[] declaredFields = aClass.getDeclaredFields();
|
|
|
- for (Field declaredField : declaredFields) {
|
|
|
- declaredField.setAccessible(true);
|
|
|
- // 构建 WHERE 条件
|
|
|
- WhereUtil.init(parent).build(param, aClass, parent.whereInfos, declaredField);
|
|
|
- // 构建 LEFT JOIN 信息
|
|
|
- JoinUtil.init(parent).initLeftJoin(aClass, parent.leftJoinInfos, declaredField);
|
|
|
- }
|
|
|
parent.select = select;
|
|
|
return this;
|
|
|
}
|
|
|
@@ -138,7 +113,7 @@ public class SelectUtil {
|
|
|
}
|
|
|
// 初始化表信息
|
|
|
TableInfoUtil.init(parent).initTableInfo(aClass);
|
|
|
- parent.superTable = parent.tableInfos.get(aClass.getName());
|
|
|
+ parent.superTable = parent.tableInfos.get(parent.classAlias.get(aClass));
|
|
|
parent.select = select;
|
|
|
//获取 主键
|
|
|
String column = "id";
|
|
|
@@ -148,7 +123,7 @@ public class SelectUtil {
|
|
|
if(declaredField.isAnnotationPresent(TableId.class)){
|
|
|
TableId tableId = declaredField.getAnnotation(TableId.class);
|
|
|
column = tableId.value();
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
// 构建 LEFT JOIN 信息
|
|
|
JoinUtil.init(parent).initLeftJoin(aClass, parent.leftJoinInfos, declaredField);
|
|
|
}
|
|
|
@@ -191,13 +166,83 @@ public class SelectUtil {
|
|
|
if (!field.isAnnotationPresent(Select.class)
|
|
|
&& (field.isAnnotationPresent(TableField.class) || field.isAnnotationPresent(TableId.class))) {
|
|
|
// 检查字段是否包含TableField注解
|
|
|
- processTableFieldAnnotation(field);
|
|
|
+ processTableFieldAnnotation(clazz, field);
|
|
|
} else if (field.isAnnotationPresent(Select.class)) {
|
|
|
processSelectAnnotation(field);
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.error("初始化选择对象时发生异常", e);
|
|
|
+ } finally {
|
|
|
+ //如果是分页 则处理 order by
|
|
|
+ if (parent.isPage) {
|
|
|
+ Object req = parent.req;
|
|
|
+ Class<?> superclass = req.getClass().getSuperclass();
|
|
|
+ Object res = parent.res;
|
|
|
+ //处理order by
|
|
|
+ //todo 排序需要在最后组装sql 的时候进行解析, 此时所有的实体类均已经做好解析映射
|
|
|
+ try {
|
|
|
+ //处理排序字段
|
|
|
+ Field sidxField = superclass.getDeclaredField("sidx");
|
|
|
+ sidxField.setAccessible(true);
|
|
|
+ Object sidxObj = sidxField.get(req);
|
|
|
+ //如果是 请求端 传排序字段 则 需要 解析 请求参数映射的表结构
|
|
|
+ if(Objects.nonNull(sidxObj) && StringUtils.isNotBlank(String.valueOf(sidxObj))){
|
|
|
+ parent.isFrontSort = true;
|
|
|
+ Field sortField = superclass.getDeclaredField("sord");
|
|
|
+ sortField.setAccessible(true);
|
|
|
+ String sort = "asc";
|
|
|
+ if(Objects.nonNull(sortField.get(req))){
|
|
|
+ sort = sortField.get(req).toString();
|
|
|
+ }
|
|
|
+ //获取返回类的字段的 表的别名
|
|
|
+ Class<?> resClass = res.getClass();
|
|
|
+ Field declaredField = resClass.getDeclaredField(String.valueOf(sidxObj));
|
|
|
+ String alias = parent.superTable.getAlias();
|
|
|
+ if(Objects.nonNull(declaredField)){
|
|
|
+ //是否存在 @Select 注解
|
|
|
+ if(declaredField.isAnnotationPresent(Select.class)){
|
|
|
+ Select selectAnn = declaredField.getAnnotation(Select.class);
|
|
|
+ //如果存在则获取 该注解的table 对应的别名
|
|
|
+ TableInfo tableInfo = parent.tableInfos.get(parent.classAlias.get(selectAnn.table()));
|
|
|
+ if(Objects.nonNull(tableInfo)){
|
|
|
+ alias = tableInfo.getAlias();
|
|
|
+ }
|
|
|
+ }else if(declaredField.isAnnotationPresent(LeftJoin.class)){//存在 leftjoin注解
|
|
|
+ LeftJoin leftJoin = declaredField.getAnnotation(LeftJoin.class);
|
|
|
+ TableInfo tableInfo = parent.tableInfos.get(parent.classAlias.get(leftJoin.table()));
|
|
|
+ if(Objects.nonNull(tableInfo)){
|
|
|
+ alias = tableInfo.getAlias();
|
|
|
+ }
|
|
|
+ }else if(declaredField.isAnnotationPresent(RightJoin.class)){//RightJoin
|
|
|
+ RightJoin rightJoin = declaredField.getAnnotation(RightJoin.class);
|
|
|
+ TableInfo tableInfo = parent.tableInfos.get(parent.classAlias.get(rightJoin.table()));
|
|
|
+ if(Objects.nonNull(tableInfo)){
|
|
|
+ alias = tableInfo.getAlias();
|
|
|
+ }
|
|
|
+ }else if(declaredField.isAnnotationPresent(Join.class)){//join
|
|
|
+ Join join = declaredField.getAnnotation(Join.class);
|
|
|
+ TableInfo tableInfo = parent.tableInfos.get(parent.classAlias.get(join.table()));
|
|
|
+ if(Objects.nonNull(tableInfo)){
|
|
|
+ alias = tableInfo.getAlias();
|
|
|
+ }
|
|
|
+ }else if(declaredField.isAnnotationPresent(TableField.class)){// TableField
|
|
|
+ //如果存在则获取当前 class 的taleName 注解
|
|
|
+ TableName tableName = resClass.getAnnotation(TableName.class);
|
|
|
+ if(Objects.nonNull(tableName) && StringUtils.isNotBlank(tableName.value())){
|
|
|
+ TableInfo tableInfo = parent.tableInfos.get(tableName.value());
|
|
|
+ if(Objects.nonNull(tableInfo)){
|
|
|
+ alias = tableInfo.getAlias();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ parent.orderByMap.put(Objects.equals(sort,"desc") ? -1 : 0 ,alias +"."+ String.valueOf(sidxObj));
|
|
|
+ }
|
|
|
+ }catch (NoSuchFieldException | IllegalAccessException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -206,10 +251,22 @@ public class SelectUtil {
|
|
|
*
|
|
|
* @param field 字段对象
|
|
|
*/
|
|
|
- private void processTableFieldAnnotation(Field field) {
|
|
|
+ private void processTableFieldAnnotation(Class<?> clazz ,Field field) {
|
|
|
try {
|
|
|
+
|
|
|
+ TableInfo superTable = null;
|
|
|
+ //获取表信息
|
|
|
+ String tableName = parent.classAlias.get(clazz);
|
|
|
+ if(StringUtils.isBlank(tableName)){
|
|
|
+ superTable = TableInfoUtil.init(parent).initTableInfo(clazz);
|
|
|
+ } else {
|
|
|
+ superTable = parent.tableInfos.get(tableName);
|
|
|
+ }
|
|
|
+// if(Objects.isNull(superTable)){
|
|
|
+// superTable = parent.superTable;
|
|
|
+// }
|
|
|
+
|
|
|
boolean isDistinct = field.isAnnotationPresent(Distinct.class);
|
|
|
- TableInfo superTable = parent.superTable;
|
|
|
TableId tableId = field.getAnnotation(TableId.class);
|
|
|
if (Objects.nonNull(tableId)) {
|
|
|
String selectSql = "";
|
|
|
@@ -330,20 +387,26 @@ public class SelectUtil {
|
|
|
*/
|
|
|
private void processSelectAnnotation(Field field) {
|
|
|
try {
|
|
|
+
|
|
|
+
|
|
|
Select selectAnnotation = field.getAnnotation(Select.class);
|
|
|
boolean isDistinct = field.isAnnotationPresent(Distinct.class) || selectAnnotation.distinct() ;
|
|
|
if (Objects.nonNull(selectAnnotation.table())) {
|
|
|
- String tableName = selectAnnotation.table()
|
|
|
- .getName();
|
|
|
- TableInfo tableInfo = parent.tableInfos.get(tableName);
|
|
|
+// String tableName = selectAnnotation.table().getName();
|
|
|
+ String tableNames = parent.classAlias.get(selectAnnotation.table());
|
|
|
+ if(StringUtils.isBlank(tableNames) ){
|
|
|
+ TableInfoUtil.init(parent).initTableInfo(selectAnnotation.table());
|
|
|
+ tableNames = parent.classAlias.get(selectAnnotation.table());
|
|
|
+ }
|
|
|
+ TableInfo tableInfo = parent.tableInfos.get(tableNames);
|
|
|
if(Objects.isNull(tableInfo)){
|
|
|
//如果未查到 但有别名则使用别名进行匹配
|
|
|
String alias = selectAnnotation.alias();
|
|
|
if(StringUtils.isNotBlank(alias)){
|
|
|
tableInfo = TableInfoUtil.init(parent).initTableInfoByAlias(selectAnnotation.table(), alias);
|
|
|
} else {
|
|
|
- TableInfoUtil.init(parent).initTableInfo(selectAnnotation.table());
|
|
|
- tableInfo = parent.tableInfos.get(tableName);
|
|
|
+ tableInfo = TableInfoUtil.init(parent).initTableInfo(selectAnnotation.table());
|
|
|
+// tableInfo = parent.tableInfos.get(tableNames);
|
|
|
}
|
|
|
}
|
|
|
if (Objects.nonNull(tableInfo)) {
|
|
|
@@ -357,7 +420,7 @@ public class SelectUtil {
|
|
|
checkSelectSql(selectSql, field, tableInfo, selectAnnotation.fieldName());
|
|
|
// parent.selectColumns.add(selectSql);
|
|
|
} else {
|
|
|
- log.warn("未找到表名 {} 对应的 TableInfo 对象", tableName);
|
|
|
+ log.warn("未找到表名 {} 对应的 TableInfo 对象", tableNames);
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|