SpringBoot中配置起动时的数据库初始化角本
一、简介
我们使用SpringBoot + JPA时,需要程序在启动时执行数据表的初始化或者数据库记录的初始化。一般数据表的初始化可以通过在Spring Boot的application.properties中进行配置spring.jpa.hibernate.ddl-auto=update来实现。但是数据记录的初始化,该怎么做呢?
下面,我们将使用SpringBoot2.0.4Release版本做实验
二、配置application.properties
连接字符串角配置:
#连接字符串
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://172.16.4.203:3306/mydatabase?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.platform=org.hibernate.dialect.MySQL57Dialect
初始化数据:
#初始化数据
spring.datasource.schema=classpath:schema.sql
spring.datasource.data=classpath:data.sql
spring.datasource.sql-script-encoding=utf-
spring.datasource.initialization-mode=ALWAYS
三、说明
因为SpringBoot在启动时,只有检测到spring.datasource.initialization-mode=ALWAYS配置,后再检测spring.datasource.schema之后,且配置的sql角本命令不为空,才会去执行schema和spring.datasource.data。因此需要在scheme.sql中随便写一句sql语句。
schema.sql
-- 这里是定义数据结构的SQL,每次运行都会执行,此文件不能为空,必须至少写一句Sql语句。
show tables;
data.sql
-- 下列角本是同步服务系统每次启动会自动执行的角本。编写时,请注意确保角本中不会变更现有记录。 -- SettingTab
INSERT setting_tab(id,name,value,description) SELECT 1, 'name', 'admin','用户名' FROM dual WHERE not EXISTS (select 1 from setting_tab where setting_tab.id = 1);
INSERT setting_tab(id,name,value,description) SELECT 2, 'key', '','密钥' FROM dual WHERE not EXISTS (select 1 from setting_tab where setting_tab.id=2);
SpringBoot中配置起动时的数据库初始化角本的更多相关文章
- springboot中配置主从redis
测试redis的主从配置 redis实例 文件夹名称如下 redis_master_s redis_slaver1_s redis_slaver2_s redis.conf文件 master的redi ...
- web.xml中配置启动时加载的servlet,load-on-starup
web.xml中配置启动时加载的servlet,load-on-starup 使用servlet来初始化配置文件数据: 在servlet的配置当中,<load-on-startup>1&l ...
- 在Eclipse中配置Tomcat时,出现Cannot create a server using the selected type错误
在eclipse中配置Tomcat时,出现Cannot create a server using the selected type错误 原因:Tomcat被删除或者是重新安装,并且安装目录改变了. ...
- 在SpringBoot中配置aop
前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...
- SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理
SpringBoot中配置字符编码一共有两种方式 方式一: 使用传统的Spring提供的字符编码过滤器(和第二种比较,此方式复杂,由于时间原因这里先不介绍了,后续补上) 方式二(推荐使用) 在appl ...
- springboot 中使用Druid 数据源提供数据库监控
一.springboot 中注册 Servlet/Filter/Listener 的方式有两种,1 通过代码注册 ServletRegistrationBean. FilterRegistration ...
- SpringBoot中通过SpringBootServletInitializer如何实现容器初始化
相关文章 <Servlet3.0之四:动态注册和Servlet容器初始化> <SpringBoot中通过SpringBootServletInitializer如何实现组件加载> ...
- 在SpringBoot中配置定时任务
前言 之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单. ...
- springboot中配置过滤器以及可能出现的问题
在springboot添加过滤器有两种方式: 1.通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中需要增加c ...
随机推荐
- Oracle学习笔记--第2章 oracle 数据库体系结构
第2章 oracle 数据库体系结构 目录: ————————————— 2.1物理存储结构 2.1.1数据文件 2.2.2控制文件 2.1.3重做日志文件 2.1.4其他文件 2.2逻辑存储结构 2 ...
- socket.shutdownOutput()方法
我在用客户端给服务端发报文的时候,一开始客户端没有调用这个方法,结果服务端使用SAX解析报文的时候,public void endDocument() throws SAXException总是得不到 ...
- [转] SSO单点登录原理和流程分析
WEB的登录那些事#### 说道账户登录和注册,其实我们每天都在亲身感受着,像微博.知乎还有简书等等.我们总是需要定期的去重新登录一下,对于这种认证机制,我们都能说出来两个名词,Cookie.Sess ...
- Spring3.X jdk8 java.lang.IllegalArgumentException
异常提示: javax.servlet.ServletException: Servlet.init() for servlet springMVC threw exception org.apach ...
- python全栈开发day37-html
web准备总结: 结构标准:相当于人的身体.html就是用来制作网页的. 表现标准: 相当于人的衣服.css就是对网页进行美化的. 行为标准: 相当于人的动作.JS就是让网页动起来,具有生命力的 1. ...
- 052 自动将每日的日志增量导入到hive中
一:大纲介绍 1.导入方式 load data local inpath 'local_file_path' into table tbname partition (date='',hour='') ...
- P3719 [AHOI2017初中组]rexp
P3719 [AHOI2017初中组]rexp一开始想的是类似计算式子的值的东西,用栈.然后发现处理最大值很麻烦,因为处理的很像子过程,所以考虑递归来做.碰到'('就递归一次,碰到'|'就取最大值再递 ...
- odoo 模型继承
在odoo中有两种模型的继承机制(传统方式和委托继承方式) 重点:在__manifest__.py中找到depends,加上要继承的模块 'depends': ['account'] 注意继承的模型所 ...
- 模拟页面获取的php数据(一)
<?php return array( "aData" => array(//通勤方式 "trafficType" => array( 0 = ...
- Session失效后所有Ajax请求跳转登录地址
当登录的Session失效后,采用ajax请求数据时会没有反应,这时候应该自动跳转到登录页面,让用户重新登录. 全局配置以下可实现 $(function() { $.ajaxSetup({ compl ...