diff --git a/bean-searcher/src/main/java/com/ejlchina/searcher/implement/DefaultSqlResolver.java b/bean-searcher/src/main/java/com/ejlchina/searcher/implement/DefaultSqlResolver.java index 3564f872794c3eaf1ef08cecdc0771e5ee6036c1..1cdf9cc3c27c74a0d35b19c44f76470ae01c3bd1 100644 --- a/bean-searcher/src/main/java/com/ejlchina/searcher/implement/DefaultSqlResolver.java +++ b/bean-searcher/src/main/java/com/ejlchina/searcher/implement/DefaultSqlResolver.java @@ -52,7 +52,7 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { } Map paraMap = searchParam.getParaMap(); SqlWrapper fieldSelectSqlWrapper = buildFieldSelectSql(beanMeta, fetchFields, paraMap); - SqlWrapper fromWhereSqlWrapper = buildFromWhereSql(beanMeta, searchParam.getParamsGroup(), paraMap); + SqlWrapper fromWhereSqlWrapper = buildFromWhereSql(beanMeta, searchParam, paraMap); String fieldSelectSql = fieldSelectSqlWrapper.getSql(); String fromWhereSql = fromWhereSqlWrapper.getSql(); @@ -134,7 +134,8 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { return sqlWrapper; } - protected SqlWrapper buildFromWhereSql(BeanMeta beanMeta, Group> paramsGroup, Map paraMap) { + protected SqlWrapper buildFromWhereSql(BeanMeta beanMeta, SearchParam searchParam, Map paraMap) { + Group> paramsGroup = searchParam.getParamsGroup(); SqlWrapper sqlWrapper = new SqlWrapper<>(); SqlWrapper tableSql = resolveTableSql(beanMeta.getTableSnippet(), paraMap); sqlWrapper.addParas(tableSql.getParas()); @@ -159,7 +160,7 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { } } if (hasWhereParams) { - useGroup(whereGroup, beanMeta, paraMap, builder, sqlWrapper.getParas()); + useGroup(whereGroup, beanMeta, searchParam, paraMap, builder, sqlWrapper.getParas(), false); } } if (groupBy != null) { @@ -181,7 +182,7 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { } } if (hasHavingParams) { - useGroup(havingGroup, beanMeta, paraMap, builder, sqlWrapper.getParas()); + useGroup(havingGroup, beanMeta, searchParam, paraMap, builder, sqlWrapper.getParas(), true); } } } @@ -189,8 +190,8 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { return sqlWrapper; } - protected void useGroup(Group> group, BeanMeta beanMeta, Map paraMap, - StringBuilder sqlBuilder, List paraReceiver) { + protected void useGroup(Group> group, BeanMeta beanMeta, SearchParam searchParam, Map paraMap, + StringBuilder sqlBuilder, List paraReceiver, boolean isHaving) { group.forEach(event -> { if (event.isGroupStart()) { sqlBuilder.append("("); @@ -220,7 +221,14 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { (name) -> { String field = name != null ? name : param.getName(); FieldMeta meta = beanMeta.requireFieldMeta(field); - return resolveDbFieldSql(meta.getFieldSql(), paraMap); + SqlSnippet sql = meta.getFieldSql(); + //如果是 group by having 且查询中存在该字段,则使用该字段别名 + if (isHaving && searchParam.getFetchFields().contains(field)) { + SqlSnippet sqlSnippet = new SqlSnippet(); + sqlSnippet.setSql(meta.getDbAlias()); + sql = sqlSnippet; + } + return resolveDbFieldSql(sql, paraMap); }, param.isIgnoreCase(), param.getValues() @@ -231,7 +239,6 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { } }); } - protected SqlWrapper resolveGroupBy(BeanMeta beanMeta, Map paraMap) { String groupBy = beanMeta.getGroupBy(); if (StringUtils.isNotBlank(groupBy)) {