Ibatis相关基础之一-为什么要用sqlMapClientTemplate
来源:互联网 发布:mac战网外服 编辑:程序博客网 时间:2024/06/05 14:11
Ibatis中最常用的类是SqlMapClientTemplate,它封装了SqlMapClient,我们在做数据库操作的时候通常都是用SqlMapClientTemplate类。通过如下配置文件:
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.sqlMapClientTemplate<span style="font-family: Arial, Helvetica, sans-serif;">"></span><property name="sqlMapClient" ref="sqlMapClient" /></bean><bean id="sqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"><property name="configLocation"><value>classpath:sqlmap.xml</value></property><property name="dataSource" ref="dataSource" /></bean>
就可以声明一个SqlMapClientTemplate的bean。
那么,为什么要用SqlMapClientTemplate而不使用SqlMapClient。因为
1)SqlMapClientTemplate可以帮我们统一抛出的异常类型为DataAccessException
2)管理Session的生命周期。每次执行CRUD操作时,都会调用execute(SqlMapClientCallback<T> action)方法,该方法负责创建并关闭session。
3)除此之外,如果使用SqlMapClient的insert/update/delete/query方法,它将会直接调用其父接口SqlMapExecutor的相应方法;而若使用SqlMapClientTemplate的insert/update/delete/query,将使用该类自己定义的相应方法,这些方法来自于 org.springframework.orm.ibatis.SqlMapClientTemplate这个类,是完全基于Spring的,而com.ibatis.sqlmap.client.SqlMapExecutor是基于ibatis的。
在批量操作时,为了提高性能,我们通常采用批量执行。那么,借助于SqlMapClientTemplate我们如何实现批量执行呢?
getSqlMapClientTemplate().execute(new SqlMapClientCallback() { public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { executor.startBatch(); executor.update("insertSomething", "myParamValue"); executor.update("insertSomethingElse", "myOtherParamValue"); executor.executeBatch(); return null; } });很简单吧。那么,以后再用Itatis时请使用SqlMapClientTemplate吧。
0 0
- Ibatis相关基础之一-为什么要用sqlMapClientTemplate
- ibatis--SqlMapClientTemplate用法
- SqlMapClientTemplate 的使用,ibatis+spring
- SqlMapClientTemplate对Ibatis的封装
- ibatis之SqlMapClientTemplate和SqlMapClient
- ibatis.SqlMapClientTemplate的使用集锦
- ibatis+spring+struts spring 外部注入 SqlMapClientTemplate
- Spring+iBatis整合(业务层聚合SqlMapClientTemplate)
- Ibatis中SqlMapClientTemplate和SqlMapClient的区别
- ibatis中SqlMapClientTemplate与SqlMapClient的区别
- Ibatis中SqlMapClientTemplate和SqlMapClient的区别
- SqlMapClientTemplate
- Spring框架学习【SqlMapClientTemplate对Ibatis的封装】
- spring技术内幕13-SqlMapClientTemplate对Ibatis的封装
- 数据仓库研究之一--为什么要用数据仓库
- iBATIS相关
- iBatis 相关
- iBatis基础
- 1、早期的多任务
- Linux学习笔记--目录处理命令----pwd ---打印当前目录
- Android Studio R文件出错
- 新浪微博自定义加入csdn
- ListView Item获取View
- Ibatis相关基础之一-为什么要用sqlMapClientTemplate
- Leetcode#24||Swap Nodes in Pairs
- 将十进制数转换为二进制数
- 有关win32信号量和事件在多线程使用的小例子
- POJ3186:Treats for the Cows 区间DP
- 如何使用Cocos2d-x 3.0制作基于tilemap的游戏:第一部分
- 《集体智慧编程》第5章一些问题
- KMP算法复习
- leetcode 106: Construct Binary Tree from Inorder and Postorder Traversal