您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > mybatis多参数传递且其中一个参数传多个值用in
1.当查询的参数只有一个时findByIds(ListLongids)1.a如果参数的类型是List,则在使用时,collection属性要必须指定为listselectid=findByIdsMapresultMap=BaseResultMapSelectincluderefid=Base_Column_List/fromjriawhereIDinforeachitem=itemindex=indexcollection=listopen=(separator=,close=)#{item}/foreach/selectfindByIds(Long[]ids)1.b如果参数的类型是Array,则在使用时,collection属性要必须指定为arrayselectid=findByIdsMapresultMap=BaseResultMapselectincluderefid=Base_Column_List/fromjriawhereIDinforeachitem=itemindex=indexcollection=arrayopen=(separator=,close=)#{item}/foreach/select2.当查询的参数有多个时,例如findByIds(Stringname,Long[]ids)这种情况需要特别注意,在传参数时,一定要改用Map方式,这样在collection属性可以指定名称下面是一个示例MapString,Objectparams=newHashMapString,Object(2);params.put(name,name);params.put(ids,ids);mapper.findByIdsMap(params);selectid=findByIdsMapresultMap=BaseResultMapselectincluderefid=Base_Column_List/fromjriawhereIDinforeachitem=itemindex=indexcollection=idsopen=(separator=,close=)#{item}/foreach/select完整的示例如下:例如有一个查询功能,Mapper接口文件定义如下方法:ListJriafindByIds(Long...ids);使用in查询的sql拼装方法如下:selectid=findbyIdsresultMap=BaseResultMapselectincluderefid=Base_Column_List/fromjriawhereIDinforeachitem=itemindex=indexcollection=arrayopen=(separator=,close=)#{item}/foreach/select例子:Mapper.xml中!--根据库存状态和设备状态查询设备--selectid=selectByStockAndEquipmentStatusparameterType=TbiEquipmentresultMap=resultMapSELECTincluderefid=Base_Column_List/FROMt_bi_equipmentwhere0=0iftest=equipmentStockStatue!=nullandequipmentStockStatue!=''andEQUIPMENT_STOCK_STATUS=#{equipmentStockStatue,jdbcType=INTEGER}/ififtest=equipmentStatue!=nullandequipmentStatue!=''andEQUIPMENT_STATUSinforeachitem=itemindex=indexcollection=equipmentStatueopen=(separator=,close=)#{item}/foreach/if/selectMapper中ListTbiEquipmentselectByStockAndEquipmentStatus(MapString,Objectparams);Service中ListTbiEquipmentselectByStockAndEquipmentStatus(intequipmentStockStatue,int[]equipmentStatue);这个中的equipmentStockStatue和equipmentStatue要和mapper.xml中的参数名保持一致,且controller中也要保持一致ServiceImpl中@OverridepublicListTbiEquipmentselectByStockAndEquipmentStatus(intequipmentStockStatue,int[]equipmentStatue){MapString,Objectparams=newHashMapString,Object();params.put(equipmentStockStatue,equipmentStockStatue);params.put(equipmentStatue,equipmentStatue);returntbiEquipmentMapper.selectByStockAndEquipmentStatus(params);}Controller中tbiEquipmentService.selectByStockAndEquipmentStatus(equipmentStockStatue,equipmentStatue)equipmentStockStatue的值要定义,equipmentStatue也要定个集合,里面也得定义值
本文标题:mybatis多参数传递且其中一个参数传多个值用in
链接地址:https://www.777doc.com/doc-2889297 .html