简体   繁体   中英

QueryDSL build select count query

I would like to build select count query by using QueryDSL library, something like this:

select count(1) from table1

I created next code:

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();

As result selectStatement is next:

select alias.field1 from hr.table1 alias

Could please someone advice how to rewrite code above to get

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

用最后一行替换

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

这还不够吗?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM