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 模型的初始数 ...
随机推荐
- GitLab CI/CD 配置
GitLab CI/CD 配置 概念 持续集成的相关概念,可以看这篇文章 持续集成是什么? - 阮一峰的网络日志 操作示例 创建测试项目 sample-web,然后打开项目的 Runners 配置 找 ...
- Vue Slots
子组件vue <template> <div> <slot v-if="slots.header" name="header"&g ...
- homekit_四路继电器
这款继电器使用苹果手机进行控制,有普通版本和点动版本可供选择,有兴趣的可以去以下链接购买: https://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-1 ...
- Java爬取先知论坛文章
Java爬取先知论坛文章 0x00 前言 上篇文章写了部分爬虫代码,这里给出一个完整的爬取先知论坛文章代码. 0x01 代码实现 pom.xml加入依赖: <dependencies> & ...
- 图论算法(二)最短路算法:Floyd算法!
最短路算法(一) 最短路算法有三种形态:Floyd算法,Shortset Path Fast Algorithm(SPFA)算法,Dijkstra算法. 我个人打算分三次把这三个算法介绍完. (毕竟写 ...
- 输入url后的加载过程~
1)查找域名对应的IP地址: 2)建立连接(TCP的三次握手): 3)构建网页: 4)断开连接(TCP的四次挥手): TCP的三次握手:为了准确无误的把数据送到目标处,TCP协议采用了三次握手策略,用 ...
- Cannot instantiate the type ......的解决
使用public abstract class MainWindow implements ActionListener{} 之后创建对象MainWindow window = new MainWin ...
- IDEA中列编辑
快捷键 :Alt+Shift+insert,也可以按住Alt+Shift时,点击要编辑部分
- 2.Oracle数据库安装教程
一.准备安装 基本都是按部就班. 使用的OS版本:OEL4 安装程序路径: /mnt/Oracle11g_linux_x86_64/database 创建用户 使用的.bash_profile 修改的 ...
- node.js03 第一个node.js程序和读取文件
Hello World 1.创建运行 创建txt文件起名为hellonode,在记事本中编写JavaScript脚本文件 例如: var bbl = 'hellonode' console.log(b ...