SpringBoot2.x spring.datatsource的schema配置

本文主要介绍了在SpringBoot2.x中遇到的数据源初始化问题,当配置了schema属性但未自动创建表时,提出了解决方案——通过设置initialization-mode属性来指定初始化数据源模式,并给出了使用MySQL 8.x 和 Druid的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

配置了schema属性却没有自动创建表

    schema:
      - classpath:sql/department.sql
      - classpath:sql/employee.sql

解决方案

添加initialization-mode属性,配置初始化数据源的模式

initialization-mode: always

参考文档:
https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/howto.html#howto-initialize-a-database-using-spring-jdbc

示例

以下代码使用了 mysql 8.x + druid

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    #   数据源其他配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
    schema:
      - classpath:sql/department.sql
      - classpath:sql/employee.sql
    initialization-mode: always
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值