如何使用带有 iBatis (myBatis) 的注释进行 IN 查询? [英] How to use Annotations with iBatis (myBatis) for an IN query?

查看:41
本文介绍了如何使用带有 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 准备好的语句的细微差别,而不是 MyBatis.有一个链接 这里 解释了这个问题并提供了各种解决方案.不幸的是,这些解决方案都不适用于您的应用程序,但是,了解有关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[]" 将参数类型转换为整数数组.输入到语句中的参数应该类似于:

"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天全站免登陆