JdbcTemplate是Spring提供的一套JDBC模板框架,利用AOP技术来解决直接使用JDBC时大量重复代码的问题。JdbcTemplate虽然没有MyBatis那么灵活,但是比直接使用JDBC要方便很多。Spring Boot中对JdbcTemplate的使用提供了自动化配置类JdbcTemplateAutoConfiguration
当classpath下存在DataSource和JdbcTemplate并且DataSource只有一个实例时,自动配置才会生效,若开发者没有提供JdbcOperations,则Spring Boot会自动向容器中注入一个JdbcTemplate(JdbcTemplate是JdbcOperations的子类)。由此可以看到,开发者想要使用JdbcTemplate,只需要提供JdbcTemplate的依赖和DataSource依赖即可。
添加如下依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency>
spring-boot-starter-jdbc中提供了spring-jdbc,另外还加入了数据库驱动依赖和数据库连接池依赖。
数据库配置:
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/boot?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: aaa password: aaa
相关demo代码:
@Data public class Book { private Integer id; private String name; private String author; }
@Repository public class BookDao { @Autowired JdbcTemplate jdbcTemplate; public int addBook(Book book) { return jdbcTemplate.update("INSERT INTO book(name,author) VALUES (?,?)", book.getName(), book.getAuthor()); } public int updateBook(Book book) { return jdbcTemplate.update("UPDATE book SET name=?,author=? WHERE id=?", book.getName(), book.getAuthor(), book.getId()); } public int deleteBookById(Integer id) { return jdbcTemplate.update("DELETE FROM book WHERE id=?", id); } public Book getBookById(Integer id) { return jdbcTemplate.queryForObject("select * from book where id=?", new BeanPropertyRowMapper<>(Book.class), id); } public List<Book> getAllBooks() { return jdbcTemplate.query("select * from book", new BeanPropertyRowMapper<>(Book.class)); } }
@Service public class BookService { @Autowired BookDao bookDao; public int addBook(Book book) { return bookDao.addBook(book); } public int updateBook(Book book) { return bookDao.updateBook(book); } public int deleteBookById(Integer id) { return bookDao.deleteBookById(id); } public Book getBookById(Integer id) { return bookDao.getBookById(id); } public List<Book> getAllBooks() { return bookDao.getAllBooks(); } }
@RestController public class BookController { @Autowired BookService bookService; @GetMapping("/bookOps") public void bookOps() { Book b1 = new Book(); b1.setId(99); b1.setName("西厢记"); b1.setAuthor("王实甫"); int i = bookService.addBook(b1); System.out.println("addBook>>>" + i); Book b2 = new Book(); b2.setId(1); b2.setName("朝花夕拾"); b2.setAuthor("鲁迅"); int updateBook = bookService.updateBook(b2); System.out.println("updateBook>>>" + updateBook); Book b3 = bookService.getBookById(1); System.out.println("getBookById>>>" + b3); int delete = bookService.deleteBookById(2); System.out.println("deleteBookById>>>" + delete); List<Book> allBooks = bookService.getAllBooks(); System.out.println("getAllBooks>>>" + allBooks); } }
CREATE TABLE `book` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `author` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; INSERT INTO `boot`.`book` (`id`, `name`, `author`) VALUES ('1', '朝花夕拾', '鲁迅'); INSERT INTO `boot`.`book` (`id`, `name`, `author`) VALUES ('3', '西厢记', '王实甫');
查看文章:Spring Boot+Vue全栈开发实战 5.1 整合JdbcTemplate
更多文章: spring jdbcTemplate