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

相关文章:

  • 2021-12-20
  • 2021-03-29
  • 2021-12-16
  • 2022-01-25
  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
  • 2022-01-07
猜你喜欢
  • 2021-08-25
  • 2021-09-30
  • 2021-05-19
  • 2021-12-30
  • 2021-11-29
  • 2021-09-24
  • 2021-10-12
相关资源
相似解决方案