Ibatis中SqlMapClientTemplate和SqlMapClient的区别

SqlMapClientTemplate是org.springframework.orm.ibatis下的

而SqlMapClient是ibatis的

SqlMapClientTemplate是SqlMapClient的封装类. 
SqlMapClient中包含着session的管理. 
SqlMapClientTemplate用于session的封装,以及异常的捕捉. 
所以按照以上的推断来说.应该尽量使用SqlMapClientTemplate. 
保证session以及Exception的正常以及统一.

下面是两种类型在spring中的集成配置及用法:、

SqlMapClientTemplate:

1、在spring中的配置

  1. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  2. <property name="dataSource" ref="dataSource" />
  3. <property name="configLocation">
  4. <value>classpath:/sqlmap-config.xml</value>
  5. </property>
  6. </bean>
  7. <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
  8. <property name="sqlMapClient" ref="sqlMapClient"></property>
  9. </bean>
  10. <bean id="accountDAO" class="com.raycloud.test.dao.AccountDAO">
  11. <property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
  12. </bean>

2、在dao中的实现

  1. @Repository//使用该注解相当于声明了一个bean
  2. public class AccountDAO {
  3. SqlMapClientTemplate sqlMapClientTemplate;
  4. public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
  5. this.sqlMapClientTemplate = sqlMapClientTemplate;
  6. }
  7. public Integer addAccount(Account account) throws SQLException{
  8. return (Integer)this.sqlMapClientTemplate.insert("Account.insertAccount", account);
  9. }
  10. }

SqlMapClient:
1、在spring中的配置

  1. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  2. <property name="dataSource" ref="dataSource" />
  3. <property name="configLocation">
  4. <value>classpath:/sqlmap-config.xml</value>
  5. </property>
  6. </bean>
  7. <bean id="accountDAO" class="com.raycloud.test.dao.AccountDAO">
  8. <property name="sqlMapClient" ref="sqlMapClient"></property>
  9. </bean>

2、在dao中的实现

  1. @Repository//使用该注解相当于声明了一个bean
  2. public class AccountDAO {
  3. SqlMapClient sqlMapClient;
  4. public void setSqlMapClient(SqlMapClient sqlMapClient) {
  5. this.sqlMapClient = sqlMapClient;
  6. }
  7. public Integer addAccount(Account account) throws SQLException{
  8. return (Integer)this.sqlMapClient.insert("Account.insertAccount", account);
  9. }
  10. }
上一篇:贴上整合ssm环境的代码


下一篇:mybatis-spring整合过程中AbstractMethodError