spring+ibatis的公有DAO
2016-09-20 18:56
302 查看
这里用到的配置在这篇http://my.oschina.net/u/2274874/blog/639504里,这个公有的DAO也很简单,主要是继承了spring的SqlMapClientDaoSupport
下面是一个使用的例子,这里用到的是ibatis2.X的版本,不是mybatis3.X的版本
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; } }); } }
相关文章推荐
- 微服务框架Spring Cloud介绍 Part2: Spring Cloud与微服务
- spring aop 无法代理父类方法 的解决方法
- Spring MVC过滤器-超类 GenericFilterBean
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- java 中Spring task定时任务的深入理解
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 监听器获取Spring配置文件的方法
- Spring Boot 定制URL匹配规则的方法
- Java利用Sping框架编写RPC远程过程调用服务的教程