`

ibatis批量删除

阅读更多

 

在《ibatis简单应用》相应文件中加入以下内容:

TestDao:

<!--第一种方法-->

 

public void deleteSome(String id) throws Exception;

<!--第二种方法-->

public void deleteSome(List<String>list) throws Exception;


ITestDaoImpl:

<!--第一种方法-->

 

public void deleteSome(String id) throws Exception {

sqlMapClient.delete("deleteSome1", id);

}

<!--第二种方法-->

public void deleteSome(List<String> list) throws Exception {

sqlMapClient.delete("deleteSome2",list);

}

 

Test.xml:

<!--第一种方法-->

 

<delete id="deleteSome1" parameterClass="String">

delete from test where id in ($id:String$)

</delete>

<!--第二种方法-->

<delete id="deleteSome2" parameterClass="java.util.List">

 delete from test

   <!--conjunction="OR"也可以表示为conjunction=","-->

  <iterate prepend="WHERE" open="(" close=")" conjunction="OR">

id = #list[]#

 </iterate>

</delete>


 

main方法测试

//第一种方法

new ITestDaoImpl().deleteSome("12,13");  //只要把id用“,”连接字符串即可

//第二种方法

 

List<String>idList= new ArrayList<String>();

idList.add("14");

idList.add("15");

new ITestDaoImpl().deleteSome(idList);

 

spring也提供了批量更新的方法

 

this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {  

public Object doInSqlMapClient(SqlMapExecutor executor)  

throws SQLException { 

Map<String,Object> temp = new HashMap<String,Object>();  

executor.startBatch();  

for(Long eachID : sampleIDList) {   

executor.update("update",temp);  

}  

executor.executeBatch();  

return null;  

});  

}

就像hibernate中一样,是使用模板调用了一个回调函数,而参数类型parameterClass="java.util.Map"。

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics