Java MyBatis:使用动态查询比较字符串值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19450304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MyBatis: compare String value using dynamic query
提问by axcdnt
I'm using MyBatis to map some queries where I need to compare a String
argument (myString
).
我正在使用 MyBatis 来映射一些需要比较String
参数的查询( myString
)。
My Mapper interfaceis:
我的映射器界面是:
public Map<Integer, String> findSomething(@Param("myString") String myString);
public Map<Integer, String> findSomething(@Param("myString") String myString);
My XMLis as follow:
我的XML如下:
<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 statement the following error is throwed:
当我执行此语句时,抛出以下错误:
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 String
comparison made this way, smells bad. Unfortunately it was the way database was modeled.
一个String
做这样的比较,味道不好。不幸的是,这是数据库建模的方式。
MyBatis version: 3.2.2
MyBatis 版本: 3.2.2
采纳答案by axcdnt
Definitely I had to go through a complicated approach.
当然,我必须通过一个复杂的方法。
Changed mapper method signature:
更改映射器方法签名:
public Map<Integer, String> findSomething(@Param("myPojo") MyPojo myPojo);
The XMLmust be:
该XML必须是:
<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;
Just don't forget, being a POJO, the parameter class must have it's respective getters and setters.
只是不要忘记,作为 POJO,参数类必须具有各自的 getter 和 setter。
回答by Ian Lim
Assuming Mybatis 3.2.2 and above, Revise your xml to the following and see if it helps:
假设 Mybatis 3.2.2 及以上版本,将你的 xml 修改为以下内容,看看是否有帮助:
<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;
回答by Manuel Vera Silvestre
Tested with MyBatis 3.2.8
用 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>
Note the single quotes around test and the double quotes around de constant values. The trick is to use equals as you can see.
注意 test 周围的单引号和 de 常量值周围的双引号。诀窍是使用equals,如您所见。
回答by Alexander Davliatov
One more solution should be mentioned:
应该提到另一种解决方案:
<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>