我正在尝试在Spring Boot 1.5.7版本中编写以下代码。
@Bean
public JdbcBatchItemWriter<WriterSO> writer(DataSource dataSource, ItemPreparedStatementSetter<WriterSO> setter) {
return new JdbcBatchItemWriterBuilder<WriterSO>()
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.itemPreparedStatementSetter(setter)
.sql("insert into writer (id, fullName, randomNum) values (?,?,?)").dataSource(dataSource).build();
}
但无法使用或导入属于JdbcBatchItemWriterBuilder的软件包。这个软件包在springboot 1.5.7中不可用?
下面是我在项目中使用的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.batch</groupId>
<artifactId>Batchwithpostgres</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Batchwithpostgres</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project
>
任何建议在Springboot 1.5.7中工作时编写备用代码的建议
答案 0 :(得分:0)
根据JdbcBatchItemWriterBuilder javadoc,它是在版本4中添加的,而Spring Boot 1.5.7使用的是Spring Batch的版本3,因此对您不可用。
您可以使用构造函数并设置所有必填字段:
@Bean
public JdbcBatchItemWriter<WriterSO> writer(DataSource dataSource, ItemPreparedStatementSetter<WriterSO> setter) {
JdbcBatchItemWriter<WriterSO> jdbcBatchItemWriter = new JdbcBatchItemWriter<>();
jdbcBatchItemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<WriterSO>());
jdbcBatchItemWriter.setItemPreparedStatementSetter(setter);
jdbcBatchItemWriter.setSql("insert into writer (id, fullName, randomNum) values (?,?,?)");
jdbcBatchItemWriter.setDataSource(dataSource);
return jdbcBatchItemWriter;
}
答案 1 :(得分:0)
Spring Boot 1.5.7带来了Spring Batch 3.0.8,它不为项目读取器和写入器提供构建器。
这些构建器是introduced in Spring Batch 4,可以使用Spring Boot 2.x来使用。