views:

53

answers:

2

Hello All,

Is it possible to pass dynamic query to Ibatis and get the record from it?

E.g. I built my query using StringBuilder and at last, I got the following query "select emp_id, emp_name from employee where emp_id==1" . Now i need to pass this complete query to Ibatis and get the record.

Note: Here the number of columns and where conditions will vary on each query formation

EDIT: How to pass the query to Ibatis and get it executed using ibatis?

A: 

MyBatis comes with SelectBuilder and SQLBuilder. You can use this SelectBuilder to construct the dynamic query. More information about SelectBuilder can found in the user guide.

chedine
+1  A: 

I don't think you can, and even if you could, you shouldn't do that. To "build your query using StringBuilder" defeats iBatis purpose, and is prone to lots of problems (SQL injection among them) which iBatis is precisely designed to prevent.

Do yourself a favour: read about dynamic queries in iBatis and take out your SQL from Java to XML (if you really want to use iBatis).

If you really insist... well, I guess you can pass the whole sql query as a single string to iBatis, for example invoking a stored procedure that executes dynamically that sql code. Horrid but conceivable.

leonbloy
May I know how to pass string to Ibatis and handle its result set.<select id="SEARCH_QUERRY_DATA" parameterClass="String" remapResults="true" resultClass="System.Collections.Hashtable" > <dynamic> $querryString$ </dynamic> </select>I tried with above statement but not sure how to pass the querystring and handle the result set
Sri Kumar