SpringBoot程序启动时在Oracle数据库中建表充值
例子工程下载链接: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)
查看数据库结果:

至此任务完成。
--2020-04-28--
SpringBoot程序启动时在Oracle数据库中建表充值的更多相关文章
- SpringBoot程序启动时执行初始化代码
因项目集成了Redis缓存部分数据,需要在程序启动时将数据加载到Redis中,即初始化数据到Redis. 在SpringBoot项目下,即在容器初始化完毕后执行我们自己的初始化代码. 第一步:创建实现 ...
- 在Oracle 11.2的数据库中建表时遇到 RESULT_CACHE (MODE DEFAULT) ORA-00922: missing or invalid option
在Oracle 11.2的数据库中建表时遇到 RESULT_CACHE (MODE DEFAULT) ORA-00922: missing or invalid option hostdr:[/ho ...
- SpringBoot项目启动时链接数据库很慢
SpringBoot项目启动时链接数据库很慢 springboot项目在启动时候,如下图所示,链接数据库很慢 解决方法:在mysql 的配置文件中 配置 skip-name-resolve
- spingmvc实现在程序启动时调用数据库数据
直接上代码: package com.java.zxf.servlet; import java.text.ParseException; import java.text.SimpleDateFor ...
- 如何在ASP.NET Core程序启动时运行异步任务(3)
原文:Running async tasks on app startup in ASP.NET Core (Part 3) 作者:Andrew Lock 译者:Lamond Lu 之前我写了两篇有关 ...
- 如何在ASP.NET Core程序启动时运行异步任务(2)
原文:Running async tasks on app startup in ASP.NET Core (Part 2) 作者:Andrew Lock 译者:Lamond Lu 在我的上一篇博客中 ...
- 如何在ASP.NET Core程序启动时运行异步任务(1)
原文:Running async tasks on app startup in ASP.NET Core (Part 1) 作者:Andrew Lock 译者:Lamond Lu 背景 当我们做项目 ...
- SpringBoot在启动时的多环境配置以及加载顺序
通常我们在开发完成一个SpringBoot项目时,总是要打包部署的. 在启动SpringBoot应用时,我们常常会使用命令java -jar xxx.jar来启动这个服务. 命令java -jar 除 ...
- Code First 迁移----官方 应用程序启动时自动升级(MigrateDatabaseToLatestVersion 初始值设定项)
Code First 迁移 如果使用的是 Code First 工作流,推荐使用 Code First 迁移改进应用程序的数据库架构. 迁移提供一组允许以下操作的工具: 创建可用于 EF 模型的初始数 ...
随机推荐
- easyPOI使用
更多的easyPOI资源的网在easypoi的官网. 1 在pom.xml中添加依赖 <dependency> <groupId>cn.afterturn</groupI ...
- find the lowest number location
before #设定路径列表Path def find_path2(heightmap, x, y, water_level=557,path=[]): #global path #设定坐标 右0 左 ...
- Python中json.dump与repr的区别
Json是一种轻量级的数据交换格式,Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它包含了两个函数: 引入json包: import json json.dumps(): ...
- AndroidStudio新建项目报错build failed
AndroidStudio新建项目报错build failed 报错信息 org.gradle.initialization.ReportedException: org.gradle.interna ...
- 搭建 WordPress 博客教程
搭建 WordPress 博客教程(超详细) 在 2018年7月29日 上张贴 由 suncent一条评论 本文转自:静候那一米阳光 链接:https://www.jianshu.com/p/5675 ...
- for语句——猜数字
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib. ...
- 【译】GitHub 为什么挂?官方的可行性报告为你解答
本文翻译自 GitHub 官方博客<Introducing the GitHub Availability Report> 原文链接:https://github.blog/2020-07 ...
- 数据库之Oracle优化技巧(二)
1.通过内部函数提高 SQL 效率 复杂的 SQL 往往牺牲了执行效率. 能够掌握上面的运用函数解决问题的方法在实际 工作中是非常有意义的 . 2.使用表的别名(Alias) 当在 SQL 语句中连接 ...
- SparkSQL /DataFrame /Spark RDD谁快?
如题所示,SparkSQL /DataFrame /Spark RDD谁快? 按照官方宣传以及大部分人的理解,SparkSQL和DataFrame虽然基于RDD,但是由于对RDD做了优化,所以性能会优 ...
- Redis设计与实现——多机数据库的实现
复制 旧版Redis的复制功能分为同步(sync)和命令传播两个操作. sync:是一个非常耗费资源的操作 命令传播 ...