[ACCEPTED]-Passing multiple arguments into a SELECT without using a complex object-mybatis

Accepted answer
Score: 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">
  AND CONTACT_PHONE = ${phone}
  AND CONTACT_MAIl = ${email}


Score: 8

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

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

Hope this helps.

Score: 6

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

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

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

Score: 0

cant comment,

Found the answer, thanks.


this 9 link is broken, currently correct one is 8 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 7 know the name of a method parameter so MyBatis 6 names them by default like: param1, param2... If 5 you want to give them a name use the @param 4 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 3 in your xml like follows:

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


when you declare 2 method in your interface instead of standard 1 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)

More Related questions