Java API for Dynamic SQL

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Java API for Dynamic SQL

Sumeet Singh
Can I fire a dynamic sql via a Java API in MyBatis? I am trying to build a module which when given a SQL in a string format (same like you give in a mapper xml) and a parameter value map can be fired via the API. 
e.g. 

My mapper function 
<select id="executeReport" resultType="java.util.Map">
${sql}
</select>


This works if dynamic sql is not used, it replaces all parameters within the sql.. 
Map<String,Object> parameterMap = new HashMap<>();
parameterMap.put("sql","select name from Client where name = #{productName}");
parameterMap.put("productName","Gluon");
sqlSession.select("executeReport",parameterMap,reportResultHandler);


However, if I put in the dynamic SQL syntax, it complains which is understandable .. Question is is there any Java API so that I can pass the sql and use the dynamic sql functionality of MyBatis//
Map<String,Object> parameterMap = new HashMap<>();
parameterMap.put("sql","select name from Client where name = #{productName} <if test=\"p1 != null\"> and enabled = 0 </if>");
parameterMap.put("productName","Gluon");
parameterMap.put("p1",null);
sqlSession.select("executeReport",parameterMap,reportResultHandler);

Any pointers on how to accomplish this?

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Java API for Dynamic SQL

Kazuki Shimizu
I think the MyBatis does not assume this usage.

On Wednesday, October 19, 2016 at 6:48:21 AM UTC+9, Sumeet Singh wrote:
Can I fire a dynamic sql via a Java API in MyBatis? I am trying to build a module which when given a SQL in a string format (same like you give in a mapper xml) and a parameter value map can be fired via the API. 
e.g. 

My mapper function 
<select id="executeReport" resultType="java.util.Map">
${sql}
</select>


This works if dynamic sql is not used, it replaces all parameters within the sql.. 
Map<String,Object> parameterMap = new HashMap<>();
parameterMap.put("sql","select name from Client where name = #{productName}");
parameterMap.put("productName","Gluon");
sqlSession.select("executeReport",parameterMap,reportResultHandler);


However, if I put in the dynamic SQL syntax, it complains which is understandable .. Question is is there any Java API so that I can pass the sql and use the dynamic sql functionality of MyBatis//
Map<String,Object> parameterMap = new HashMap<>();
parameterMap.put("sql","select name from Client where name = #{productName} <if test=\"p1 != null\"> and enabled = 0 </if>");
parameterMap.put("productName","Gluon");
parameterMap.put("p1",null);
sqlSession.select("executeReport",parameterMap,reportResultHandler);

Any pointers on how to accomplish this?

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.