解決Mybatis?mappe同時(shí)傳遞?List?和其他參數(shù)報(bào)錯(cuò)的問(wèn)題
問(wèn)題描述:
想要實(shí)現(xiàn)這個(gè)接口,同時(shí)傳入planId和projectPlans,屢次報(bào)錯(cuò)。好在一路坎坷,解決了多方bug,最后終于搞定了
void insertList(@Param("planId") Integer planId, @Param("projectPlans") List<ProjectPlan> projectPlans);
1 剛開(kāi)始爆綁定失敗
后來(lái)發(fā)現(xiàn)原來(lái)是我的xml文件沒(méi)有寫(xiě)mapper后綴
2 多個(gè)參數(shù)傳值用@Param失敗
別人說(shuō)用這個(gè),但是發(fā)現(xiàn)根本不行,
void insertList(@Param("planId") Integer planId, @Param("projectPlans") List<ProjectPlan> projectPlans);
<insert id="insertList" parameterType="java.util.Map"> insert into nursing_project_plan (plan_id,project_id,execute_time,execute_cycle, execute_frequency,create_by, update_by, create_time, update_time,remark) values <foreach collection="projectPlans" item="project" separator=","> (#{planId},#{project.projectId},#{project.executeTime},#{project.executeCycle}, #{project.executeFrequency},#{project.createBy},#{project.updateBy},#{project.createTime},#{project.updateTime},#{project.remark} ) </foreach> </insert>
解決思路
報(bào)錯(cuò)信息
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'customerIdList' not found. Available parameters are [arg0, collection, list]
解釋:
解釋:
當(dāng)我們傳遞一個(gè) List 實(shí)例或者數(shù)組作為參數(shù)對(duì)象傳給 MyBatis。當(dāng)你這么做的時(shí)候,MyBatis 會(huì)自動(dòng)將它包裝在一個(gè) Map 中,用名稱在作為鍵。List 實(shí)例將會(huì)以list 作為鍵,而數(shù)組實(shí)例將會(huì)以array作為鍵。所以,當(dāng)我們傳遞的是一個(gè)List集合時(shí),mybatis會(huì)自動(dòng)把我們的list集合包裝成以list為Key值的map。
錯(cuò)誤代碼:
mapper層
void insertList(List<ProjectPlan>projectPlans ); //這個(gè)方法也是會(huì)錯(cuò) void insertList(List<ProjectPlan>projectPlans ,Integer planId);
xml層
<insert id="insertList" > insert into nursing_project_plan (plan_id,project_id,execute_time,execute_cycle, execute_frequency,create_by, update_by, create_time, update_time,remark) values <foreach collection="projectPlans" item="project" separator=","> (#{planId},#{project.projectId},#{project.executeTime},#{project.executeCycle}, #{project.executeFrequency},#{project.createBy},#{project.updateBy},#{project.createTime},#{project.updateTime},#{project.remark} ) </foreach> </insert>
解決方法
service層
(封裝成map)
Map maps = new HashMap(); maps.put("projectPlans", projectPlans); maps.put("planId", 2); nursingProjectPlanMapper.insertList(maps);
mapper層
void insertList( Map maps);
xml層
<insert id="insertList" parameterType="java.util.Map"> insert into nursing_project_plan (plan_id,project_id,execute_time,execute_cycle, execute_frequency,create_by, update_by, create_time, update_time,remark) values <foreach collection="projectPlans" item="project" separator=","> (#{planId},#{project.projectId},#{project.executeTime},#{project.executeCycle}, #{project.executeFrequency},#{project.createBy},#{project.updateBy},#{project.createTime},#{project.updateTime},#{project.remark} ) </foreach> </insert>
成功了
到此這篇關(guān)于解決Mybatis mappe同時(shí)傳遞 List 和其他參數(shù)報(bào)錯(cuò)的問(wèn)題的文章就介紹到這了,更多相關(guān)Mybatis mappe傳遞 List 報(bào)錯(cuò)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決mybatis plus報(bào)錯(cuò)Invalid bound statement (not found):問(wèn)題
- MyBatis嵌套查詢collection報(bào)錯(cuò):org.apache.ibatis.exceptions.TooManyResultsException
- mybatis-plus報(bào)錯(cuò)Not Found TableInfoCache異常問(wèn)題
- 解決MybatisPlus批量插入數(shù)據(jù)報(bào)錯(cuò):Error getting generated key or setting result to parameter object問(wèn)題
- Mybatis提示Tag name expected的問(wèn)題及解決
相關(guān)文章
springboot整合Dubbo與Feign的實(shí)現(xiàn)?(無(wú)注冊(cè)中心)
本文主要介紹了springboot整合Dubbo與Feign的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04springboot項(xiàng)目中mybatis-plus@Mapper注入失敗問(wèn)題
這篇文章主要介紹了springboot項(xiàng)目中mybatis-plus@Mapper注入失敗問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07spring?boot?導(dǎo)出數(shù)據(jù)到excel的操作步驟(demo)
這篇文章主要介紹了spring?boot?導(dǎo)出數(shù)據(jù)到excel的實(shí)現(xiàn)步驟,文中通過(guò)打開(kāi)一個(gè)平時(shí)練習(xí)使用的springboot的demo給大家詳細(xì)介紹,需要的朋友可以參考下2022-03-03java把excel內(nèi)容上傳到mysql實(shí)例代碼
這篇文章主要介紹了java把excel內(nèi)容上傳到mysql實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01mybatis3.4.6 批量更新 foreach 遍歷map 的正確姿勢(shì)詳解
這篇文章主要介紹了mybatis3.4.6 批量更新 foreach 遍歷map 的正確姿勢(shì)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11