MyBatis_傳入參數的問題


一、單個參數

  1、基本數據類型

    (1)直接使用

      List<ChargeRuleDO> tests(long id);
      <select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
        select * from t_charge_rule t where t.id = #{id}
      </select>
      #{}中的參數名與方法中的參數名一致

    (2)使用注解
      List<ChargeRuleDO> tests(@Param("aid") long bid);
      <select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
        select * from t_charge_rule t where t.id = #{aid}
      </select>
      #{}中的參數名與方法中的@Param()里的參數名一致

  2、復雜數據類型(這里主要是指Java實體類)
    (1)直接使用
      List<ChargeRuleDO> tests(TestQO testQO);
      <select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
        select * from t_charge_rule t where t.id = #{id} and t.rule_type=#{ruleType}
      </select>
      #{}中的參數名與方法中的參數的屬性名一致

    (2)使用注解
      List<ChargeRuleDO> tests(@Param("atestQO") TestQO btestQO);
      <select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
        select * from t_charge_rule t where t.id = #{atestQO.id} and t.rule_type=#{atestQO.ruleType}
      </select>
      #{}中的參數名與方法中的@Param()里的參數對應的屬性名一致,而且必須寫成"#{atestQO.id}"的格式,不能簡寫成"#{id}".

二、多個參數
  1、基本數據類型
    (1)直接使用
      List<ChargeRuleDO> tests(long id,String ruleType);
      <select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
        select * from t_charge_rule t where t.id = #{0} and t.rule_type=#{1}
      </select>
      這里改用#{index},其中,#{0}代表接收的是dao層中的第一個參數,#{1}代表dao層中第二參數,更多參數一致往后加即可。

    (2)使用注解
      List<ChargeRuleDO> tests(@Param("id") long id,@Param("ruleType") String ruleType);
      <select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
        select * from t_charge_rule t where t.id = #{id} and t.rule_type=#{ruleType}
      </select>
      #{}中的參數名與方法中的@Param()里的參數名一致

  2、復雜數據類型
    (1)直接使用
      嘗試了兩種取參數的方法都不行

    (2)使用注解
      List<ChargeRuleDO> tests(@Param("testQO")TestQO testQO,@Param("testQO2")TestQO2 testQO2);
      <select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
        select * from t_charge_rule t where t.id = #{testQO.id} and t.rule_type=#{testQO.ruleType} and t.user_id=#{testQO2.uesrId}
      </select>
      #{}中的參數名與方法中的@Param()里的參數對應的屬性名一致,而且必須寫成"#{testQO.id}"的格式,不能簡寫成"#{id}".

  3、基本數據類型與復雜數據類型混合使用
    因為在多參數的情況下,復雜數據類型不能直接使用,所以這里主要嘗試基本數據類型直接使用並且復雜數據類型使用注解這一種情況
    List<ChargeRuleDO> tests(long id,String ruleType,@Param("testQO2")TestQO2 testQO2);
    <select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
      select * from t_charge_rule t where t.id = #{0} and t.rule_type=#{1} and t.user_id=#{testQO2.uesrId}
    </select>

三、List參數
  List<ChargeRuleDO> tests(List<long> list);
  <select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
    select * from t_charge_rule t where t.id in
      <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
        #{item}
      </foreach>
  </select>

 

  以上是我遇到過的,並且曾經犯過錯誤的幾種方式.當然還有別的一些傳參方式,在這里我就不一一寫出來了,大家可以自己去百度,等遇到了,我再補充.


免責聲明!

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



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