MyBatis - 多參數的傳遞方式

    此章對 Dao 接口中 多個基礎類型參數、或單參數但爲 Bean / Map 等複雜類型、或者多個複雜類型參數 的情況

  • _parameter

    當 Dao 接口中定義的是一個複雜類型參數時,此時內置參數 _parameter 代表的是當前參數對應的複雜類型對象

    當 Dao 接口中定義的是多個參數時,多個參數會被封裝爲一個 map 類型,此時內置參數 _parameter 代表的是封裝了多個參數的 Map 對象

    簡而言之:接口定義爲單參數,則  _parameter 爲當前參數,接口定義爲多參數,則  _parameter 爲參數集的 Map 對象

  • 參數的說明

當單個基本參數時,未別名時,取值隨意命名,判斷必須 _parameter,如果通過 @Param 指定了命名,只能使用 命名;
當單個複雜參數時,未別名時,屬性取值和屬性判斷都是直接使用屬性名稱(若Map類型使用Key名稱),別名之後,都必須攜帶別名的前綴,即別名.屬性

當多個基本參數時,屬性的默認取值和判斷都爲 arg[index],別名之後直接使用 別名
當多個複雜參數時,屬性的默認取值和判斷都爲 arg[index].屬性名稱,別名後通過 別名.屬性
當多個任何參數時,可以在 Service 層封裝爲一個 Map 對象,然後以單參數的形式傳遞給 Dao 層

總而言之:單參數基礎類型,直接通過 _parameter/隨意命名 引用;單參數的複雜類型/自定實體,直接通過 屬性名稱 引用,Map 通過 key 引用;單參數別名、單參數Array/List、無論別名與否和參數類型的多參數,都將封裝爲 Map 對象,以別名(未別名以序號)獲取參數,要獲取參數的屬性時再通過 別名.屬性名稱 獲取

一、順序(索引)傳參

    在 XML 中,通過 #{arg+index}確定參數,索引從 0 開始;不使用 parameterType 屬性 或 指定爲 Map 類型

public interface StudentDao {
    
    public Student selectStudent(String name, Integer clsId);
    
}
<select id="selectStudent" resultType="com.xl.entity.Student">
    
    <!-- 以 MyBatis 3.4.6 的測試來看是 arg+Index 非 index -->
    SELECT * FROM student WHERE stu_name = #{arg0} AND cls_id = #{arg1}
    
</select>

二、@Param 註解傳參

    在 DAO 中直接通過 @Param 指定參數的名稱,在 XML 中使用此名稱;不使用 parameterType 屬性 或 指定爲 Map 類型

public interface StudentDao {
    
    public Student selectStudent(@Param("stuName") String name, @Param("clsId") Integer clsId);
    
}
<select id="selectStudent" resultType="com.xl.entity.Student">
    
    SELECT * FROM student WHERE stu_name = #{stuName} AND cls_id = #{clsId}
    
</select>

三、Java Map 傳參

    在 XML 中,使用 #{key} 指定 Map 裏面的 key 名稱,使用 parameterType 指定爲 Map 類型 或 不指定

public interface StudentDao {
    
    /**
     * Service:
     *     Map<String,Object> map = new HashMap<>();
     *     map.put("stuName", "zhangwu");
     *     map.put("clsId", "1001");
     */
    public Student selectStudent(Map<String, Object> map);
    
}
<select id="selectStudent" parameterType="java.util.Map" resultType="com.xl.entity.Student">
    
    SELECT * FROM student WHERE stu_name = #{stuName} AND cls_id = #{clsId}
    
</select>

四、Java Bean 傳參

    在 XML 中,使用 #{attr} 對應 Bean 裏面的成員屬性,使用 parameterType 指定爲 Bean 類型 或 不指定

public interface StudentDao {
    
    /**
     * Service:
     *     Student stu = new Student();
     *     stu.setName("zhangwu");
     *     stu.setClsId(1001);
     */
    public Student selectStudent(Student student);
    
}
<select id="selectStudent" parameterType="com.xl.entity.Student" resultType="com.xl.entity.Student">

    SELECT * FROM student WHERE stu_name = #{name} AND cls_id = #{clsId}
    
</select>

五、多個 Bean 傳參

  •     1、索引參數
public interface StudentDao {
    
    public Student selectStudent(Student student, Page page);
    
}
<select id="selectStudent" resultType="com.xl.entity.Student">

    SELECT * FROM student WHERE stu_name = #{arg0.name} AND cls_id = #{arg0.clsId} limit ${arg1.pageNum}, ${arg1.pageSize}

</select>
  •     2、別名參數
public interface StudentDao {
    
    public Student selectStudent(@Param("stu") Student student, @Param("page") Page page);
    
}
<select id="selectStudent" resultType="com.xl.entity.Student">

    SELECT * FROM student WHERE stu_name = #{stu.name} AND cls_id = #{stu.clsId} limit ${page.pageNum}, ${page.pageSize}

</select>

 

發佈了234 篇原創文章 · 獲贊 53 · 訪問量 49萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章