pom.xml

    1. <!--JDBC-->
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-data-jdbc</artifactId>
    5. </dependency>
    6. <!--Mysql驱动-->
    7. <dependency>
    8. <groupId>mysql</groupId>
    9. <artifactId>mysql-connector-java</artifactId>
    10. <!--<version>5.1.49</version>-->
    11. </dependency>

    application.yaml
    image.png

    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 秒超时
    

    测试:
    image.png

    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);
        }
    
    }
    

    结果为:
    image.png