IbatisNet的LIST遍歷實現模糊查詢

根據 張善友 寫的如何結合IbatisNet的LIST遍歷實現模糊查詢  實現

BaseSqlMapDao內定義了一個內部類來輔助模糊查詢。內部類代碼如下:
        protected class KeyWordSearch
        {
            private IList<string> _keywords = new List<string>();

            public KeyWordSearch(IEnumerable<string> keywords)
            {
                foreach (var keyword in keywords)
                {
                    _keywords.Add(GetLikeKeyword(keyword));
                }
            }

            public IList<string> KeywordList
            {
                get { return _keywords; }
            }

            public static string GetLikeKeyword(string keyword)
            {
                return string.Format("%{0}%", keyword);
            }
        }

在使用Dao類型使用
           Hashtable htargs = new Hashtable(2);
           htargs.Add("parentNo", parentNo);
           object likekeywordObj = new KeyWordSearch(new string[] {"關鍵字1", "關鍵字2"});
           htargs.Add("likekeyword", likekeywordObj);

Map文件:
  <dynamic>
    <isNotNull prepend="AND" property="likekeyword">
      <iterate property="likekeyword.KeywordList" open="(" close=")" conjunction="OR">
        NAME LIKE #likekeyword.KeywordList[]#
      </iterate>
    </isNotNull>
  </dynamic>


當然單個項的模糊查詢可以直接使用
KeyWordSearch.GetLikeKeyword方法

htargs.Add("contact", KeyWordSearch.GetLikeKeyword("劉"));

    <isNotEmpty prepend="AND" property="contact">
      CONTACT LIKE #contact#
    </isNotEmpty>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章