Get records by Ids in
The following example shows getting a list of student records given their ids.
The attribute parameterClass="list" allows us to pass
list of student ids as parameter to the query which would substitute
#value[]# in the SQL.
The iterate tag builds
a comma separated list of student ids to construct the where in clause of SQL.
package com.bethecoder.tutorials.ibatis.tests.basic;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import com.bethecoder.tutorials.ibatis.common.Student;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class GetByIdIn {
/**
* @param args
* @throws IOException
* @throws SQLException
*/
public static void main ( String [] args ) throws IOException, SQLException {
Reader reader = Resources.getResourceAsReader ( "SqlMapConfig.xml" ) ;
SqlMapClient sqlMapClent = SqlMapClientBuilder.buildSqlMapClient ( reader ) ;
List<Integer> studentIds = Arrays.asList ( 1 , 2 , 4 ) ;
List<Student> studs = ( List<Student> )
sqlMapClent.queryForList ( "Student.getByIdsIn" , studentIds ) ;
for ( Student stud : studs ) {
System.out.println ( stud ) ;
}
}
}
It gives the following output,
Student[1, Jim, Attic, 32, Painting, +919999999999]
Student[2, Raj, Kumar, 18, Reading books, +914444488888]
Student[4, Arjun, Mishra, 28, Football, +917777777777]