QueryDSL 构建 select 计数查询

QueryDSL build select count query

我想使用 QueryDSL 库构建 select 计数查询,如下所示:

select count(1) from table1

我创建了下一个代码:

SQLTemplates sqlTemplates = builder.printSchema().build();
Configuration configuration = new Configuration(sqlTemplates);
Path table = new RelationalPathBase(Object.class, "alias", "hr", "table1");
StringPath[] columnsPath = new StringPath[1];
columnsPath[0] = Expressions.stringPath(table, "field1");
SQLQuery sqlQuery = new SQLQuery(dataSource.getConnection(), configuration)
                .from(table);
sqlQuery.setUseLiterals(true);
String selectStatement = sqlQuery.getSQL(columnsPath).getSQL();

结果 select下一个语句:

select alias.field1 from hr.table1 alias

能否请人指教如何重写上面的代码以获得

select count(alias.field1) from hr.table1 alias

这还不够吗?

...from(table).count();

将最后一行替换为

String selectStatement = sqlQuery.getSQL(columnsPath[0].count()).getSQL();