ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rahul Saluja <rahul.sal...@vnl.in>
Subject Insert help
Date Wed, 04 Feb 2009 07:09:55 GMT
Hello all ,

Can any one tell me how to use  typehandlercallback  implementation with  sqlmap  while doing
insert

and one more thing how would I map my array data type in my sqlmap


I know I can map my class for int  but how I would do it for short array which holds the different
account numbers which a person holds
At my Backend AccountNumbers field map to varchar , so to overcome this I can use custom TypeHandlerCallback
implementation  but how would
Declare this in my sqlmap and I am not allowed to change my data type in DB.

Say I have following class

public class Person
{

      int age ;
      short [] Accountnumbers;
      public short[] getAccountnumbers()
      {
            return Accountnumbers;
      }
      public void setAccountnumbers(short[] accountnumbers)
      {
            Accountnumbers = accountnumbers;
      }
      public int getAge()
      {
            return age;
      }
      public void setAge(int age)
      {
            this.age = age;
      }


}





import java.sql.SQLException;
import java.sql.Types;


import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

public class ArraysToStringTypeHandler implements TypeHandlerCallback
{


      public Object getResult(ResultGetter arg0) throws SQLException
      {

            return null;
      }


      public void setParameter(ParameterSetter setter, Object parameter) throws SQLException
      {
            if (parameter == null) {

                  setter.setNull(Types.CHAR);
        } else {


            setter.setString(ArraysToString( parameter));
        }
            // TODO Auto-generated method stub

      }


      public Object valueOf(String arg0)
      {
            // TODO Auto-generated method stub
            return null;
      }



      private String ArraysToString(Object obj)
      {

            if ( obj == null)
            {
                  throw new IllegalArgumentException ("Could not convert null to a String
value. " +
                  "Valid argument is an array of type short Only ");
            }
            else if (obj instanceof short []  )
            {


                  short [] ourData = ( short [])obj;

                  int length = ourData.length;


                  StringBuffer dataHolderSb = new StringBuffer();
                  for(int i = 0 ;i<length ; i++)
                  {


                        dataHolderSb.append(ourData[i]);
                  }
                  return dataHolderSb.toString();


            }
            else{

                  return null;
            }
            //return null;

      }
}

<insert id="Person" parameterClass="com.foo.Contact">
           insert into personTable (age,Accountnumbers)
           values (#age #,#what to declare for short array here for Accountnumber field, handler=ArraysToStringTypeHandler
#)
</insert>

Looking forward to your response.


Regards
Rahul Saluja
The information contained in this e-mail is private & confidential and may also be legally
privileged. If you are not the intended recipient, please notify us, preferably by e-mail,
and do not read, copy or disclose the contents of this message to anyone.

Mime
View raw message