SpringBoot中配置起动时的数据库初始化脚本

配置application.yml

连接字符串

spring:
  datasource:
    platform: mysql
    url: jdbc:mysql://localhost:3306/gov_admin?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 111111
    driver-class-name: com.mysql.jdbc.Driver
    sql-script-encoding: utf-8
    schema: classpath:schema.sql
    data: classpath:data.sql
    continue-on-error: true
    initialization-mode: always
    type: com.alibaba.druid.pool.DruidDataSource

初始化数据

spring.datasource.schema=classpath:schema.sql
spring.datasource.data=classpath:data.sql
spring.datasource.sql-script-encoding=utf-8
spring.datasource.initialization-mode=ALWAYS

因为SpringBoot在启动时,只有检测到spring.datasource.initialization-mode=ALWAYS配置,后再检测spring.datasource.schema之后,且配置的sql角本命令不为空,才会去执行schema和spring.datasource.data。因此需要在scheme.sql中随便写一句sql语句

创建schema.sql脚本

-- 系统组织表
CREATE TABLE IF NOT EXISTS `organization` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `address` varchar(255) DEFAULT NULL,
  `coords` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `fax` varchar(255) DEFAULT NULL,
  `key_values` varchar(255) DEFAULT NULL,
  `level` int(11) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `org_code` varchar(255) DEFAULT NULL,
  `parent_id` bigint(20) DEFAULT NULL,
  `phone_number` varchar(255) DEFAULT NULL,
  `pinyin` varchar(255) DEFAULT NULL,
  `region_id` bigint(20) DEFAULT NULL,
  `sort` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

创建date.sql脚本

-- 初始化系统组织
INSERT IGNORE INTO `organization` (`id`, `org_code`, `name`, `pinyin`, `parent_id`, `phone_number`, `fax`, `level`, `sort`, `region_id`, `address`, `key_values`) VALUES
    ('1', 'ce6019d8', '系统组织', NULL, '0', NULL, NULL, '1', '1', NULL, NULL, '{}');

-- 初始化系统管理员角色
INSERT IGNORE INTO `sys_role` (`id`, `name`, `type`, `value`, `organization_id`) VALUES
    ('1', '管理员', '0', NULL, '1');

-- 初始化系统菜单数据
INSERT IGNORE INTO `sys_resource` (`id`, `description`, `icon`, `name`, `parent_id`, `res`, `sort`, `type`, `levels`,`organization_id`, `visible`) VALUES
    ('1', '用户', 'glyphicon glyphicon-user icon', '用户', NULL, NULL, '1', '0', '1', '1', '1'),
    ('2', '账户管理', NULL, '账户管理', '1', 'app.sysuser', '1', '0', '2', '1', '1'),
    ('3', '角色管理', NULL, '角色管理', '1', 'app.sysrole', '2', '0', '2', '1', '1'),
    ('4', '组织管理', NULL, '组织管理', '1', 'app.organization', '3', '0', '2', '1', '1'),
    ('5', '日志', 'glyphicon glyphicon-edit icon', '日志', NULL, NULL, '8', '0', '1', '1', '1'),
    ('6', '浏览日志', NULL, '浏览日志', '5', 'app.browseLog', '1', '0', '2', '1', '1'),
    ('7', '操作日志', NULL, '操作日志', '5', 'app.operationLog', '2', '0', '2', '1', '1'),
    ('8', '设置', 'icon icon-settings icon', '设置', NULL, NULL, '10', '0', '1', '1', '1'),
    ('9', '全局设置', NULL, '全局设置', '8', 'app.globalConfig', '1', '0', '2', '1', '1'),
    ('10', '数据字典', NULL, '数据字典', '8', 'app.dicType', '2', '0', '2', '1', '1'),
    ('11', '局域管理', NULL, '局域管理', '8', 'app.administrativeArea', '3', '0', '2', '1', '1'),
    ('12', '菜单管理', NULL, '菜单管理', '8', 'app.sysResource', '4', '0', '2', '1', '1');
认真可以把事情做对,而用心却可以做到完美
原文地址:https://www.cnblogs.com/fangh816/p/13294246.html