如何在 jdbcTemplate.query 函数外声明一个 lambda 表达式?
how to declare a lambda expression outside jdbcTemplate.query function?
我有以下代码片段(取自Mykong tutorial: Spring Boot JDBC Examples):
public class JdbcBookRepository implements BookRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Book> findAll() {
return jdbcTemplate.query(
"select * from books",
(rs, rowNum) ->
new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
)
);
}
@Override
public List<Book> findByNameAndPrice(String name, BigDecimal price) {
return jdbcTemplate.query(
"select * from books where name like ? and price <= ?",
new Object[]{"%" + name + "%", price},
(rs, rowNum) ->
new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
)
);
}
}
您可以在此处查看重复代码:
(rs, rowNum) ->
new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
)
为了消除重复,我正在尝试重构此代码并使用函数式样式声明以下函数 - 如下所示:
BiFunction<ResultSet,Integer,Book> rowExtractor = (rs, rowNum) ->
new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
);
但是 Java 编译器抱怨:java: unreported exception java.sql.SQLException; must be caught or declared to be thrown
我的问题:是否可以使用函数式风格在 java 中以某种方式优雅地做到这一点?
我知道我可以像下面那样用“老方法”来做,但我想尝试功能接口:
RowMapper<Book> extractor1 = new RowMapper<>(){
@Override
public Object mapRow(ResultSet rs, int i) throws SQLException {
return new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
);
}
};
创建一个 RowMapper
如下,
RowMapper<Book> bookRowMapper = (rs, rowNum) ->
new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
);
并按如下方式使用,
jdbcTemplate.query("select * from books", bookRowMapper);
jdbcTemplate.query(
"select * from books where name like ? and price <= ?",
new Object[]{"%" + name + "%", price},
bookRowMapper
);
我有以下代码片段(取自Mykong tutorial: Spring Boot JDBC Examples):
public class JdbcBookRepository implements BookRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Book> findAll() {
return jdbcTemplate.query(
"select * from books",
(rs, rowNum) ->
new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
)
);
}
@Override
public List<Book> findByNameAndPrice(String name, BigDecimal price) {
return jdbcTemplate.query(
"select * from books where name like ? and price <= ?",
new Object[]{"%" + name + "%", price},
(rs, rowNum) ->
new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
)
);
}
}
您可以在此处查看重复代码:
(rs, rowNum) ->
new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
)
为了消除重复,我正在尝试重构此代码并使用函数式样式声明以下函数 - 如下所示:
BiFunction<ResultSet,Integer,Book> rowExtractor = (rs, rowNum) ->
new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
);
但是 Java 编译器抱怨:java: unreported exception java.sql.SQLException; must be caught or declared to be thrown
我的问题:是否可以使用函数式风格在 java 中以某种方式优雅地做到这一点?
我知道我可以像下面那样用“老方法”来做,但我想尝试功能接口:
RowMapper<Book> extractor1 = new RowMapper<>(){
@Override
public Object mapRow(ResultSet rs, int i) throws SQLException {
return new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
);
}
};
创建一个 RowMapper
如下,
RowMapper<Book> bookRowMapper = (rs, rowNum) ->
new Book(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
);
并按如下方式使用,
jdbcTemplate.query("select * from books", bookRowMapper);
jdbcTemplate.query(
"select * from books where name like ? and price <= ?",
new Object[]{"%" + name + "%", price},
bookRowMapper
);