创建动态查询Ibatis

发布于 2024-12-08 04:59:02 字数 107 浏览 0 评论 0原文

有什么方法可以传递动态列名吗?

传递值

我们可以使用 ## value#

,其中 #columnName# = #value#

对我不起作用

Is there any way to pass dynamic column names ?

we can pass values ussing ##

value#

Where #columnName# = #value#

not work for me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

二货你真萌 2024-12-15 04:59:02

这是我通过从 IBatis 生成动态查询的解决方案:。

查询的配置对象:

[Serializable]
    public partial class QueryHelper
    {
        private string _queryFields;
        public string query_fields
        {
            get { return _queryFields; }
            set { _queryFields = value; }
        }

        private string _queryFrom;
        public string query_from
        {
            get { return _queryFrom; }
            set { _queryFrom = value; }
        }

        private string _queryFilterDefa;
        public string query_filter_defa
        {
            get { return _queryFilterDefa; }
            set { _queryFilterDefa = value; }
        }

        private string _queryFilterId;
        public string query_filter_id
        {
            get { return _queryFilterId; }
            set { _queryFilterId = value; }
        }

        private string _queryJoins;
        public string query_joins
        {
            get { return _queryJoins; }
            set { _queryJoins = value; }
        }

        private string _queryFilter;
        public string query_filter
        {
            get { return _queryFilter; }
            set { _queryFilter = value; }
        }

        private string _queryOrder;
        public string query_order
        {
            get { return _queryOrder; }
            set { _queryOrder = value; }
        }
    }

XML

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="QueryHelper"
    xmlns="http://ibatis.apache.org/mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <alias>
    <typeAlias alias="QueryHelper" type="Core.CustomEntities.QueryHelper" />
  </alias>
  <statements>
    <select id="ExecuteQuery" parameterClass="QueryHelper" resultClass="HashTable">
      SELECT $query_fields$
      FROM $query_from$
      <isNotEmpty property="query_joins" >
        $query_joins$
      </isNotEmpty>
      WHERE $query_filter_defa$
      <isNotEmpty prepend="AND" property="query_filter_id" >
        $query_filter_id$
      </isNotEmpty>
      <isNotEmpty prepend="AND" property="query_filter" >
        $query_filter$
      </isNotEmpty>
      <isNotEmpty prepend="ORDER BY" property="query_order" >
        $query_order$
      </isNotEmpty>
    </select>
  </statements>
</sqlMap>

This is my solution by generate dynamic queryes from IBatis:.

The configuration Object for the query:

[Serializable]
    public partial class QueryHelper
    {
        private string _queryFields;
        public string query_fields
        {
            get { return _queryFields; }
            set { _queryFields = value; }
        }

        private string _queryFrom;
        public string query_from
        {
            get { return _queryFrom; }
            set { _queryFrom = value; }
        }

        private string _queryFilterDefa;
        public string query_filter_defa
        {
            get { return _queryFilterDefa; }
            set { _queryFilterDefa = value; }
        }

        private string _queryFilterId;
        public string query_filter_id
        {
            get { return _queryFilterId; }
            set { _queryFilterId = value; }
        }

        private string _queryJoins;
        public string query_joins
        {
            get { return _queryJoins; }
            set { _queryJoins = value; }
        }

        private string _queryFilter;
        public string query_filter
        {
            get { return _queryFilter; }
            set { _queryFilter = value; }
        }

        private string _queryOrder;
        public string query_order
        {
            get { return _queryOrder; }
            set { _queryOrder = value; }
        }
    }

The XML

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="QueryHelper"
    xmlns="http://ibatis.apache.org/mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <alias>
    <typeAlias alias="QueryHelper" type="Core.CustomEntities.QueryHelper" />
  </alias>
  <statements>
    <select id="ExecuteQuery" parameterClass="QueryHelper" resultClass="HashTable">
      SELECT $query_fields$
      FROM $query_from$
      <isNotEmpty property="query_joins" >
        $query_joins$
      </isNotEmpty>
      WHERE $query_filter_defa$
      <isNotEmpty prepend="AND" property="query_filter_id" >
        $query_filter_id$
      </isNotEmpty>
      <isNotEmpty prepend="AND" property="query_filter" >
        $query_filter$
      </isNotEmpty>
      <isNotEmpty prepend="ORDER BY" property="query_order" >
        $query_order$
      </isNotEmpty>
    </select>
  </statements>
</sqlMap>
会发光的星星闪亮亮i 2024-12-15 04:59:02

您可以为整个 WHERE 子句构建一个字符串,将其放入参数对象中,然后在 iBATIS XML 中引用它,如下所示

select *
from blah
where ?whereClause?

:参数末尾的 告诉 iBATIS 按原样使用其内容。这可能会带来潜在风险(SQL 注入等),因此请务必在继续使用此方法之前彻底检查参数。

You can build a string for the entire WHERE clause, put it in your parameter object, and then reference it in your iBATIS XML like this:

select *
from blah
where ?whereClause?

The '?' on the ends of a parameter tells iBATIS to use its contents as-is. This can open the door to potential risks (SQL injection, etc.), so be sure to check your parameters thoroughly before proceeding with this method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文