val allList = jdbcTemplate.query(findSql.toString(), BeanPropertyRowMapper(JdInventoryNews::class.java)) 报错org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.haier.storetask.domain.JdInventoryNews]: No default constructor found; nested exception is java.lang.NoSuchMethodException
时间: 2024-02-18 22:03:01 浏览: 25
这个错误是因为你的JdInventoryNews类中没有无参构造函数,而在使用BeanPropertyRowMapper时,它需要使用无参构造函数来实例化对象。你可以在JdInventoryNews类中手动添加一个无参构造函数来解决这个问题。例如:
```
class JdInventoryNews {
// ...其他属性和构造函数
constructor() {
// 无参构造函数
}
}
```
相关问题
val allList = jdbcTemplate.query(findSql.toString(), BeanPropertyRowMapper(JdInventoryNews::class.java)) 报错org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.haier.storetask.domain.JdInventoryNews]: No default constructor found; nested exception is java.lang.NoSuchMethodException实体类已经添加了无参构造
如果你已经添加了无参构造函数,但是仍然出现这个错误,那么可能是因为你的JdInventoryNews类中的其他构造函数没有正确实现。
请检查JdInventoryNews类中的所有构造函数,确保它们都正确地初始化了所有属性。如果你的类中有其他构造函数,可能需要手动添加一个无参构造函数来解决这个问题。另外,你也可以尝试使用另一种构造函数来创建BeanPropertyRowMapper,例如:
```
val rowMapper = BeanPropertyRowMapper(JdInventoryNews::class.java, false)
val allList = jdbcTemplate.query(findSql.toString(), rowMapper)
```
在这种情况下,第二个参数`false`表示不要使用严格的属性名称匹配,这可能会导致找不到getter和setter方法而出现错误。
kotlin var mutableList: MutableList<JdInventoryNews> = list.toMutableList() 和 val allList: MutableList<JdInventoryNews> = jdbcTemplate.query(findSql.toString(), BeanPropertyRowMapper(JdInventoryNews::class.java)) 获取两个list的差值
可以使用 Kotlin 的扩展函数 `subtract` 来获取两个 `MutableList` 的差值,示例如下:
```kotlin
// 定义两个 MutableList
var mutableList: MutableList<JdInventoryNews> = list.toMutableList()
val allList: MutableList<JdInventoryNews> = jdbcTemplate.query(findSql.toString(), BeanPropertyRowMapper(JdInventoryNews::class.java))
// 获取两个 MutableList 的差值
val diffList = mutableList.subtract(allList)
```
这样,`diffList` 就是 `mutableList` 中不在 `allList` 中的元素列表。