pom.xml
<!--JDBC--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</artifactId></dependency><!--Mysql驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><!--<version>5.1.49</version>--></dependency>
application.yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8
username: root
password: 941941
driver-class-name: com.mysql.cj.jdbc.Driver
#可不配,非必须
jdbc:
template:
query-timeout: 5 #查询数据 3 秒超时
测试:
package com.wzy.springbootweb02;
@Slf4j
@SpringBootTest
class SpringbootWeb02ApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
Long aLong = jdbcTemplate.queryForObject("select count(*) from student", Long.class);
log.info("记录总数{}",aLong);
}
}
结果为:
