Column map Mybatis property

I have a query display block, for example:

<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 just map the property name to the column name, for example:

 <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?

ParameterType is a java class, for example:

 public class SomeType{ private String propertyName; private String propertyValue; ... getters and setters } 
+2
source share
2 answers

I think it would be better if you converted the column parameter to your code and passed the result column as a parameter. In this case, you can 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 a propertyColumn to your VO.

+1
source

One way to do this is to use:

Prepare two ArrayLists, one with property names and one with eigenvalues. Make sure they are in the correct order, i.e. propValuesList [i] must be set to propNamesList [i].

Then put it in the HashMap and pass it as input to the displayed 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 the displayed 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> 

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

+2
source

All Articles