Monday, March 25, 2013

Ibatis queryForList returning a List of String objects

Using an IBatis query that returns mutiple rows from the database, each row can be returned as an object and all these individual objects can be encompassed in a List.

query
<select id="getEmpNames" resultClass="java.lang.String">
        select names from emp_address where dept_id = #value#
    </select>
   
    Java code
    public class EmployeeDaoImpl extends SqlMapClientDaoSupport {
    ...
    ...
        public List<String> getEmpNames(Long deptId) {
       
         List<String> tempList = getSqlMapClientTemplate().queryForList("getEmpNames", deptId);
         return tempList;
         }
     ..
     }
   

For example if the above code snippet returns 5 rows from the database, IBatis internally constructs 5 String objects and returns a List<String>.

No comments: