検索時に select count(*) を同時に実行する機能

次の0.9.10で入れようと思います。

たとえば、SQLファイル(selectByDepartmentId.sql)にはこう書いておきます。

select * from EMPLOYEE where DEPARTMENT_ID = /*departmentId*/0 order by EMPLOYEE_ID

JavaのコードではSelectOptionsのcount()を呼び出してからDaoに渡します。実行後は、getCountSize()で総件数を取得できます。

EmployeeDao dao = new EmployeeDaoImpl();
SelectOptions options = SelectOptions.get().limit(5).offset(3).count();  //count()を呼び出しておく
List<Employee> employees = dao.selectByDepartmentId(10, options);
long countSize = options.getCountSize();  //getCountSize()で総件数がとれる

上のコードを実行すると元のSQLをベースにしてページング用のSQLと select count(*) のSQLが2つ実行されることになります。

select * from EMPLOYEE where DEPARTMENT_ID = 10 order by EMPLOYEE_ID limit 5 offset 3
select count(*) from ( select * from EMPLOYEE where DEPARTMENT_ID = 10)