示例中均省略了对集合的判空
1.参数为基本类型的数组、列表
<sqlMap namespace="author">
<typeAlias alias="book" type="Book" />
<resultMap class="book" id="bookResult">
<result property="id" column="id" />
<result property="bookName" column="bookName"/>
<result property="price" column="price" />
<result property="shelve" column="shelve" />
<result property="authorId" column="authorId" />
</resultMap>
<select id="findBooks" resultMap="bookResult">
SELECT id,authorId,bookName,price,shelve FROM book
WHERE id IN
<iterate open="(" close=")" conjunction="," >
#[]#
</iterate>
</select>
</sqlMap>
public List<Book> findBooks(Integer[] ids) throws SQLException ;
public List<Book> findBooks(List<Integer> ids) throws SQLException ;
2.参数为自定义类型的列表
<sqlMap namespace="author">
<typeAlias alias="book" type="Book" />
<resultMap class="book" id="bookResult">
<result property="id" column="id" />
<result property="bookName" column="bookName"/>
<result property="price" column="price" />
<result property="shelve" column="shelve" />
<result property="authorId" column="authorId" />
</resultMap>
<select id="findBooks" resultMap="bookResult" parameterClass="java.util.List">
SELECT id,authorId,bookName,price,shelve FROM book
WHERE id IN
<iterate open="(" close=")" conjunction="," >
#[].id#
</iterate>
</select>
</sqlMap>
public List<Book> findBooks(List<Book> books) throws SQLException;
3.参数为自定义类型的数组
需要配置自定义TypeHandlerCallback
<sqlMap namespace="author">
<typeAlias alias="book" type="Book" />
<resultMap class="book" id="bookResult">
<result property="id" column="id" />
<result property="bookName" column="bookName"/>
<result property="price" column="price" />
<result property="shelve" column="shelve" />
<result property="authorId" column="authorId" />
</resultMap>
<select id="findBooks" resultMap="bookResult">
SELECT id,authorId,bookName,price,shelve FROM book
WHERE id IN
<iterate open="(" close=")" conjunction="," >
#[]:id#
</iterate>
</select>
</sqlMap>
public List<Book> findBooks(Book[] books) throws SQLException;
4.参数为Map,基本类型的数组、列表放入Map中,假设ids为数组或列表的键
<sqlMap namespace="author">
<typeAlias alias="book" type="Book" />
<resultMap class="book" id="bookResult">
<result property="id" column="id" />
<result property="bookName" column="bookName"/>
<result property="price" column="price" />
<result property="shelve" column="shelve" />
<result property="authorId" column="authorId" />
</resultMap>
<select id="findBooks" resultMap="bookResult" parameterClass="java.util.Map">
SELECT id,authorId,bookName,price,shelve FROM book
WHERE id IN
<iterate open="(" close=")" conjunction ="," property="ids">
#ids[]#
</iterate>
</select>
</sqlMap>
public List<Book> findBooks(Map<String,Object> params) throws SQLException;
4.参数为Map,自定义类型的列表放入Map中,假设ids为列表的键
<sqlMap namespace="author">
<typeAlias alias="book" type="Book" />
<resultMap class="book" id="bookResult">
<result property="id" column="id" />
<result property="bookName" column="bookName"/>
<result property="price" column="price" />
<result property="shelve" column="shelve" />
<result property="authorId" column="authorId" />
</resultMap>
<select id="findBooks" resultMap="bookResult" parameterClass="java.util.Map">
SELECT id,authorId,bookName,price,shelve FROM book
WHERE id IN
<iterate open="(" close=")" conjunction ="," property="ids">
#ids[].id#
</iterate>
</select>
</sqlMap>
public List<Book> findBooks(Map<String,Object> params) throws SQLException;
5.参数为Map,自定义类型的数组放入Map中,假设ids为列表的键
需要配置自定义的TypeHandlerCallback
<sqlMap namespace="author">
<typeAlias alias="book" type="Book" />
<resultMap class="book" id="bookResult">
<result property="id" column="id" />
<result property="bookName" column="bookName"/>
<result property="price" column="price" />
<result property="shelve" column="shelve" />
<result property="authorId" column="authorId" />
</resultMap>
<select id="findBooks" resultMap="bookResult" parameterClass="java.util.Map">
SELECT id,authorId,bookName,price,shelve FROM book
WHERE id IN
<iterate open="(" close=")" conjunction ="," property="ids">
#ids[]:id#
</iterate>
</select>
</sqlMap>
public List<Book> findBooks(Map<String,Object> params) throws SQLException;
6.参数为字符串,不建议使用此方式,有sql注入的风险,此处不能使用#,否则只会返回第一个id的结果
<sqlMap namespace="author">
<typeAlias alias="book" type="Book" />
<resultMap class="book" id="bookResult">
<result property="id" column="id" />
<result property="bookName" column="bookName"/>
<result property="price" column="price" />
<result property="shelve" column="shelve" />
<result property="authorId" column="authorId" />
</resultMap>
<select id="findBooks" resultMap="bookResult" >
SELECT id,authorId,bookName,price,shelve FROM book
WHERE id IN ($ids$)
</select>
</sqlMap>
public List<Book> findBooks(String ids) throws SQLException;