Ibatis中SqlMapClientTemplate和SqlMapClient的区别

 我来答
greystar_cn
2016-01-18 · 知道合伙人软件行家
greystar_cn
知道合伙人软件行家
采纳数:16407 获赞数:17260
本人主要从事.NET C#方向的技术开发工作,具有10多年的各类架构开发工作经验。

向TA提问 私信TA
展开全部
SqlMapClientTemplate是org.springframework.orm.ibatis下的
而SqlMapClient是ibatis的

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

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

1、在spring中的配置

[html] view plaincopyprint?
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:/sqlmap-config.xml</value>
</property>
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<bean id="accountDAO" class="com.raycloud.test.dao.AccountDAO">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:/sqlmap-config.xml</value>
</property>
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<bean id="accountDAO" class="com.raycloud.test.dao.AccountDAO">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>
2、在dao中的实现

[java] view plaincopyprint?
@Repository//使用该注解相当于声明了一个bean
public class AccountDAO {

SqlMapClientTemplate sqlMapClientTemplate;

public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}

public Integer addAccount(Account account) throws SQLException{
return (Integer)this.sqlMapClientTemplate.insert("Account.insertAccount", account);
}
}
@Repository//使用该注解相当于声明了一个bean
public class AccountDAO {

SqlMapClientTemplate sqlMapClientTemplate;

public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}

public Integer addAccount(Account account) throws SQLException{
return (Integer)this.sqlMapClientTemplate.insert("Account.insertAccount", account);
}
}

SqlMapClient:
1、在spring中的配置

[html] view plaincopyprint?
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:/sqlmap-config.xml</value>
</property>
</bean>
<bean id="accountDAO" class="com.raycloud.test.dao.AccountDAO">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:/sqlmap-config.xml</value>
</property>
</bean>
<bean id="accountDAO" class="com.raycloud.test.dao.AccountDAO">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>

2、在dao中的实现

[java] view plaincopyprint?
@Repository//使用该注解相当于声明了一个bean
public class AccountDAO {

SqlMapClient sqlMapClient;

public void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}

public Integer addAccount(Account account) throws SQLException{
return (Integer)this.sqlMapClient.insert("Account.insertAccount", account);
}
}
@Repository//使用该注解相当于声明了一个bean
public class AccountDAO {

SqlMapClient sqlMapClient;

public void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}

public Integer addAccount(Account account) throws SQLException{
return (Integer)this.sqlMapClient.insert("Account.insertAccount", account);
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式