8

I am trying to pass in startSequenceId, stopSequenceId, orderNumber into the SQL map, however, i don't wish to use a typed object, i.e. parameterType="com.abc.Order", can i do so?

<select id="getSequenceIdByOrderNumber" parameterType="?" resultType="int">
    select *
    from log
    where seq_id
    between #{startSequenceId} and #{stopSequenceId}
    and order_no = #{orderNumber}
    and rownum = 1
</select>
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215

5 Answers5

16

You can use the built in parameterType 'map' eg

Map<String, Object> parms = new HashMap<String, Object>();
parms.put("name", "abc");
parms.put("phone", "123");
parms.put("email", "123@email.com");

List<Contact> list = myBatis.selectList("getContacts",parms);


<!-- in xml mapper -->
<select id="getContacts" parameterType="map" resultMap="Contact">
  SELECT * FROM CONTACT 
  WHERE CONTACT_NAME = ${name}
  AND CONTACT_PHONE = ${phone}
  AND CONTACT_MAIl = ${email}
</select>
johnm
  • 7,327
  • 1
  • 24
  • 35
8

@Chin I'll post what I had typed anyway with a simple example though you found what your looking for. My example using iBatis 2.3.4

<select id="retrieveTestXXX" parameterClass="java.util.Map" resultClass="java.lang.Integer">
    SELECT
    example_table.id
    FROM example_table
    WHERE example_table.xx_id = #testId# AND example_table.xx_id = #test2Id#
</select>

Hope this helps.

MalsR
  • 1,197
  • 1
  • 12
  • 23
6

In MyBatis 3, you can use @Param annotation in your mapper class(interface) method:

public getSequenceIdByOrderNumber(@Param("seqId") int sequenceId,@Param("orderId") int orderNo);

Then you can use #{seqId}, #{orderId} in the SQL without using parameterType attribute.

corlaez
  • 1,352
  • 1
  • 15
  • 30
Janahan
  • 401
  • 8
  • 20
  • If you use @Param annotations, you also must add parameterType="map" to your SQL element in the mapper, or you will get a class cast exception. – otterslide Sep 01 '16 at 15:15
0

cant comment,

Found the answer, thanks.

http://code.google.com/p/mybatis/wiki/HowToSelectMultipleParams

this link is broken, currently correct one is https://github.com/mybatis/mybatis-3/wiki/FAQ#how-do-i-use-multiple-parameters-in-a-mapper

copy + paste from this wiki

Java reflection does not provide a way to know the name of a method parameter so MyBatis names them by default like: param1, param2... If you want to give them a name use the @param annotation this way:

import org.apache.ibatis.annotations.Param;
public interface UserMapper {
   User selectUser(@Param("username") String username, @Param("hashedPassword") String hashedPassword);
}

Now you can use them in your xml like follows:

<select id=”selectUser” resultType=”User”>
    select id, username, hashedPassword
    from some_table
    where username = #{username}
    and hashedPassword = #{hashedPassword}
</select>

tl;dr

when you declare method in your interface instead of standard java structure

Type methodName (
    ParamType1 paramName1,
    ParamType2 paramName2,
    … ); (this is interface so no method body)

use (add @Param)

Type methodName (
    @Param("name1 available in xml") ParamType1 paramName1,
    @Param("name2 available in xml") ParamType2 paramName2,
    …); (this is still interface so no method body)
Community
  • 1
  • 1
Alexander
  • 177
  • 2
  • 9
-2

Found the answer, thanks.

http://code.google.com/p/mybatis/wiki/HowToSelectMultipleParams

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215