ibatis in語句引數傳入方法

y_keven發表於2014-05-06
 第一種:傳入引數僅有陣列
       <select id="GetEmailList_Test"  resultClass="EmailInfo_">
            select *
            from MailInfo with (nolock)
            where ID in
                <iterate open="(" close=")" conjunction="," >
                    #[]#
                </iterate>
        </select>
呼叫
            string[] strValue = new string[] { "1", "2", "3" };
            Reader.QueryForList<EmailInfoModel>("WebApp_Ibatisnet.dao.GetEmailList_Test", strValue );

       第二種:傳入引數有陣列,且有其他資料
        <select id="GetEmailList_Test3" parameterClass="TestIn" resultClass="EmailInfo_">
            select  top(#Count#)*
            from MailInfo with (nolock)
            where ID in
            <iterate open="(" close=")" conjunction="," property="ArrValue" >
                #ArrValue[]#
            </iterate>
        </select>
呼叫
            TestIn ti = new TestIn();
            ti.Count = 1;
            ti.ArrValue = strValue;
            return Reader.QueryForList<EmailInfoModel>("WebApp_Ibatisnet.dao.GetEmailList_Test3", ti);
實體類:
   public class TestIn
    {
        private int count;
        public int Count
        {
            get { return count; }
            set { count = value; }
        }
        private string[] arrValue;
        public string[] ArrValue
        {
            get { return arrValue; }
            set { arrValue = value; }
        }
    }

       第三種:in後面的資料確定,使用string傳入
        <select id="GetEmailList_Test2" parameterClass="TestIn" resultClass="EmailInfo_">
            select *
            from MailInfo with (nolock)
            where ID in
            ($StrValue$)
        </select>
呼叫
                Reader.QueryForList<EmailInfoModel>("WebApp_Ibatisnet.dao.GetEmailList_Test2", "1,2,3");


其他資訊:
Iterate的屬性:
prepend -可被覆蓋的SQL語句組成部分,新增在語句的前面(可選)
property -型別為java.util.List的用於遍歷的元素(必選)
open -整個遍歷內容體開始的字串,用於定義括號(可選)
close -整個遍歷內容體結束的字串,用於定義括號(可選)
conjunction -每次遍歷內容之間的字串,用於定義AND或OR(可選)
<iterate>遍歷型別為java.util.List的元素。

相關文章