title: Extremely dynamic queries with MyBatis for remote grid filtering/pagination/sorting
body: <p>A project I'm working on makes heavy use of the ExtJS framework. We have a lot of data to display and we do so via grids, which all have a standard set of paging, sorting, and filtering features.</p>
  
  <p>Currently, our backend is in PHP and we are using straight SQL. I wrote a SQL wrapper in PHP that would accept a query, a list of filters, sort info, and page info and output a query that had the appropriate WHERE, ORDER BY, and pagination features by using the original query as a subquery. I should note that our project is tied to Oracle.</p>
  
  <p>Filters can come in the form of comparisons (=, &lt;, &lt;=, >=, >, IS NULL) or lists ( IN () ) and sorting is simply a single column with a direction. Pagination is start and end rownumbers.</p>
  
  <p>Longish story short, we are moving toward a java-based architecture using Spring and MyBatis. I'm having a hard time figuring out how to implement the same functionality using MyBatis in an easy way. The nice thing about the wrapper was that we only had to write our base query once and then it would morph into whatever query the grid needed. Since we are talking about potentially tens of thousands of records and we need pagination, pulling things back to the server and doing java-based filtering/sorting isn't really an option.</p>
  
  <p>Has anyone solved this (or a similar) problem before? I've seen the Dynamic SQL capabilities of MyBatis (briefly) but it doesn't seem to offer quite the flexibility and power that I need. Am I wrong?</p>
  
  <p>Any guidance from seasoned MyBatis users would be greatly appreciated.</p>
  
  <p>I should note that we are exploratory right now and that using an ORM isn't entirely off the table. Is this something more easily accomplished using, say, Hibernate?</p>