List<Product> products = jdbcClient .sql("SELECT * FROM product WHERE price > :price") .param("price", 300000) .query(Product.class) .list();
Optional<Product> product = jdbcClient .sql("SELECT * FROM product WHERE id = :id") .param("id", 1) .query(Product.class) .optional();
int[] updateCounts = jdbcClient .sql("INSERT INTO product(model, price) VALUES(:model, :price)") .paramSource(products.stream() .map(p -> Params.of("model", p.getModel(), "price", p.getPrice()))) .batch();
// 单个结果查询 Product aitoM7 = jdbcClient .sql("SELECT * FROM product WHERE id = :id") .param("id", 1) .query(Product.class) .single(); // 列表查询 List<Product> aitoProducts = jdbcClient .sql("SELECT * FROM product WHERE brand = :brand") .param("brand", "AITO") .query(Product.class) .list();
int updatedRows = jdbcClient .sql("UPDATE product SET price = :price WHERE model = :model") .param("price", 320000) .param("model", "M7") .update();
@Configuration public class DatabaseConfig { @Bean public JdbcClient jdbcClient(DataSource dataSource) { return JdbcClient.create(dataSource); } }