MyBatis如何傳入多個參數


一、單個參數

mapper

public List<Test> getTestList(String id);

xml

<select id = "getTestList" parameterType = "java.lang.String" resultType = "com.test.Test">
  select t.* from test t where t.id = #{id} 
</select>

二、多個參數

1、使用索引

mapper

public List<Test> getTestList(String id, String name);

xml

<select id = "getTestList" resultType = "com.test.Test">
  select t.* from test t where t.id = #{0} and t.name = #{1}  
</select>

2、使用Map封裝多參數

mapper

public List<Test> getTestList(HashMap map);

xml

<select id = "getTestList" parameterType = "hashmap" resultType = "com.test.Test">
  select t.* from test t where t.id = #{id} and t.name= #{name}  
</select>  

#{}中的變量名要和map中的key對應。

3、使用注解

mapper

public List<Test> getTestList(@Param("id")int id, @Param("name")int name);

xml

<select id = "getTestList" resultMap = "com.test.Test">
   select t.* from test t where t.id = #{id} and t.name = #{name}
</select>

 

 

參考-致謝:

1、mingyue1818 ---- MyBatis傳入多個參數的問題

 


免責聲明!

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



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