Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 使用Spring和Kotlin检查JdbcTemplate中的可空列_Spring Boot_Kotlin_Jdbctemplate - Fatal编程技术网

Spring boot 使用Spring和Kotlin检查JdbcTemplate中的可空列

Spring boot 使用Spring和Kotlin检查JdbcTemplate中的可空列,spring-boot,kotlin,jdbctemplate,Spring Boot,Kotlin,Jdbctemplate,在使用Kotlin的Spring应用程序中,我正在读取一个sql表,其中有许多可为空的列。对于使用此if表达式的可为null的列I: if (rs.getObject("ordernumber") != null) rs.getInt("ordernumber") else null 有没有比为每个可为null的列编写if表达式更简单的方法 我已经将示例简化为一个可为null的列,但当然还有更多的可为null的列,包括String、Integer、T

在使用Kotlin的Spring应用程序中,我正在读取一个sql表,其中有许多可为空的列。对于使用此if表达式的可为null的列I:

  if (rs.getObject("ordernumber") != null) rs.getInt("ordernumber") else null
有没有比为每个可为null的列编写if表达式更简单的方法

我已经将示例简化为一个可为null的列,但当然还有更多的可为null的列,包括String、Integer、Timestamp等等

    fun getEmployeePayRecord(employee: Employee): List<EmployeePayRecord> {

        val rowMapper: RowMapper<EmployeePayRecord> = RowMapper { rs, _ ->
            EmployeePayRecord(
                uuid = rs.getString("uuid"),
                workingDay = rs.getTimestamp("working_day").toLocalDateTime(),
                orderNumber = if (rs.getObject("ordernumber") != null) rs.getInt("ordernumber") else null
            )
        }

        return jdbcTemplate.query(
            """select uuid
                    , working_day
                    , ordernumber
                 from plrv11.employee_pay_record
                where employee_number = :employeeNumber
                order by working_day
            """, rowMapper, employee.employeeNumber
        ).toList()
    }

我建议使用helper方法并使用
wasNull
,而不是执行两次get。类似于
intresult=rs.getInt(“ordernumber”);返回rs.wasNull()?无效的结果。而且
query
已经返回了一个列表,那么为什么
toList
?如果您使用
queryForStream
并执行
toList
操作,这就超出了目的,您可以只使用常规的
查询
。@M.Deinum-toList()确实是多余的。我把它扔掉了。非常感谢你。我完全忽略了它。@M.Deinum辅助函数必须是一个泛型函数,因为我有字符串、整数、大小数和时间戳。helper函数应该能够处理所有类型。嗯,很棘手。不,你应该为不同的类型提供多个帮助函数,而不是将其硬塞进一个方法中。为了获得额外的优势,你可以将你的
getXXXOrNull
转换成
ResultSet
上的扩展函数,这样你就可以调用:`orderNumber=rs.getIntOrNull(“orderNumber”)
fun ResultSet.getIntOrNull(columnName: String): Int? {
    val result = getInt(columnName)
    return if (wasNull()) null else result
}