扩展jdbccursoritemreader时出现sql查询错误

mepcadol  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(352)

我有下面的itemreader类

@Configuration
    public class ReadingObjectItemReader extends JdbcCursorItemReader<Person> {

        public DataSource getDataSource() { return dataSource; }        
        @Resource
        public void setDataSource(DataSource dataSource) { super.setDataSource(dataSource); }

        @Autowired
        DataSource dataSource;

        @Bean
        public JdbcCursorItemReader<Person> cursorReader() {
            JdbcCursorItemReader<Person> databaseReader = new JdbcCursorItemReader<>();
            databaseReader.setDataSource(dataSource);
            databaseReader.setSql("select TOP(10) * from dbo.Person ");
            databaseReader.setFetchSize(5);
            databaseReader.setRowMapper(new BeanPropertyRowMapper<>(Person.class));
            return databaseReader;
        }
}

当我运行我的代码库时,上面的代码作为itemreader,我得到

Caused by: java.lang.IllegalArgumentException: The SQL query must be provided

以下是完整的堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readingObjectItemReader' defined in file [\demo-project\bin\main\com\spring-batch\test\config\ReadingObjectItemReader.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: The SQL query must be provided
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.1.BUILD-SNAPSHOT.jar:2.3.1.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.1.BUILD-SNAPSHOT.jar:2.3.1.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.1.BUILD-SNAPSHOT.jar:2.3.1.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.1.BUILD-SNAPSHOT.jar:2.3.1.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.1.BUILD-SNAPSHOT.jar:2.3.1.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.1.BUILD-SNAPSHOT.jar:2.3.1.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.1.BUILD-SNAPSHOT.jar:2.3.1.BUILD-SNAPSHOT]
    at gov.va.med.ccrs.pit.extract.CcrsPitextractApplication.main(CcrsPitextractApplication.java:12) ~[main/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.3.1.BUILD-SNAPSHOT.jar:2.3.1.BUILD-SNAPSHOT]
Caused by: java.lang.IllegalArgumentException: The SQL query must be provided
    at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.batch.item.database.JdbcCursorItemReader.afterPropertiesSet(JdbcCursorItemReader.java:108) ~[spring-batch-infrastructure-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at gov.va.med.ccrs.pit.extract.config.ReadingObjectItemReader.afterPropertiesSet(ReadingObjectItemReader.java:58) ~[main/:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    ... 22 common frames omitted

有什么我应该加入这个代码工作。提前谢谢。

uemypmqf

uemypmqf1#

通过不扩展jdbccursoritemreader并从我定义步骤的类中传递数据源,我能够实现这一点。
在做了更正之后,课堂看起来是这样的:

@Configuration
public class ReadingObjectItemReader {

    @Bean
    ItemReader<Person> cursorReaderForDb(DataSource dataSource) {
        String sql = "select TOP(10) * from dbo.Person ";
        JdbcCursorItemReader<Person> cursor = new JdbcCursorItemReader<Person>();
        cursor.setDataSource(dataSource);
        cursor.setSql(sql);
        cursor.setRowMapper(new BeanPropertyRowMapper<>(Person.class));
        cursor.setFetchSize(10);
        return cursor;
    }
}

我知道这是一种奇怪的行为。我仍然希望通过扩展jdbccursoritemreader来实现这一点。因此,任何建议,使其正确的是感谢。

相关问题