Search code examples
db2mybatisognl

How to apply a method to a parameter in MyBatis


After reading about Mapper XMLs I can't help to wonder how one might go about appling some common transforms to a parameter. For example...

<select id="selectPerson" parameterType="String" resultType="hashmap">
  <!-- #{name} should always be upper case and have a trailing % -->
  SELECT * FROM PERSON WHERE FIRST_NAME like #{name} 
</select> 

After reading this and this I can make some observations.

  1. Using SQL functions such as upper or concat or '||' or '+' to do transforms kills performance in DB2
  2. I could always wrap the the mapper or expose the details in the service layer but that seems messy

What I want is to be able to do something like...

<select id="selectPerson" parameterType="String" resultType="hashmap">
  <!-- #{name} should always be upper case and have a trailing % -->
  SELECT * FROM PERSON WHERE FIRST_NAME like #{name.upperCase() + '%'} 
</select> 

Is something like this possible or what is the second best solution?

Update: it appears that MyBatis uses OGNL for some expression evaluation. For example, if and ${} expressions use OGNL but #{} does NOT appear to unless there is some way to trick it.


Solution

  • I ran through the same problem too. But I didn't find any solution for this. So I had to preprocess the #{name} parameter from the calling function.