spring+ibatis的公有DAO

这里用到的配置在这篇http://my.oschina.net/u/2274874/blog/639504里,这个公有的DAO也很简单,主要是继承了spring的SqlMapClientDaoSupport

public class BaseDao extends SqlMapClientDaoSupport {
    @Resource(name = "sqlMapClient")
    private SqlMapClient sqlMapClient;

    @PostConstruct
    public void initSqlMapClient() {
        super.setSqlMapClient(sqlMapClient);
    }
}

下面是一个使用的例子,这里用到的是ibatis2.X的版本,不是mybatis3.X的版本

@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;
            }
        });
    }
}

 

你可能感兴趣的:(DAO,spring,ibatis)