IBATIS- SQL Maps: Problems with Dynamic SQL Elements

Hi experts,

In my sqlmapping, one of my statement is like this:

    <select id="getTotalNumber" resultClass="int" parameterClass="java.lang.String" >
   
        SELECT count(*) as Num
        FROM AWARD
                Quarter =#value#      
    </select>  
Simply, huh? and it works fine.
but now I want to some dynamic elements on it, so now my statement is this:
    <select id="getTotalNumber" resultClass="int" parameterClass="java.lang.String" >
   
        SELECT count(*) as Num
        FROM AWARD
        <dynamic prepend="WHERE">
            <isNotEmpty property="value">
                Quarter =#value#  
            </isNotEmpty>
        </dynamic>  
   
    </select>  

The problem is it complain that java.lang.String has no variable "value"
How can i do it in a simple way ??? (I don't want to create a new javabeans and parse in..)



joeyoungkcAsked:
Who is Participating?
 
suprapto45Commented:
Hi,

You can use HashMap though.

Let me give you example.

------------------------
HashMap map = new HashMap();
map.put("quarter", new Integer(quarter));

Object obj = sqlMap.queryForObject("getTotalNumber", map);
            if (obj != null)
                total = Integer.parseInt(obj.toString());

------------------------

    <select id="getTotalNumber" resultClass="int" parameterClass="map" >
   
        SELECT count(*) as Num
        FROM AWARD
        <dynamic prepend="WHERE">
         <isNotEmpty property="quarter">
            Quarter = $quarter$
         </isNotEmpty>
        </dynamic>  
   
    </select>  

I hope that helps.

Regards
Dave

0
 
joeyoungkcAuthor Commented:
Dave,

Thanks for the reply.
Just wondering is it possible to use String instead of other object? Because I have many similar statement, I don't want to create HashMap for each of them.

Thanks
0
 
suprapto45Commented:
Hi,

Unfortunately, I do not know if you want to stick with String only. I only solved that particular problem using HashMap and JavaBean. I am sorry.

Regards
Dave

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
suprapto45Commented:
Hi,

Alternatively, instead of checking the dynamic prepend=WHERE in your iBatis, you can just check the String in your Java class.

if (quarter == null)
{
    Object obj = sqlMap.queryForObject("getTotalNumberWithoutDynamicPrepend", quarter);
}
else
{
    Object obj = sqlMap.queryForObject("getTotalNumberWithHardCodeWhere", quarter);
}

Regards
Dave

0
 
suprapto45Commented:
Just in case, it would be easier and faster for you :)

Regards
Dave
0
 
joeyoungkcAuthor Commented:
Thanks a lot!
Thanks for your help.
0
 
suprapto45Commented:
Hi,

Glad I could help.

Regards
Dave
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.