spring3-annotation-JdbcDaoSupport

wei*_*isd 3 spring dao jdbc

在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;
}
Run Code Online (Sandbox Code Playgroud)

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

我不想用?

<bean id="termsDao" class="com.manage.base.dao.impl.TestDaoImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

此代码在xml中设置?和“ jdbcTemplate”是否已在其他“ spring-xml”中定义?

如何通过注释解决此问题?“需要'dataSource'或'jdbcTemplate'”

gka*_*mal 5

您可以使用以下方法之一。第一个-采用dataSource是首选/推荐做法,因为您不会在公共接口中公开SpringFramework类。两者都会起作用。

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

  @Autowired
  TestDaoImpl(DataSource dataSource) {
    setDataSource(dataSource);
  }
}
Run Code Online (Sandbox Code Playgroud)

要么

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

  @Autowired
  TestDaoImpl(JDBCTemplate template) {
    setJdbcTemplate(template);
  }
}
Run Code Online (Sandbox Code Playgroud)