这可能与iBatis + spring有关 [英] is this possible to do with iBatis + spring

查看:75
本文介绍了这可能与iBatis + spring有关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是在弹簧支撑下使用ibatis的通常用法.或这就是我正在做的事情.请让我知道是否可以做一个更好的方法?

beans xml:

<bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/some/som1/my/mydb"/>
</bean>
<bean id="SqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation" value="classpath:sql-map-config-oracle.xml"/>
  <property name="dataSource" ref="DataSource"/>
</bean>
 <bean id="myDAO" class="com.reports.MyUserAccessDAO">
    <property name="sqlMapClient" ref="SqlMapClient"/>
    <property name="dataSource" ref="DataSource"/>
 </bean>

界面:

public interface MyUserAccessIface {
    public SomeBean getUserReports (String org);
}

DAO:

public class MyUserAccessDAO extends SqlMapClientDaoSupport implements MyUserAccessDAO {
    public SomeBean getUserReports (String org)
    {
      SomeBean bean = new SomeBean();
      //code for parameters goes here 
      getSqlMapClientTemplate().queryForList("namesp.userreport", parm);
      //fetch the result from parm and put them in SomeBean
      return bean
    }
}

致电DAO:

MyUserAccessIface iBatisDAO = 
(MyUserAccessIface) ApplicationInitializer.getApplicationContext().getBean("myDAO");

即使此方法工作正常,我也不了解接口的必要性.

问题

  • 可以从接口中取出接口吗? 图片,并且仍然可以正常工作吗?
  • 可以更改设计/设置,以便 对DAO的调用非常简单(即使需要基本抽象类也是如此)

    MyUserAccessDAO mydao =新的MyUserAccessDAO(); mydao.getUserReports("blah");

几天前我已经按照这些思路提出了问题,但是经过2天的苦苦挣扎并发现更多问题之后,我现在再次提出了这个问题.如果可能,请提供您要更改/添加的代码段.

此设计的单元测试无法正常进行,因为所有内容都位于容器内部.如果我可以解决问题,那么我也将其添加到问题中(以供参考).

此外,我认为对于试图使spring + ibatis起作用的人来说,这最终将是一个不错的起点.

就像我上面提到的那样,我想这样称呼我的DAO(或者我可以将一些内容作为构造函数参数传递给我):

MyUserAccessDAO mydao = new MyUserAccessDAO(); mydao.getUserReports("blah");

要实现上述目标,我需要在DAO中添加以下行

setSqlMapClient((SqlMapClient)ApplicationInitializer.getApplicationContext().getBean("SqlMapClient"));

仍然需要知道所有要覆盖的内容才能为此编写测试用例.测试用例将无法访问容器内的任何内容,因此将取决于驱动程序数据源...

由于我违反了此处的最佳做法...我不介意仅针对测试用例更改我的DAO ...

解决方案

接口可以从图片中取出并仍然可以正常工作吗?

不要拿出接口.它存在的理由很充分(例如,用于交易的代理生成,AOP,用于服务中测试的模拟等)

为什么要把它拿出来?它对您造成什么焦虑?

可以更改设计/设置,以便对DAO的调用很简单(即使需要基本抽象类)

您为什么称其为新"?如果您使用的是Spring,则应使用App Context注入它.如果您称新",则不受Spring的控制.那应该是重点.

usual idiom of using ibatis with spring support is following. Or this is how I'm doing it. please let me know if it can be done a better way?

beans xml:

<bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/some/som1/my/mydb"/>
</bean>
<bean id="SqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation" value="classpath:sql-map-config-oracle.xml"/>
  <property name="dataSource" ref="DataSource"/>
</bean>
 <bean id="myDAO" class="com.reports.MyUserAccessDAO">
    <property name="sqlMapClient" ref="SqlMapClient"/>
    <property name="dataSource" ref="DataSource"/>
 </bean>

interface:

public interface MyUserAccessIface {
    public SomeBean getUserReports (String org);
}

DAO:

public class MyUserAccessDAO extends SqlMapClientDaoSupport implements MyUserAccessDAO {
    public SomeBean getUserReports (String org)
    {
      SomeBean bean = new SomeBean();
      //code for parameters goes here 
      getSqlMapClientTemplate().queryForList("namesp.userreport", parm);
      //fetch the result from parm and put them in SomeBean
      return bean
    }
}

calling the DAO:

MyUserAccessIface iBatisDAO = 
(MyUserAccessIface) ApplicationInitializer.getApplicationContext().getBean("myDAO");

even though this works fine I dont understand the need for an interface.

Questions

  • Can the interface be taken out of the picture and still have this working?
  • can the design/settings be changed so the call to the DAO is simply (even if that requires a base abstract class)

    MyUserAccessDAO mydao = new MyUserAccessDAO(); mydao.getUserReports("blah");

I've asked question along these lines couple days back but after struggling for 2 days and finding more things out I asked this question again now. If possible please provide code snippets of what you'd change/add.

Unit testing with this design does not work because everything is residing inside the container. If I get that working, then I will be adding that to the question as well (for information purpose).

Also, I think for someone trying to make spring + ibatis work...this would end up being a good place to start.

Edit:

Like I mentioned above that I would like to call my DAO like this (or i'm fine with passing something along as constructor parameter):

MyUserAccessDAO mydao = new MyUserAccessDAO(); mydao.getUserReports("blah");

To achieve above I would have following line in my DAO

setSqlMapClient((SqlMapClient)ApplicationInitializer.getApplicationContext().getBean("SqlMapClient"));

Still need to know what all to overwrite to be able to write test case for this. Test case wont be able to access anything inside the container so it will depend on Driver Datasource...

Since I am going against the best practice here...i dont mind changing my DAO exclusively just for the test case...

解决方案

Can the interface be taken out of the picture and still have this working?

Don't take the interface out. It's there for a good reason (e.g., proxy generation for transactions, AOP, mocks for testing in services, etc.)

Why do you want to take it out? What angst is it causing for you?

Can the design/settings be changed so the call to the DAO is simply (even if that requires a base abstract class)

Why are you calling "new"? If you're using Spring, you should be injecting this using the App Context. If you call "new", it's not under Spring's control. That should be the whole point.

这篇关于这可能与iBatis + spring有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆