Ibatis 中的动态查询

发布于 2024-09-07 14:29:58 字数 274 浏览 3 评论 0原文

是否可以将动态查询传递给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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

闻呓 2024-09-14 14:29:58

我不认为你可以,即使你可以,你也不应该这样做。 “使用 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.

末骤雨初歇 2024-09-14 14:29:58

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.

且行且努力 2024-09-14 14:29:58

老问题,但我想插话。我同意@leonbloy,ibatis 提供的功能可以避免您尝试做的事情。 动态查询的 ibatis 链接应该可以帮助您解决这个问题。

这是我使用的一个简单示例:

有一种方法将参数作为字典传递

public IList<ITraceLogRecord> GetTraceLogRecords(string systemType, string plantName, int? deviceId, DateTime startTime, DateTime endTime, string logDescription, string loggerName, List<int> traceLevelIds)
    {
        IDictionary<string, object> traceQueryParameters = new Dictionary<string, object>();
        traceQueryParameters.Add("deviceId", deviceId);
        traceQueryParameters.Add("startTime", startTime);
        traceQueryParameters.Add("endTime", endTime);
        traceQueryParameters.Add("logDescription", logDescription);
        traceQueryParameters.Add("loggerName", loggerName);
        traceQueryParameters.Add("traceLevelIds", traceLevelIds);

        return DataSources.GetDbConnectionName(systemType, plantName).QueryForList<ITraceLogRecord>("SelectTraceLogRecords", traceQueryParameters);
    }

创建 select 语句并检查输入是否为空,以确定是否将它们包含在 where 中子句:

<select id="SelectTraceLogRecords" parameterClass="System.Collections.IDictionary" resultMap="TraceLogRecordMap">
  SELECT TraceLevelId, Trace, DeviceId, LoggerName, CreatedTimeStamp, ThreadId
  FROM Trace
  <dynamic prepend="WHERE">
    <isNotNull prepend="AND" property="deviceId">
      DeviceId = #deviceId#
    </isNotNull>
    <isNotNull prepend="AND" property="startTime">
      CreatedTimeStamp >= #startTime#
    </isNotNull>
    <isNotNull prepend="AND" property="endTime">
      <![CDATA[CreatedTimeStamp <= #endTime#]]>       
    </isNotNull>
    <isNotNull prepend="AND" property="logDescription">
      Trace LIKE #logDescription#
    </isNotNull>
    <isNotNull prepend="AND" property="loggerName">
      LoggerName LIKE #loggerName#
    </isNotNull>
    <isNotNull prepend="AND" property="traceLevelIds">
      <iterate property="traceLevelIds" open="(" close=")" conjunction="OR">
        TraceLevelId = #traceLevelIds[]#
      </iterate>
    </isNotNull>
  </dynamic>
</select>

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

public IList<ITraceLogRecord> GetTraceLogRecords(string systemType, string plantName, int? deviceId, DateTime startTime, DateTime endTime, string logDescription, string loggerName, List<int> traceLevelIds)
    {
        IDictionary<string, object> traceQueryParameters = new Dictionary<string, object>();
        traceQueryParameters.Add("deviceId", deviceId);
        traceQueryParameters.Add("startTime", startTime);
        traceQueryParameters.Add("endTime", endTime);
        traceQueryParameters.Add("logDescription", logDescription);
        traceQueryParameters.Add("loggerName", loggerName);
        traceQueryParameters.Add("traceLevelIds", traceLevelIds);

        return DataSources.GetDbConnectionName(systemType, plantName).QueryForList<ITraceLogRecord>("SelectTraceLogRecords", traceQueryParameters);
    }

Create your select statement and check if the inputs are null for whether to include them in your where clause:

<select id="SelectTraceLogRecords" parameterClass="System.Collections.IDictionary" resultMap="TraceLogRecordMap">
  SELECT TraceLevelId, Trace, DeviceId, LoggerName, CreatedTimeStamp, ThreadId
  FROM Trace
  <dynamic prepend="WHERE">
    <isNotNull prepend="AND" property="deviceId">
      DeviceId = #deviceId#
    </isNotNull>
    <isNotNull prepend="AND" property="startTime">
      CreatedTimeStamp >= #startTime#
    </isNotNull>
    <isNotNull prepend="AND" property="endTime">
      <![CDATA[CreatedTimeStamp <= #endTime#]]>       
    </isNotNull>
    <isNotNull prepend="AND" property="logDescription">
      Trace LIKE #logDescription#
    </isNotNull>
    <isNotNull prepend="AND" property="loggerName">
      LoggerName LIKE #loggerName#
    </isNotNull>
    <isNotNull prepend="AND" property="traceLevelIds">
      <iterate property="traceLevelIds" open="(" close=")" conjunction="OR">
        TraceLevelId = #traceLevelIds[]#
      </iterate>
    </isNotNull>
  </dynamic>
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文