ibatis 多種傳參方式


1,在公司項目yuda遇到的傳入in語句,如果直接拼接in語句:in (....),sqlmap中使用#...#輸出是不行的。

        為需要使用:

                           第三種: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"); 

參考自:https://blog.csdn.net/yangkai_hudong/article/details/25130555

一下內容來自參考:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 第一種:傳入參數僅有數組 
       <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的元素。

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2,使用hashmap傳參

         

雖然ibatai sql map可以配置多個參數,但sqlMap只能傳入一個參數,我們有兩種方式,一是把我們的參數封裝成一個類,通過set/get取值的方式給sql map注入參數,二是通過hashMap(可以組合一些不是同一個pojo的參數有優勢)

 

<select id="getPeopleList" resultClass="model.User" parameterClass="java.util.Map">
  <![CDATA[
     select * from test where name like '%$name$%'
  ]]>
</select>

 

   Map map=new HashMap();
   map.put("name", "gaoxiang");    key為參數名,value位數據
   List list = sqlMap.queryForList("getPeopleList", map);

在這里注意ibatis中#和$符號的區別:https://blog.csdn.net/geyouchao/article/details/51817747

https://blog.csdn.net/kiss_vicente/article/details/7602900

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM