這裏用到的配置在這篇http://my.oschina.net/u/2274874/blog/639504裏,這個公有的DAO也很簡單,主要是繼承了spring的SqlMapClientDaoSupportjava
public class BaseDao extends SqlMapClientDaoSupport { @Resource(name = "sqlMapClient") private SqlMapClient sqlMapClient; @PostConstruct public void initSqlMapClient() { super.setSqlMapClient(sqlMapClient); } }
下面是一個使用的例子,這裏用到的是ibatis2.X的版本,不是mybatis3.X的版本spring
@Service("TestDaoImpl") public class TestInfoDaoImpl extends BaseDao implements TestDao { public void deleteTestInfo(Map<String, Object> index) { this.getSqlMapClientTemplate().delete("deleteTestInfo", index); } public void insertTestInfo(TestInfo instance) { this.getSqlMapClientTemplate().insert("insertTestInfo", instance); } public List selectTestInfos(Map<String, Object> index) { return this.getSqlMapClientTemplate().queryForList("selectTestInfos", index); } // 查詢條數 public int selectTestInfosCount(<String, Object> index) { return (Integer) this.getSqlMapClientTemplate().queryForObject( "selectTestInfosCount", index); } public void updateTestInfo(Map<String, Object> index) { this.getSqlMapClientTemplate().update("updateTestInfo", index); } // ibatis自帶的分頁,查出結果集後再截取須要的記錄 public List selectTestInfos(Map<String, Object> index, int startrow, int pagesize) { return this.getSqlMapClientTemplate().queryForList("selectTestInfos", index, startrow, pagesize); } // 批量刪除,批量操做,只要把executor.delete("deleteTestInfo", locallist.get(i))換爲對應的寫法就能夠了。 // 好比executor.insert("insertTestInfo", locallist.get(i))就是批量插入了。 public void deleteTestInfos(List<Map<String, Object>> indexs) { final List<Map<String, Object>> locallist = indexs; getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>() { public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { executor.startBatch(); for (int i = 0, count = locallist.size(); i < count; i++) { executor.delete("deleteTestInfo", locallist.get(i)); if ((i + 1) % Constants.BATCH_DEAL_NUM == 0) { // 分批Batch,Constants.BATCH_DEAL_NUM爲批量提交的條數 executor.executeBatch(); executor.startBatch(); } } executor.executeBatch(); return null; } }); } }