spring3-annotation-JdbcDaoSupport [英] spring3-annotation-JdbcDaoSupport

查看:85
本文介绍了spring3-annotation-JdbcDaoSupport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在dao中使用注释

@Repository("testDao")
public class TestDaoImpl extends JdbcDaoSupport implements BaseDao{

@Override
public Object addObject(String sqlid, Object obj) {
    // TODO Auto-generated method stub
    return null;
}

由以下原因引起:java.lang.IllegalArgumentException:需要'dataSource'或'jdbcTemplate'

Caused by: java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required

我不想使用:

<bean id="termsDao" class="com.manage.base.dao.impl.TestDaoImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>

此代码在xml中设置,"jdbcTemplate"已在其他"spring-xml"中定义.

this code set in xml, and "jdbcTemplate" has been defined in other "spring-xml"。

如何通过注释解决此问题:需要'dataSource'或'jdbcTemplate'"

How to solve this problem by an annotation :"'dataSource' or 'jdbcTemplate' is required"

推荐答案

您可以使用以下方法之一.第一个-最好使用/推荐使用dataSource,因为您不会在公共接口中公开SpringFramework类.两者都可以.

You can use one of the below approaches. The first one - taking a dataSource is preferred / recommended as you don't expose a SpringFramework class in your public interface. Both will work.

@Repository("testDao")
public class TestDaoImpl extends JdbcDaoSupport implements BaseDao{

  @Autowired
  TestDaoImpl(DataSource dataSource) {
    setDataSource(dataSource);
  }
}

@Repository("testDao")
public class TestDaoImpl extends JdbcDaoSupport implements BaseDao{

  @Autowired
  TestDaoImpl(JDBCTemplate template) {
    setJdbcTemplate(template);
  }
}

这篇关于spring3-annotation-JdbcDaoSupport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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