MyBatis: compare String value with dynamic query

I use MyBatis to match some queries where I need to compare the argument String( myString).

My Mapper interface :

public Map<Integer, String> findSomething(@Param("myString") String myString);

My XML is as follows:

<select id="findSomething" parameterType="String" resultType="Map">
    SELECT column1 as key,
           column2 as value
    FROM my_table
    <where>
         <choose>
            <when test="myString == 'xxx'">
                column3 = 1
            </when>
            <when test="myString == 'yyy'">
                myColumn  = 2
            </when>
            <when test="myString == 'zzz'">
                myColumn  = 3
            </when>
        </choose>
    </where>
    ORDER BY value;
</select>

When I execute this expression, the following error is thrown:

ERROR [stderr] Caused by: org.apache.ibatis.exceptions.PersistenceException: 
ERROR [stderr] ### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'myString' in 'class java.lang.String'

A Stringcomparison made in a way that smells bad. Unfortunately, this is how the database was modeled.

MyBatis version: 3.2.2

+4
source share
4 answers

I definitely had to go through a difficult approach.

The signature of the matching method is changed:

public Map<Integer, String> findSomething(@Param("myPojo") MyPojo myPojo);

XML should be:

<select id="findSomething" resultType="Map">
SELECT column1 as key,
       column2 as value
FROM my_table
<where>
     <choose>
        <when test="myPojo == 'xxx'">
            column3 = 1
        </when>
        <when test="myPojo == 'yyy'">
            myColumn  = 2
        </when>
        <when test="myPojo == 'zzz'">
            myColumn  = 3
        </when>
    </choose>
</where>
ORDER BY value;

, , POJO, .

+2

MyBatis 3.2.8

<select id="findSomething" parameterType="String" resultType="Map">
    SELECT column1 as key,
           column2 as value
    FROM my_table
    <where>
         <choose>
            <when test='"xxx".equals(myString)'>
                column3 = 1
            </when>
            <when test='"yyy".equals(myString)'>
                myColumn  = 2
            </when>
            <when test='"zzz".equals(myString)'>
                myColumn  = 3
            </when>
        </choose>
    </where>
    ORDER BY value;
</select>

. , , .

+5

Assuming Mybatis 3.2.2 and higher, change your xml to the following and see if it helps:

<select id="findSomething" parameterType="String" resultType="Map">
SELECT column1 as key,
       column2 as value
FROM my_table
<where>
     <choose>
        <when test="value == 'xxx'">
            column3 = 1;
        </when>
        <when test="value == 'yyy'">
            myColumn  = 2;
        </when>
        <when test="value == 'zzz'">
            myColumn  = 3;
        </when>
    </choose>
</where>
ORDER BY value;

0
source

Another solution worth mentioning:

<where>
     <choose>
        <when test='value == "xxx"'>
            column3 = 1;
        </when>
        <when test='value == "yyy"'>
            myColumn  = 2;
        </when>
        <when test='value == "zzz"'>
            myColumn  = 3;
        </when>
    </choose>
</where>
0
source

All Articles