SpringBoot程序启动时在Oracle数据库中建表充值

时间:2021-01-25 19:24:36

例子工程下载链接:https://files.cnblogs.com/files/xiandedanteng/gatling20200428-1.zip

需求:在工程启动时在Oracle数据库中建表。

实现步骤:

1.在pom.xml中引入JPA和Oracle的依赖,Oracle不必多说,JPA则是一个规范化接口,封装了Hibernate作为默认实现,以让用户不用通过任何配置即可完成数据库的操作。

        <!-- JPA for create table and insert data -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <!-- Oracle jdbcdriver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.3.0.0</version>
</dependency>

2.在 application.properties 中写入以下内容,其中上半部分是数据库的连接信息,下半部分指示了在初始化是建表充值,有书籍如《SpringBoot实战派》P202中所说需要加入spring.datasource.initialize=true,但实际一用提示说已经强烈不赞成这么用了,用spring.datasource.initialization-mode=always就好:

spring.datasource.url=jdbc:oracle:thin:@dev-dm-logirtmsa101z.dev.ufo.local:2050/SV_TRTMSAPDB
spring.datasource.username=RTMSA_ufo
spring.datasource.password=test01
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=update
spring.datasource.schema=classpath:schema.sql
spring.datasource.data=classpath:data.sql
spring.jpa.show-sql=true

3.在src/main/resources下创建文件schema.sql,内容如下:

create TABLE hy_emp(
id number(4,0) primary key,
name nvarchar2(20) not null,
salary integer not null);

注意Oracle不是MySQL,使用DROP table if exists hy_emp的语法会报错,把它删除就好了。

4.在src/main/resources下创建文件data.sql,内容如下:

insert into hy_emp(id,name,salary) values('','Andy','');
insert into hy_emp(id,name,salary) values('','Bill','');
insert into hy_emp(id,name,salary) values('','Cindy','');
insert into hy_emp(id,name,salary) values('','Douglas','');
insert into hy_emp(id,name,salary) values('','Eliot','');
insert into hy_emp(id,name,salary) values('','Felix','');

这个文件是用来插值的。

启动:

2020-04-28 09:30:41.190 - Starting GatlingApplication on vdiw10bknan041 with PID 16516 (D:\Users\os-yang.he\git\gatling\target\classes started by os-yang.he in D:\Users\os-yang.he\git\gatling)
2020-04-28 09:30:41.198 - No active profile set, falling back to default profiles: default
2020-04-28 09:30:41.963 - Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-04-28 09:30:41.993 - Finished Spring Data repository scanning in 16ms. Found 0 JPA repository interfaces.
2020-04-28 09:30:42.838 - Tomcat initialized with port(s): 8080 (http)
2020-04-28 09:30:42.854 - Initializing ProtocolHandler ["http-nio-8080"]
2020-04-28 09:30:42.855 - Starting service [Tomcat]
2020-04-28 09:30:42.855 - Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-28 09:30:43.010 - Initializing Spring embedded WebApplicationContext
2020-04-28 09:30:43.011 - Root WebApplicationContext: initialization completed in 1738 ms
2020-04-28 09:30:43.253 - HikariPool-1 - Starting...
2020-04-28 09:30:44.301 - HikariPool-1 - Start completed.
2020-04-28 09:30:54.272 - HHH000204: Processing PersistenceUnitInfo [name: default]
2020-04-28 09:30:54.363 - HHH000412: Hibernate ORM core version 5.4.12.Final
2020-04-28 09:30:54.561 - HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-04-28 09:30:54.739 - HHH000400: Using dialect: org.hibernate.dialect.Oracle12cDialect
2020-04-28 09:31:00.330 - HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-04-28 09:31:00.339 - Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-04-28 09:31:00.390 - spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-04-28 09:31:00.542 - Initializing ExecutorService 'applicationTaskExecutor'
2020-04-28 09:31:00.775 - Starting ProtocolHandler ["http-nio-8080"]
2020-04-28 09:31:00.811 - Tomcat started on port(s): 8080 (http) with context path ''
2020-04-28 09:31:00.814 - Started GatlingApplication in 20.065 seconds (JVM running for 20.773)

查看数据库结果:

SpringBoot程序启动时在Oracle数据库中建表充值

至此任务完成。

--2020-04-28--