[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

IBATIS- SQL Maps: Problems with Dynamic SQL Elements

Posted on 2005-05-12
7
Medium Priority
?
465 Views
Last Modified: 2008-01-09
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..)



0
Comment
Question by:joeyoungkc
  • 5
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
suprapto45 earned 1000 total points
ID: 13992398
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
 

Author Comment

by:joeyoungkc
ID: 13993115
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 13993141
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Expert Comment

by:suprapto45
ID: 13993166
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 13993168
Just in case, it would be easier and faster for you :)

Regards
Dave
0
 

Author Comment

by:joeyoungkc
ID: 13996966
Thanks a lot!
Thanks for your help.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13997489
Hi,

Glad I could help.

Regards
Dave
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month18 days, 5 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question