Spring批量导入期间的枚举到字符串转换失败

我正在使用 Spring Boot 和 Hiberna 创建一个 Spring 批处理作业,但在插入过程中遇到了问题。这是代码的相关部分:


@Bean

    public JdbcBatchItemWriter<OphthalmicLens> writer(DataSource dataSource) throws SQLException {

        return new JdbcBatchItemWriterBuilder<OphthalmicLens>()

                .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())

                .sql("INSERT INTO OphthalmicLens (`createdBy`,`createdDate`,`lastModifiedBy`,`lastModifiedDate`,`sid`,`version`,`manufacturer`,`manufacturerCode`,`name`,`sku`,`upc`,`cylinder`,`design`,`diameter`,`index`,`material`,`source`,`sphere`,`type`) VALUES (:createdBy,NOW(),:lastModifiedBy,NOW(),UUID(),:version,:manufacturer,:manufacturerCode,:name,:sku,:upc,:cylinder,:design,:diameter,:index,:material,:source,:sphere,:type)")

                .dataSource(dataSource).build();

    }

属性设计是一个枚举:


public enum OphthalmicDesign {

    SPHERIC, ASPHERIC, ATORIC, BIASPHERIC

}

这是在我的OphthalmicLens豆子中使用的:


@NotNull

    @Enumerated(EnumType.STRING)

    @Column(nullable = false)

    private OphthalmicDesign design = OphthalmicDesign.SPHERIC;

我正在使用 Mysql 5.7,并且数据库中的该列按预期作为 VARCHAR(255) 映射。

我理解问题是枚举未转换为字符串。我可以使用不同的方法来解决这个问题,BeanPropertyItemSqlParameterSourceProvider但我认为这太过分了,而且我会失去 Spring 设施的优势。


即使我使用 Spring Batch,您是否有提示告诉 Spring/Hibernate 自动将该枚举转换为字符串?


一只萌萌小番薯
浏览 188回答 1
1回答

蝴蝶刀刀

您可以指示BeanPropertyItemSqlParameterSourceProvider返回VARCHAR字段的类型,design如下所示:@Beanpublic JdbcBatchItemWriter<OphthalmicLens> writer(DataSource dataSource) throws SQLException {&nbsp; &nbsp; return new JdbcBatchItemWriterBuilder<OphthalmicLens>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<OphthalmicLens>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public SqlParameterSource createSqlParameterSource(OphthalmicLens item) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new BeanPropertySqlParameterSource(item) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public int getSqlType(String paramName) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (paramName.equalsIgnoreCase("desing")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Types.VARCHAR;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return super.getSqlType(paramName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sql("INSERT INTO OphthalmicLens (`createdBy`,`createdDate`,`lastModifiedBy`,`lastModifiedDate`,`sid`,`version`,`manufacturer`,`manufacturerCode`,`name`,`sku`,`upc`,`cylinder`,`design`,`diameter`,`index`,`material`,`source`,`sphere`,`type`) VALUES (:createdBy,NOW(),:lastModifiedBy,NOW(),UUID(),:version,:manufacturer,:manufacturerCode,:name,:sku,:upc,:cylinder,:design,:diameter,:index,:material,:source,:sphere,:type)")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .dataSource(dataSource)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java