通过在application.properties文件中设置记录SQL日志
logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
记录Spring Data JDBC自动生成的增删改查CRUD分页、排序SQL语句
分页排序查询:
2022-10-11 10:44:14.162 DEBUG 8888 --- [nio-8080-exec-1] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL statement [SELECT `product`.`id` AS `id`, `product`.`name` AS `name`, `product`.`brand` AS `brand`, `product`.`price` AS `price`, `product`.`madein` AS `madein` FROM `product` ORDER BY `product`.`name` ASC LIMIT 0, 7]
2022-10-11 10:44:14.434 DEBUG 8888 --- [nio-8080-exec-1] o.s.jdbc.core.JdbcTemplate : Executing SQL query [SELECT COUNT(*) FROM `product`]
创建新记录:
2022-10-11 10:46:27.001 DEBUG 8888 --- [io-8080-exec-10] o.s.jdbc.core.JdbcTemplate : Executing SQL update and returning generated keys
2022-10-11 10:46:27.002 DEBUG 8888 --- [io-8080-exec-10] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL statement [INSERT INTO `product` (`brand`, `madein`, `name`, `price`) VALUES (?, ?, ?, ?)]
更新前显示更新表单:
2022-10-11 10:48:51.319 DEBUG 8888 --- [nio-8080-exec-9] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL query
2022-10-11 10:48:51.319 DEBUG 8888 --- [nio-8080-exec-9] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL statement [SELECT `product`.`id` AS `id`, `product`.`name` AS `name`, `product`.`brand` AS `brand`, `product`.`price` AS `price`, `product`.`madein` AS `madein` FROM `product` WHERE `product`.`id` = ?]
保存更新:
2022-10-11 10:49:57.972 DEBUG 8888 --- [nio-8080-exec-1] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL update
2022-10-11 10:49:57.972 DEBUG 8888 --- [nio-8080-exec-1] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL statement [UPDATE `product` SET `name` = ?, `brand` = ?, `madein` = ?, `price` = ? WHERE `product`.`id` = ?]
删除:
2022-10-11 10:51:12.537 DEBUG 8888 --- [nio-8080-exec-3] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL update
2022-10-11 10:51:12.537 DEBUG 8888 --- [nio-8080-exec-3] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL statement [DELETE FROM `product` WHERE `product`.`id` = ?]
可以看到Spring Data JDBC自动生成的增删改查CRUD分页、排序SQL语句非常简洁没有多余的SQL,和自己手写的一样简洁。