mybatis使用foreach踩坑及解決
mybatis使用foreach踩坑記錄
在使用fireach是最關(guān)鍵的是collection屬性
dao層:
單參數(shù)List和多參數(shù)的list
/*通過表名(日期)查詢*/ List<WagesInfoVo> selectWagesBySheet(List<String> sheetNameList); /*通過用戶名+表名(日期)查詢*/ List<WagesInfoVo> selectByPerson(@Param("personNumber") String personNumber, @Param("sheetNameList") List<String> sheetNameList);
xml文件中foreach屬性的選擇
- 單參數(shù)的collection=“list”
- 多參數(shù)的collection=“sheetNameList”,sheetNameList為傳入的參數(shù)
<select id="selectWagesBySheet" parameterType="java.lang.String" resultType="com.zl.wagesmanage.vo.wages.WagesInfoVo"> select person_number as personNumber,user_name as userName, account_number as accountNumber,wage as wage,other_money as otherMoney,reward as reward, should as should,old as old,medical as medical,lose_job as loseJob,trade_union as tradeUnion, housing_fund as housingFund,take_off as takeOff,total as total,sheet_name as sheetName from tt_excel_wages where sheet_name in <foreach collection="list" index="index" item="sheetNameList" open="(" separator="," close=")"> #{sheetNameList} </foreach> </select> <select id="selectByPerson" parameterType="java.lang.String" resultType="com.zl.wagesmanage.vo.wages.WagesInfoVo"> select person_number as personNumber,user_name as userName, account_number as accountNumber,wage as wage,other_money as otherMoney,reward as reward, should as should,old as old,medical as medical,lose_job as loseJob,trade_union as tradeUnion, housing_fund as housingFund,take_off as takeOff,total as total,sheet_name as sheetName from tt_excel_wages where person_number=#{personNumber} and sheet_name in <foreach collection="sheetNameList" index="index" item="sheetNameList" open="(" separator="," close=")"> #{sheetNameList} </foreach> </select>
controller層傳參說明
這里我都是使用@RequestParam進(jìn)行傳參,列舉了get請求和post請求
/*通過excel的表名進(jìn)行查詢*/ @GetMapping(value = "getWages") @ResponseBody public ResponseVO getWages(@RequestParam("sheetNames")List<String> sheetNames){ try { WagesRequestVo wagesRequestVo =new WagesRequestVo(); //獲取數(shù)據(jù) List<WagesInfoVo> wagesInfoVos = wagesServer.getWagesBySheet(sheetNames); wagesRequestVo.setWagesInfoVoList(wagesInfoVos); if (wagesInfoVos==null){ return ResponseVO.appFail("查詢失敗"); } return ResponseVO.success(wagesRequestVo); }catch (Exception e){ return ResponseVO.serviceFail(e.getMessage()); } } /*通過用戶名和表名進(jìn)行查詢*/ @PostMapping("getWagesByNumber") @ResponseBody public ResponseVO getWagesByNumber(@RequestParam("personNumber")String personNumber, @RequestParam("sheetName[]")List<String> sheetName){ try { WagesRequestVo wagesRequestVo =new WagesRequestVo(); //獲取數(shù)據(jù) List<WagesInfoVo> wagesInfoVos = wagesServer.getWagesByPerson(personNumber,sheetName); wagesRequestVo.setWagesInfoVoList(wagesInfoVos); if (wagesInfoVos==null){ return ResponseVO.appFail("查詢失敗"); } return ResponseVO.success(wagesRequestVo); }catch (Exception e){ return ResponseVO.serviceFail(e.getMessage()); } }
postman截圖
- get請求傳參方式:
- post傳參方式:
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用Prometheus實(shí)現(xiàn)監(jiān)控
在當(dāng)今的軟件開發(fā)世界中,監(jiān)控是至關(guān)重要的一部分,本文主要介紹了如何在Spring Boot應(yīng)用程序中使用Prometheus進(jìn)行監(jiān)控,以幫助大家更好地理解和管理您的應(yīng)用程序,有需要的可以參考下2023-10-10關(guān)于 Java 的數(shù)據(jù)結(jié)構(gòu)鏈表
這篇文章主要介紹了關(guān)于 Java 的數(shù)據(jù)結(jié)構(gòu)鏈表的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容2021-09-09Spring Cloud 系列之負(fù)載均衡 Ribbon的示例代碼
Ribbon 是 Netflix 發(fā)布的負(fù)載均衡器,它有助于控制 HTTP 和 TCP 客戶端的行為。這篇文章主要介紹了Spring Cloud 系列之負(fù)載均衡 Ribbon的示例代碼,需要的朋友可以參考下2020-11-11Java中TreeSet、HashSet、Collection重寫比較器的實(shí)現(xiàn)
比較器是一種可以對集合或數(shù)組中的元素按照自定義的方式進(jìn)行排序的對象,本文主要介紹了Java中TreeSet、HashSet、Collection重寫比較器的實(shí)現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-08-08java中 利用正則表達(dá)式提取( )內(nèi)內(nèi)容
本篇文章,小編為大家介紹關(guān)于java中 利用正則表達(dá)式提取( )內(nèi)內(nèi)容,有需要的朋友可以參考一下2013-04-04