Skip to content

使用数据库需要有相对应的依赖和数据源

依赖项:

xml
<!-- MySQL驱动 -->
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>
<!-- JDBC驱动 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

当项目中starter依赖了spring-boot-starter-jdbc或者spring-boot-starter-jdbc-jpa默认添加HikariCP连接池依赖,也就是默认使用HikariCP连接池

数据源配置:

yaml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xxx
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

JdbcTemplate