例子工程下载链接: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数据库中建表充值的更多相关文章

  1. SpringBoot程序启动时执行初始化代码

    因项目集成了Redis缓存部分数据,需要在程序启动时将数据加载到Redis中,即初始化数据到Redis. 在SpringBoot项目下,即在容器初始化完毕后执行我们自己的初始化代码. 第一步:创建实现 ...

  2. 在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 ...

  3. SpringBoot项目启动时链接数据库很慢

    SpringBoot项目启动时链接数据库很慢 springboot项目在启动时候,如下图所示,链接数据库很慢 解决方法:在mysql 的配置文件中 配置 skip-name-resolve

  4. spingmvc实现在程序启动时调用数据库数据

    直接上代码: package com.java.zxf.servlet; import java.text.ParseException; import java.text.SimpleDateFor ...

  5. 如何在ASP.NET Core程序启动时运行异步任务(3)

    原文:Running async tasks on app startup in ASP.NET Core (Part 3) 作者:Andrew Lock 译者:Lamond Lu 之前我写了两篇有关 ...

  6. 如何在ASP.NET Core程序启动时运行异步任务(2)

    原文:Running async tasks on app startup in ASP.NET Core (Part 2) 作者:Andrew Lock 译者:Lamond Lu 在我的上一篇博客中 ...

  7. 如何在ASP.NET Core程序启动时运行异步任务(1)

    原文:Running async tasks on app startup in ASP.NET Core (Part 1) 作者:Andrew Lock 译者:Lamond Lu 背景 当我们做项目 ...

  8. SpringBoot在启动时的多环境配置以及加载顺序

    通常我们在开发完成一个SpringBoot项目时,总是要打包部署的. 在启动SpringBoot应用时,我们常常会使用命令java -jar xxx.jar来启动这个服务. 命令java -jar 除 ...

  9. Code First 迁移----官方 应用程序启动时自动升级(MigrateDatabaseToLatestVersion 初始值设定项)

    Code First 迁移 如果使用的是 Code First 工作流,推荐使用 Code First 迁移改进应用程序的数据库架构. 迁移提供一组允许以下操作的工具: 创建可用于 EF 模型的初始数 ...

随机推荐

  1. 【Python学习笔记】字符串拼接方法(5种)总结

    字符串的 5 种拼接方法: “+”号 “,”号 直接连接 格式化 多行字符串拼接 第一种:“+”号 print("Hello"+"Python") 打印结果: ...

  2. 强开企业付款到零钱与现金红包,无需等待90/30天,2-12H即可强开通!

    一.微信官方给出的,企业付款到零钱|现金红包开通的说明 针对入账方式为即时入账至商户号,结算周期为T+1的商户,需满足三个条件:1)入驻满90天,2)连续正常交易30天,3)保持正常健康交易.其余结算 ...

  3. 如何解决java高并发详细讲解

    对于我们开发的网站,如果网站的访问量非常大的话,那么我们就需要考虑相关的并发访问问题了.而并发问题是绝大部分的程序员头疼的问题, 但话又说回来了,既然逃避不掉,那我们就坦然面对吧~今天就让我们一起来研 ...

  4. IE6和IE11之间 表单提交 按钮设置了disabled属性

    JSP代码可以不看,就是一个表单,通过submit提交. <form action="mainAction.do?method=saveQuote" method=" ...

  5. springboot多环境部署(profile多环境支持)

    springboot多环境部署(profile多环境支持) 背景   项目开发过程中会有开发环境(dev),测试环境(test)和生产环境(prod),不同的环境需要配置不同的配置,profile提供 ...

  6. 每日一道 LeetCode (15):二进制求和

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  7. ansibleAPI怎么做异步

    在直接使用 ansible 时候有-B -p 参数可以启用异步操作,然后返回一个 job_id 值 [root@master ansible]# ansible node1 -B -P -m yum ...

  8. java 与 springboot

    package geektime.spring.springbucks; import geektime.spring.springbucks.model.Coffee; import geektim ...

  9. 怎么对比两个excel文档的数据差异

    百度经验: https://jingyan.baidu.com/article/6181c3e0877c7a152ef15304.html

  10. 浏览器自动化的一些体会9 访问angular页面的一个问题

    发现浏览器自动化有一个重要方面没有提及,即所谓的无页面浏览器,不过最近没有需求,不想尝试,先记上一笔,以后有需求时,可以有个思路. 大约一两个月前(现在比较懒散,时间不知不觉过去,连今天是几号有时候都 ...