根据 张善友 写的如何结合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>