MyBatis多參數傳遞之Map方式示例——MyBatis學習筆記之十三

      前面的文章介紹了MyBatis多參數傳遞的註解、參數默認命名等方式,今天介紹Map的方式。仍然之前面的分頁查詢教師信息的方法findTeacherByPage爲例(示例源代碼下載地址:http://down.51cto.com/data/546809)。 html

      首先修改映射器接口TeacherMapper中的findTeacherByPage方法以下: java

//分頁查詢教師信息
public List<Teacher> findTeacherByPage(Map<String, Object> map);

      相應地,這裏用到了Map接口,就應該引入此接口:import java.util.Map。 app

在執行類CollectionDemo中,調用findTeacherByPage方法的相關代碼以下: ide

Map<String, Object> params = new HashMap<String, Object>();
//以name字段升序排序,
params.put("sort", "name");
params.put("dir", "asc");
//查詢結果從第0條開始,查詢2條記錄
params.put("start", 0);
params.put("limit", 2);
//查詢職稱爲教授或副教授的教師
params.put("title", "%教授");
//分頁查詢教師信息
List<Teacher> teachers = mapper.findTeacherByPage(params);

      能夠看出,咱們先把參數放在了一個Map中,這樣咱們就能夠在相應的SQL語句中以#{…}的形式引用這些參數了。以下所示: 學習

<select id="findTeacherByPage" resultMap="supervisorResultMap"
parameterType="java.util.Map">
select * from teacher where title like #{title}
order by ${sort} ${dir} limit #{start},#{limit}
</select>

       與之前同樣,在order by子句中應使用${…}的方式。實際上,這裏的parameterType="java.util.Map"能夠不要。 spa

      運行結果以下: 日誌

170222296.png

      猛戳這裏全面系統地學習MyBatis 3 xml

      MyBatis技術交流羣:188972810,或掃描二維碼: htm

wKioL1SaztmBchKiAADsv4YAWBY259.jpg


【MyBatis學習筆記】系列之預備篇一:ant的下載與安裝 blog

【MyBatis學習筆記】系列之預備篇二:ant入門示例

【MyBatis學習筆記】系列之一:MyBatis入門示例

【MyBatis學習筆記】系列之二:MyBatis增刪改示例

【MyBatis學習筆記】系列之三:MyBatis的association示例

【MyBatis學習筆記】系列之四:MyBatis association的兩種形式

【MyBatis學習筆記】系列之五:MyBatis與Spring集成示例

【MyBatis學習筆記】系列之六:MyBatis與Spring集成示例續

【MyBatis學習筆記】系列之七:MyBatis一對多雙向關聯

【MyBatis學習筆記】系列之八:MyBatis MapperScannerConfigurer配置

【MyBatis學習筆記】系列之九:MyBatis collection的兩種形式

【MyBatis學習筆記】系列之十:MyBatis日誌之Log4j示例

【MyBatis學習筆記】系列之十一:MyBatis多參數傳遞之註解方式示例

【MyBatis學習筆記】系列之十二:MyBatis多參數傳遞之默認命名方式示例

【MyBatis學習筆記】系列之十三:MyBatis多參數傳遞之Map方式示例

【MyBatis學習筆記】系列之十四:MyBatis中的N+1問題

【MyBatis學習筆記】系列之十五:MyBatis多參數傳遞之混合方式

【MyBatis學習筆記】系列之十六:Spring聲明式事務管理示例

【MyBatis學習筆記】系列之十七:MyBatis多對多保存示例

【MyBatis學習筆記】系列之十八:MyBatis多對多關聯查詢示例

【MyBatis學習筆記】系列之十九:如何在MyBatis-3.2.7中使用Log4j2 rc2

MyBatis中如何經過繼承SqlSessionDaoSupport來編寫DAO(一)

MyBatis中如何經過繼承SqlSessionDaoSupport來編寫DAO(二)

相關文章
相關標籤/搜索