如何从 spring xml 设置 SqlMapClientTemplate

发布于 2024-09-16 00:25:27 字数 1463 浏览 0 评论 0原文

我有以下java课程。

package com.org.data.dbresource;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

public class DBConnectionManager {
    private SqlMapClientTemplate sqlMapClientTemplate;

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

    public SqlMapClientTemplate getSqlMapClientTemplate ()
    {
        return this.sqlMapClientTemplate;
    }
}

我的 Spring xml 如下所示:

 <bean id="IbatisDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/RSRC/app/oltp"/>
 </bean>

 <bean id="MySqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation" value="classpath:sql-map.xml"/>
  <property name="dataSource" ref="IbatisDataSource"/>
 </bean>

 <bean id="myObject" class="com.org.data.dbresource.DBConnectionManager">
    <property name="sqlMapClientTemplate" ref="MySqlMapClient"/>
 </bean>

我得到的错误是:

无法转换属性值 类型 [com.ibatis.sqlmap.engine.impl.SqlMapClientImpl] 至所需类型 [org.springframework.orm.ibatis.SqlMapClientTemplate] 对于属性“sqlMapClientTemplate”;

如果我传递的是 SqlMapClient 而不是 SqlMapClientTemplate,那么一切都会正常工作,但随后我必须显式捕获 SQLExceptions

我应该更改什么?

I've got following java class.

package com.org.data.dbresource;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

public class DBConnectionManager {
    private SqlMapClientTemplate sqlMapClientTemplate;

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

    public SqlMapClientTemplate getSqlMapClientTemplate ()
    {
        return this.sqlMapClientTemplate;
    }
}

My Spring xml looks like following:

 <bean id="IbatisDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/RSRC/app/oltp"/>
 </bean>

 <bean id="MySqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation" value="classpath:sql-map.xml"/>
  <property name="dataSource" ref="IbatisDataSource"/>
 </bean>

 <bean id="myObject" class="com.org.data.dbresource.DBConnectionManager">
    <property name="sqlMapClientTemplate" ref="MySqlMapClient"/>
 </bean>

Error I get is:

Failed to convert property value of
type
[com.ibatis.sqlmap.engine.impl.SqlMapClientImpl]
to required type
[org.springframework.orm.ibatis.SqlMapClientTemplate]
for property 'sqlMapClientTemplate';

Everything works fine if, instead of SqlMapClientTemplate I pass SqlMapClient but then I have to explicitly catch SQLExceptions

What should I change?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘你却要生生世世 2024-09-23 00:25:27

该错误说明了一切 - 您正在尝试将 SqlMapClient 类型的对象(由 SqlMapClientFactoryBean 创建)注入到 SqlMapClientTemplate 类型的属性中。

您需要自己手动实例化SqlMapClientTemplate,无论是在DBConnectionManager内,例如

private SqlMapClientTemplate sqlMapClientTemplate;

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

,然后

<bean id="myObject" class="com.org.data.dbresource.DBConnectionManager">
   <property name="sqlMapClient" ref="MySqlMapClient"/>
</bean>

记住,SqlMapClientTemplate只不过是一个帮助器类。 Spring和iBatis都没有强制要求使用它,如果你想使用它,你需要自己实例化它。

The error says it all - you're trying to inject an object of type SqlMapClient (as created by SqlMapClientFactoryBean) into a property of type SqlMapClientTemplate.

You need to manually instantiate the SqlMapClientTemplate yourself, either inside DBConnectionManager, e.g.

private SqlMapClientTemplate sqlMapClientTemplate;

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

and then

<bean id="myObject" class="com.org.data.dbresource.DBConnectionManager">
   <property name="sqlMapClient" ref="MySqlMapClient"/>
</bean>

Remember, SqlMapClientTemplate isw nothing more than a helper class. Neither Spring nor iBatis mandates its use, and if you want to use it, you need to instantiate it yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文