网站建设比赛,上饶哪里可以学网站建设,郴州买房网站,手机如何创造网站在springboot中使用jdbc查询数据库列表时#xff0c;会出现数据库null转换的过程#xff0c;这个过程很容易出现意想不到的错误#xff1f;
比如#xff1a;使用场景中的jdbcTemplate要查询某列表
return jdbcTemplate.query(sql.getSql(), sql.getParamter(), new BeanP…在springboot中使用jdbc查询数据库列表时会出现数据库null转换的过程这个过程很容易出现意想不到的错误
比如使用场景中的jdbcTemplate要查询某列表
return jdbcTemplate.query(sql.getSql(), sql.getParamter(), new BeanPropertyRowMapper(OrderPojo.class));错误信息
org.springframework.beans.TypeMismatchException: Failed to convert property value of type null to required type double for property jfprice; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [io.swagger.annotations.ApiModelProperty double] for value null; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive typeat org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:600)at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:617)at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:464)at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:292)at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:260)at org.springframework.jdbc.core.BeanPropertyRowMapper.mapRow(BeanPropertyRowMapper.java:297)at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:697)at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633)at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:684)at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:716)at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:726)at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:776)at com.example.demo.repository.TestRepository.querList(TestRepository.java:110)这个错误提示 Failed to convert from type [null] to type [io.swagger.annotations.ApiModelProperty double] for value null 是由于 Spring 在查询结果中遇到 null 值时尝试将其转换为原始类型 double但原始类型double不能接受 null。在 Java 中原始类型如 double、int、boolean 等不能为 null因此会抛出这个异常。
解决方案
可以通过以下几种方法来解决这个问题 使用包装类型 使用 Double包装类代替 double原始类型。包装类 Double 可以接受 null 值而原始类型 double 不可以。 修改你的模型类将 double 类型的字段改为 Double 类型。 例如 [我是这个原因但是我没有去改这个类而是改了数据库底层的默认值] ApiModelProperty(百分比)
private Double pct; // 改为 Double这样Spring 就可以处理 null 值并将其设置为 null而不会抛出 ConversionFailedException。 使用 JsonInclude 来忽略 null 值 如果你想保留 double 类型并处理 null可以在字段上添加 JsonInclude(JsonInclude.Include.NON_NULL)这样如果字段的值为 null它将不会被映射到数据库查询结果中。 JsonInclude(JsonInclude.Include.NON_NULL)
private Double pct; // 使用 Double 类型然后你可以在查询时处理这些字段为 null。 确保数据库返回值不为 null 如果是因为数据库中的某些列值为 null你可以在查询时通过 SQL 或 ORM 层做转换。例如使用 COALESCE 或类似的函数将 null 值转换为默认值。 示例 SQL SELECT COALESCE(pct, 0.0) AS pct FROM your_table这将确保返回一个默认值 0.0 而不是 null。
总结
最推荐的做法是将字段类型从原始类型double改为包装类型Double因为这能兼容 null 值同时避免其他潜在的错误。在数据库层面你也可以确保返回值不为 null通过默认值处理。