在xml中,动态传参去数据库查询,下面是in的示例。
比如有条sql
SELECT *
FROM corp_tax c WHERE c.id
in (387419,387423) AND c.corp_year=2018 AND c.corp_month =6 and c.park_id =1 LIMIT 5
这条sql,如果要动态传参,xml的代码如下
<select id="tableFive" resultMap="twoToNine"> SELECT c.corp_name,c.month_tax_data as tax_data,c.month_fiscal_ret as fiscal_ret FROM corp_tax c WHERE c.id in <foreach collection="list" separator="," item="item" open="(" close=")"> #{item} </foreach> AND c.corp_year=#{year} AND c.corp_month =#{month} and c.park_id =#{parkId} LIMIT 5 </select>
mapper的接口为:
List<TwoToNine> tableFive(@Param("year") Integer year, @Param("month") Integer month, @Param("parkId") Integer parkId, @Param("list") List<Integer> list);
上面mapper中的参数要用@param绑定,要不然传入多个参数会报错。