ibatis配置文件in语句写法

示例中均省略了对集合的判空

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;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值