zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    剑指offer系列40:构建乘积数组
    自学网站
    存储分析--- 转载
    程序员面试宝典第四版
    资源查找
    诗句
    fifo manage
    charlse抓包
    python基础之-----列表list工厂的仓库
    python内置函数积累
  • 原文地址:https://www.cnblogs.com/baby123/p/12580244.html
Copyright © 2011-2022 走看看