使用的是SSM框架,數據庫是MySQL,做查詢的時候傳入List集合,使用SQL語句的in方式查詢數據
主要有兩點問題:我的List集合是利用的另外一個語句查詢出來的,傳入參數是int類型,返回值是int類型的List集合:
public List<Integer> select(Integer id); <select id="select" resultType="java.util.List" parameterType="java.lang.Integer"> select id from section where status='A' and unitId=#{id,jdbcType=INTEGER} </select>
這是我第一次的時候使用的返回值類型(java.util.List),這種情況下在我執行的時候會報錯:java.lang.UnsupportedOperationException
其實這里如果我們是要返回指定類型的集合直接寫java.lang.Integer(int類型)java.lang.String(字符串)等等就可以了,當然也可以自定義一個resultMap
<select id="select" resultType="java.lang.Integer" parameterType="java.lang.Integer"> select id from section where status='A' and unitId=#{id,jdbcType=INTEGER} </select>
上面是通過一個id查詢出List集合,下面是將查到的這個List集合放入查詢條件中:
public List<JUMember> selectById(List<Integer> id);
<select id="selectById" parameterType="java.util.List" resultMap="BaseResultMap">
select * from jumember where status = 'A' and id in
<foreach collection="list" index="index" item="item" open="("separator="," close=")">
#{item}
</foreach>
</select>
使用foreach 語句循環集合中的數據,item就是循環到的數據,如果你是一個復雜類型的數據做批量插入的話可以使用item.屬性名 的方式獲取對應值