• spring boot2X使用schema.sql初始化数据库


    spring boot 版本

    2.2.5.RELEASE

    初始化文件 schema.sql 放在项目resources下

    drop table users if exists;
    drop table goods if exists;
    
    create table users (
        id bigint auto_increment,
        name varchar(255),
        create_time timestamp,
        primary key (id)
    );
    
    create table goods (
      id bigint auto_increment,
      name varchar(255),
      price bigint,
      create_time timestamp,
      update_time timestamp,
      primary key (id)
    );
    
    insert into users (name, create_time) values ('Lili', now());
    insert into users (name, create_time) values ('Fiona', now());
    
    insert into goods (name, price, create_time, update_time) values ('bag', 2000, now(), now());
    insert into goods (name, price, create_time, update_time) values ('bottole', 2500, now(), now());

    数据库H2

    依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

    配置

    spring.jpa.database=h2
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=none
    spring.jpa.open-in-view=false
    spring.datasource.url=jdbc:h2:./data/test
    spring.datasource.username=sa
    spring.datasource.password=123456
    spring.datasource.driverClassName=org.h2.Driver
    spring.h2.console.path=/h2-console
    spring.h2.console.enabled=true

    启动项目,数据库会按照schema.sql 进行初始化

    说明:

      spring.datasource.initialization-mode 的值默认为 embedded

      如果要在mysql下执行需要设置

        spring.datasource.initialization-mode=always

  • 相关阅读:
    jmeter之Dummy Sampler
    【转载】Jmeter之Bean shell使用(二)
    jmeter的关联-正则表达式的应用
    启动Jmeter录制代理进行录制,报 jmeter.protocol.http.proxy.ProxyControl
    Jmeter的三个线程组
    python3进行3des的加密解密
    python连接kafka-2.0
    python读取kafka,输出到Vertica数据库
    pip下载保存Python包,pip离线安装
    阿里云kafka使用记录(python版本)
  • 原文地址:https://www.cnblogs.com/baby123/p/12580244.html
Copyright © 2020-2023  润新知