2

I have a query mapper like the following:

<select id="searchSomething" parameterType="SomeType" resultType="SomeOtherType">
  select xxxxx
  from T_XXXX
  where 1=1
  <if test="propertyName == 'userName'">
    and USER_NAME = #{propertyValue}
  </if>
  <if test="propertyName == 'address'">
    and ADDRESS = #{propertyValue}
  </if>
  <if test="propertyName == 'taskDate'">
    and TASK_DATE = #{propertyValue}
  </if>
  <if test="propertyName == 'phone1'">
    and PHONE_1 = #{propertyValue}
  </if>
  <if test="propertyName == 'phone2'">
    and PHONE_2 = #{propertyValue}
  </if>
  ...
</select>

There are so many properties. How can i simply map the property name to column name, like the following:

<select id="searchSomething" parameterType="SomeType" resultType="SomeOtherType">
  select xxxxx
  from T_XXXX
  where 1=1
    and 
    <propertyToColumn property="propertyName" />
      = #{propertyValue}
</select>

Is there something like "propertyToColumn" in MyBatis?

I found "insertColumnName" in iBatis, is it removed from MyBatis?

The parameterType is a java class like:

public class SomeType{
  private String propertyName;
  private String propertyValue;
  ... getters and setters
}

2 Answers 2

2

One way of doing this is by using :

Prepare two ArrayLists, one with propertyNames and another with propertValues. Make sure they are in proper order, ie propValuesList[i] should have the value for propNamesList[i].

Put then in a HashMap and pass it as input to mapped statement:

Map<String,Object> map = new HashMap<String,Object>();
List<String> propNamesList = new ArrayList<String>();
List<String> propValuesList = new ArrayList<String>();
propNamesList.add(0, "USER_NAME");
propNamesList.add(1, "ADDRESS");

propValuesList.add(0, "admin");
propValuesList.add(1, "hyderabad");

map.put("propNames",propNamesList);
map.put("propValues",propValuesList);

Then in mapped statement:

<select id="selectUsers" parameterType="hashmap" resultMap="UserResult">
    select * from users
    where 1 =1
    <if test="propNames != null and propValues != null">
       <foreach item="propName" index="index" collection="propNames">
        and #{propName} = #{propValues[${index}]}
       </foreach>
    </if>
 </select>

Observe the use of ${index} instead of #{index}.

1
  • This means i must keep the property-column map in java code. but it seems the only way to do this. Thank you.
    – John
    Mar 17, 2013 at 15:22
1

I think that it could be better if you do the "parameter-column" conversion in your code, and pass the result column as parameter. In that case you could do something like this:

<select id="searchSomething" parameterType="SomeType" resultType="SomeOtherType">
  select xxxxx
  from T_XXXX
  where 1=1
    and 
   ${propertyColumn} = #{propertyValue}
</select>

Of course, you will need to add the propertyColumn to your VO.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.