使用的是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.属性名 的方式获取对应值