Old issue but I wanted to chime in. I agree with @leonbloy, ibatis provides features to avoid what you are trying to do. The ibatis link for dynamic queries should help you figure it out.
Here is a simple example I've used:
Have a method to pass in your arguments as a dictionary
public IList<ITraceLogRecord> GetTraceLogRecords(string systemType, string plantName, int? deviceId, DateTime startTime, DateTime endTime, string logDescription, string loggerName, List<int> traceLevelIds)
{
IDictionary<string, object> traceQueryParameters = new Dictionary<string, object>();
traceQueryParameters.Add("deviceId", deviceId);
traceQueryParameters.Add("startTime", startTime);
traceQueryParameters.Add("endTime", endTime);
traceQueryParameters.Add("logDescription", logDescription);
traceQueryParameters.Add("loggerName", loggerName);
traceQueryParameters.Add("traceLevelIds", traceLevelIds);
return DataSources.GetDbConnectionName(systemType, plantName).QueryForList<ITraceLogRecord>("SelectTraceLogRecords", traceQueryParameters);
}
Create your select statement and check if the inputs are null for whether to include them in your where clause:
<select id="SelectTraceLogRecords" parameterClass="System.Collections.IDictionary" resultMap="TraceLogRecordMap">
SELECT TraceLevelId, Trace, DeviceId, LoggerName, CreatedTimeStamp, ThreadId
FROM Trace
<dynamic prepend="WHERE">
<isNotNull prepend="AND" property="deviceId">
DeviceId = #deviceId#
</isNotNull>
<isNotNull prepend="AND" property="startTime">
CreatedTimeStamp >= #startTime#
</isNotNull>
<isNotNull prepend="AND" property="endTime">
<![CDATA[CreatedTimeStamp <= #endTime#]]>
</isNotNull>
<isNotNull prepend="AND" property="logDescription">
Trace LIKE #logDescription#
</isNotNull>
<isNotNull prepend="AND" property="loggerName">
LoggerName LIKE #loggerName#
</isNotNull>
<isNotNull prepend="AND" property="traceLevelIds">
<iterate property="traceLevelIds" open="(" close=")" conjunction="OR">
TraceLevelId = #traceLevelIds[]#
</iterate>
</isNotNull>
</dynamic>
</select>