QueryDSL构建选择计数查询

时间:2015-09-04 10:04:34

标签: java querydsl

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

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

结果是selectStatement是下一个:

select alias.field1 from hr.table1 alias

可以请某人建议如何重写上面的代码来获取

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

1 个答案:

答案 0 :(得分:0)

这还不够吗?

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