Another option is to use create a ResultHandler, which is a MyBatis interface that you can pass to the SqlSession#select
method to handle the data coming back from your query.
Here's how you could use it to solve your issue.
First define a PriceResultHandler, something like this:
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
public class PriceResultHandler implements ResultHandler {
// keep a list, assuming you have multiple Price objects to handle
List<Price> lp = new ArrayList<Price>();
@Override
public void handleResult(ResultContext rc) {
// the result object is map with column names mapped to values like so:
// {price1=11.32, qty1=15500, price2=2.62, qty2=234, etc.}
HashMap<?,?> m = (HashMap<?,?>) rc.getResultObject();
Price p = new Price();
double[] prices = new double[]{ ((Number)m.get("price1")).doubleValue(),
((Number)m.get("price2")).doubleValue(),
((Number)m.get("price3")).doubleValue() };
int[] qty = new int[]{ ((Number)m.get("qty1")).intValue(),
((Number)m.get("qty2")).intValue(),
((Number)m.get("qty3")).intValue() };
p.setPrices(prices);
p.setQty(qty);
lp.add(p);
}
public List<Price> getPrices() {
return lp;
}
The reason the ResultHandler receives a HashMap is because you would set your mapping like this:
<select id="getPrices" resultType="hashmap">
SELECT price1, price2, price3, qty1, qty2, qty3
FROM table
WHERE ...
</select>
Then you call it in your Java code like so:
PriceResultHandler rh = new PriceResultHandler();
session.select("getPrices", rh);
// or
session.select("getPrices", arg, rh);
// where arg is a value to pass to the getPrices query mapping
Price p = rh.getPrices().get(0);
This model is roughly the same amount of work as the other option I gave in the other answer. MyBatis is simply mapping the JDBC ResultSet into a Map for you and then you make domain objects with it as you see fit.
One caveat to this method is you have to deal with the case-sensitivity issue of the column names in the maps. You cannot control this via MyBatis. It depends on the behavior of the underlying JDBC driver: https://stackoverflow.com/a/11732674/871012