jdbcTemplate.query(String sql,RowMapper<T>rm,Object....args);加上this::mapRowToIngredient和不加有什么区别
时间: 2023-05-19 08:05:09 浏览: 71
如果加上this::mapRowToIngredient,那么query方法会调用mapRowToIngredient方法来映射查询结果到Ingredient对象,否则会使用默认的映射方式。这个区别在于,如果你想自定义映射方式,那么就需要加上this::mapRowToIngredient。
相关问题
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` 中的元素列表。
jdbctemplate.query的使用
JdbcTemplate是Spring框架中用于简化JDBC操作的一个工具类,其中query()方法是其中常用的方法之一。该方法可以执行SQL查询语句,并将结果映射为Java对象。
以下是JdbcTemplate中query()方法的使用示例:
1.查询并返回List类型的结果
```java
List<Account> accountList = jdbcTemplate.query("SELECT * FROM account", new RowMapper<Account>() {
@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
Account account = new Account();
account.setId(resultSet.getInt("id"));
account.setUsername(resultSet.getString("username"));
account.setPassword(resultSet.getString("password"));
account.setBalance(resultSet.getDouble("balance"));
return account;
}
});
```
2.查询并返回单个对象
```java
Account account = jdbcTemplate.queryForObject("SELECT * FROM account WHERE id = ?", new Object[]{id}, new RowMapper<Account>() {
@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
Account account = new Account();
account.setId(resultSet.getInt("id"));
account.setUsername(resultSet.getString("username"));
account.setPassword(resultSet.getString("password"));
account.setBalance(resultSet.getDouble("balance"));
return account;
}
});
```
3.查询并返回单个值
```java
int count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM account", Integer.class);
```