如何使用iBatis的(MyBatis的)一个IN查询注解? [英] How to use Annotations with iBatis (myBatis) for an IN query?

查看:1369
本文介绍了如何使用iBatis的(MyBatis的)一个IN查询注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望仅使用MyBatis的注释;我们真的试图避免XML。我们正在尝试使用IN的条款:

We'd like to use only annotations with MyBatis; we're really trying to avoid xml. We're trying to use an "IN" clause:

@Select("SELECT * FROM blog WHERE id IN (#{ids})") 
List<Blog> selectBlogs(int[] ids); 

MyBatis的似乎不能够挑选出整数数组,并把那些到生成的查询。这似乎为失败轻声我们没有结果了。

MyBatis doesn't seem able to pick out the array of ints and put those into the resulting query. It seems to "fail softly" and we get no results back.

看起来我们可以做到这一点使用XML映射,但我们很想避免这种情况。是否有一个正确的注释语法呢?

It looks like we could accomplish this using XML mappings, but we'd really like to avoid that. Is there a correct annotation syntax for this?

推荐答案

我相信这是JDBC的$ P $的细微差别ppared声明,而不是MyBatis的。这里有一个链接解释这个问题,并提供各种解决方案。不幸的是,这些解决方案都是可行的应用程序,但是,它仍然是一个很好看的懂$ P $的限制ppared问候语句的IN的条款。一个解决方案(也许不理想),可以对具体事物的DB-一侧找到。例如,在PostgreSQL里,人们可以使用:

I believe this is a nuance of jdbc's prepared statements and not MyBatis. There is a link here that explains this problem and offers various solutions. Unfortunately, none of these solutions are viable for your application, however, its still a good read to understand the limitations of prepared statements with regards to an "IN" clause. A solution (maybe suboptimal) can be found on the DB-specific side of things. For example, in postgresql, one could use:

"SELECT * FROM blog WHERE id=ANY(#{blogIds}::int[])"

ANY是一样的IN和INT :: []是类型转换操作数为int数组。被送入语句的参数应该是这个样子:

"ANY" is the same as "IN" and "::int[]" is type casting the argument into an array of ints. The argument that is fed into the statement should look something like:

"{1,2,3,4}"

这篇关于如何使用iBatis的(MyBatis的)一个IN查询注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆