Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Ibatis中的动态查询_C#_Mysql_Ibatis_Dynamicquery - Fatal编程技术网

C# Ibatis中的动态查询

C# Ibatis中的动态查询,c#,mysql,ibatis,dynamicquery,C#,Mysql,Ibatis,Dynamicquery,是否可以将动态查询传递给Ibatis并从中获取记录 例如,我使用StringBuilder构建了我的查询,最后,我得到了以下查询“select emp_id,emp_name from employee,其中emp_id==1”。现在我需要将这个完整的查询传递给Ibatis并获取记录 注意:这里的列数和条件在每个查询形式上会有所不同 编辑:如何将查询传递给Ibatis并使用Ibatis执行查询 MyBatis附带SelectBuilder和SQLBuilder。您可以使用此选择生成器来构造动态查

是否可以将动态查询传递给Ibatis并从中获取记录

例如,我使用StringBuilder构建了我的查询,最后,我得到了以下查询“select emp_id,emp_name from employee,其中emp_id==1”。现在我需要将这个完整的查询传递给Ibatis并获取记录

注意:这里的列数和条件在每个查询形式上会有所不同


编辑:如何将查询传递给Ibatis并使用Ibatis执行查询

MyBatis附带SelectBuilder和SQLBuilder。您可以使用此选择生成器来构造动态查询。有关SelectBuilder的更多信息,请参见《用户指南》。

我认为您不能,即使您可以,也不应该这样做。“使用StringBuilder构建查询”违背了iBatis的目的,并且容易出现许多问题(其中包括SQL注入),iBatis正是为了防止这些问题而设计的

帮你自己一个忙:读一读关于从Java到XML的SQL(如果你真的想使用iBatis的话)


如果你真的坚持。。。嗯,我想您可以将整个sql查询作为单个字符串传递给iBatis,例如调用动态执行该sql代码的存储过程。可怕但可以想象。

老问题,但我想插话。我同意@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);
    }
public IList GetTraceLogRecords(字符串systemType、字符串plantName、int?deviceId、DateTime startTime、DateTime endTime、字符串logDescription、字符串loggerName、列表TraceLevel ID)
{
IDictionary traceQueryParameters=新字典();
添加(“deviceId”,deviceId);
添加(“startTime”,startTime);
添加(“结束时间”,结束时间);
添加(“logDescription”,logDescription);
添加(“loggerName”,loggerName);
添加(“TraceLevel ID”,TraceLevel ID);
返回DataSources.GetDbConnectionName(系统类型,plantName).QueryForList(“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>

选择TraceLevel ID、Trace、DeviceId、LoggerName、CreatedTimeStamp、ThreadId
追踪
DeviceId=#DeviceId#
CreatedTimeStamp>=#开始时间#
跟踪式#日志描述#
LoggerName类似于#LoggerName#
TraceLevelId=#traceLevelIds[]#

我可以知道如何将字符串传递给Ibatis并处理其结果集吗$querystring$我尝试使用上述语句,但不确定如何传递querystring和处理结果集