简介;
JdbcTemplate是Spring提供的一套JDBC模板框架,利用AOP技术解决直接使用JDBC带来的重复代码问题。它没有MyBatis使用那么灵活,但是却比直接使用JDBC方便得多。SpringBoot中对JdbcTemplate的使用提供了自动化配置类JdbcTemplateAutoConfiguration。
部分源码:
@Configuration @ConditionalOnClass({DataSource.class, JdbcTemplate.class}) @ConditionalOnSingleCandidate(DataSource.class) @AutoConfigureAfter({DataSourceAutoConfiguration.class}) @EnableConfigurationProperties({JdbcProperties.class}) public class JdbcTemplateAutoConfiguration { public JdbcTemplateAutoConfiguration() { } @Configuration static class JdbcTemplateConfiguration { private final DataSource dataSource; private final JdbcProperties properties; JdbcTemplateConfiguration(DataSource dataSource, JdbcProperties properties) { this.dataSource = dataSource; this.properties = properties; } @Bean @Primary @ConditionalOnMissingBean({JdbcOperations.class}) public JdbcTemplate jdbcTemplate() { JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource); Template template = this.properties.getTemplate(); jdbcTemplate.setFetchSize(template.getFetchSize()); jdbcTemplate.setMaxRows(template.getMaxRows()); if (template.getQueryTimeout() != null) { jdbcTemplate.setQueryTimeout((int)template.getQueryTimeout().getSeconds()); } return jdbcTemplate; } }
当classpath下存在DataSource和JdbcTemplate并且DataSource只有一个实例时,自动配置才会生效,若开发者没有提供JdbcOperations,则SpringBoot会自动向容器中注入一个JdbcTemplate(是JdbcOperations的子类)。
我们自己想用JdbcTemplate时,只需要提供JdbcTemplate和DataSource即可。
使用:
1.建表,插入数据
2.pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>
3.application.properties
4.实体类
5.Dao------》增+改+删+查+查询所有
@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)); } }
注意到上面,前三种都是update方法,jdbctemplate中增删改主要使用update和batchUpdate批处理方法,查询由query和queryForObject完成,此外execute方法可以用来执行任意的SQL,call方法执行存储过程等等自己可以了解。
6.Service层和Controller层
@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); } }
访问http://localhost:8080/bookOps
来源:https://www.cnblogs.com/crazy-lc/p/12336514.html