Parameterizing SQL table names in iBatis is slightly
different than parameterizing query values. In this example I'm
passing in a table name and an account number in a HashMap:
Map paramMap = new HashMap(2);
paramMap.put("tablename",
controller.getTableName());
paramMap.put("accountNumber",
accountNumber);
signatureCards =
sqlMap.queryForList( "getSignatureCardsForBrokerAccount",
paramMap );
Note the differences (red) in the
SQL Map file that processes the HashMap. First, a new property,
remapResults, is set to "true" in the Select tag. Secondly,
the table name parameter is surrounded with dollar signs instead
of pound signs like a regular parameter (green).
<select id="getSignatureCardsForBrokerAccount"
resultMap="signatureCardResultMap"
parameterClass="java.util.Map"
remapResults="true" >
SELECT
P1.ITEMID,
P1.ATtrIBUTE00110,
P1.ATtrIBUTE00111,
P1.ATtrIBUTE00113,
P1.ATtrIBUTE00115
FROM $tablename$ P1,
LIBSRVR.SBTPARTS P2
WHERE ATtrIBUTE00110 = #accountNumber#
AND P1.ITEMID = P2.ITEMID AND P2.PARTNUM = 1
ORDER BY P1.ATtrIBUTE00111 DESC
</select>
Sometimes it is necessary to pass more than one key
into an iBatis method. A java.util.Map can be used to accomplish
this:
Put the keys you need into a Map in your DAO implementation:
final
SqlMapExecutor sqlMap = getSqlMapExecutor();
final Map paramMap =
new HashMap(2);
paramMap.put( "locationId",
locationId);
paramMap.put( "brokerId",
brokerId);
clientBroker =
(ClientBroker)
sqlMap.queryForObject( "getClientBrokerByLocationBrokerIds"
, paramMap );
The mapped parameters can be accessed in your SQL map by name:
<select
id="getClientBrokerByLocationBrokerIds"
resultMap="clientBrokerResultMap"
parameterClass="java.util.Map"
>
SELECT
LOC_ID,
brOKER_ID,
brOKER_NAME,
PHONE_NUMBER,
FAX_NUMBER,
EMAIL,
LAST_MAINT_DATE,
LAST_MAINT_USER_ID
FROM CLIENT_LOCATION
WHERE LOC_ID = #locationId#
AND brOKER_ID = #brokerId#
</select>