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 模型的初始数 ...
随机推荐
- Linux中C++提示‘close’ was not declared
C++socket编程时关闭socket和epoll时出现‘close’ was not declared,是程序头文件缺失导致的.缺失头文件#include <unistd.h>
- Spring Boot 应用程序启动流程分析
SpringBoot 有两个关键元素: @SpringBootApplicationSpringApplication 以及 run() 方法 SpringApplication 这个类应该算是 Sp ...
- 写给程序员的机器学习入门 (八 补充) - 使用 GPU 训练模型
在之前的文章中我训练模型都是使用的 CPU,因为家中黄脸婆不允许我浪费钱买电脑.终于的,附近一个废品回收站的朋友转让给我一台破烂旧电脑,所以我现在可以体验使用 GPU 训练模型了
- 深度学习 | sklearn的train_test_split()各函数参数含义解释(超级全)
在机器学习中,我们通常将原始数据按照比例分割为"测试集"和"训练集",从 sklearn.model_selection 中调用train_test_split ...
- Spring @Transactional事物配置无效原因
spring @transaction不起作用,Spring事物注意事项 1. 在需要事务管理的地方加@Transactional 注解.@Transactional 注解可以被应用于接口定义和接口方 ...
- Jmeter 常用函数(8)- 详解 __MD5
如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.html 作用 将指定的字符串 MD5 加密并返回,加 ...
- JavaScript学习系列博客_31_JavaScript Math 工具类
Math - Math属于一个工具类,它不需要我们创建对象(例如Date日期对象需要通过构造函数创建对象 var 变量=new Date(),Math不需要),它里边封装了属性运算相关的常量和方法 我 ...
- SparkSQL DSL开发(Old)
import org.apache.spark.sql.SQLContextimport org.apache.spark.sql.expressions.Windowimport org.apach ...
- dcoker 小应用(二)
sudo yum install epel-release vi /etc/yum.repos.d/epel.repo use base url instead of mirror url ...
- layui上传同一张图片第二次时choose没有反应
将上传文件的input的val设置为空 $("#test11").parent().find("input").val('');