Ibatis 中的动态查询
是否可以将动态查询传递给Ibatis并从中获取记录?
例如,我使用 StringBuilder 构建了查询,最后,我得到了以下查询“select emp_id, emp_name from employee where emp_id==1”。现在我需要将这个完整的查询传递给 Ibatis 并获取记录。
注意:这里的列数和条件会因每个查询形式而异
编辑:如何将查询传递给 Ibatis 并使用 ibatis 执行它?
Is it possible to pass dynamic query to Ibatis and get the record from it?
E.g. I built my query using StringBuilder and at last, I got the following query "select emp_id, emp_name from employee where emp_id==1" . Now i need to pass this complete query to Ibatis and get the record.
Note: Here the number of columns and where conditions will vary on each query formation
EDIT: How to pass the query to Ibatis and get it executed using ibatis?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为你可以,即使你可以,你也不应该这样做。 “使用 StringBuilder 构建查询”违背了 iBatis 的目的,并且容易出现许多问题(其中包括 SQL 注入),而 iBatis 正是为了防止这些问题而设计的。
帮自己一个忙:阅读iBatis 中的动态查询并取出将您的 SQL 从 Java 转换为 XML(如果您确实想使用 iBatis)。
如果你真的坚持......好吧,我想你可以将整个 sql 查询作为单个字符串传递给 iBatis,例如调用动态执行该 sql 代码的存储过程。可怕但可以想象。
I don't think you can, and even if you could, you shouldn't do that. To "build your query using StringBuilder" defeats iBatis purpose, and is prone to lots of problems (SQL injection among them) which iBatis is precisely designed to prevent.
Do yourself a favour: read about dynamic queries in iBatis and take out your SQL from Java to XML (if you really want to use iBatis).
If you really insist... well, I guess you can pass the whole sql query as a single string to iBatis, for example invoking a stored procedure that executes dynamically that sql code. Horrid but conceivable.
MyBatis 附带了 SelectBuilder 和 SQLBuilder。您可以使用此SelectBuilder来构造动态查询。有关 SelectBuilder 的更多信息可以在用户指南中找到。
MyBatis comes with SelectBuilder and SQLBuilder. You can use this SelectBuilder to construct the dynamic query. More information about SelectBuilder can found in the user guide.
老问题,但我想插话。我同意@leonbloy,ibatis 提供的功能可以避免您尝试做的事情。 动态查询的 ibatis 链接应该可以帮助您解决这个问题。
这是我使用的一个简单示例:
有一种方法将参数作为字典传递
创建 select 语句并检查输入是否为空,以确定是否将它们包含在 where 中子句:
Old issue but I wanted to chime in. I agree with @leonbloy, ibatis provides features to avoid what you are trying to do. The ibatis link for dynamic queries should help you figure it out.
Here is a simple example I've used:
Have a method to pass in your arguments as a dictionary
Create your select statement and check if the inputs are null for whether to include them in your where clause: