项目启动后自动在数据库中创建数据表,插入初始化数据。
1.添加依赖,主要是flyway-core
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<swagger.version>2.9.2</swagger.version>
<swagger.bootstrap.ui>1.9.3</swagger.bootstrap.ui>
<lombok.version>1.18.8</lombok.version>
<fastjson.version>1.2.58</fastjson.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<maven-jar-plugin>3.1.0</maven-jar-plugin>
<feign.version>2.1.3.RELEASE</feign.version>
<ribbon.version>2.1.3.RELEASE</ribbon.version>
</properties>
<dependencies>
<!--1:springboot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--2:swagger的jar包-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${swagger.bootstrap.ui}</version>
</dependency>
<!--3:创建bean插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--工具类-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>cn.hiboot.mcn</groupId>
<artifactId>mcn-boot-autoconfigure</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.2.4</version>
</dependency>
</dependencies>
2.配置application.yml,主要是配置数据库连接信息,以及flyway.locations
server:
port: 10000
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test1?useUnicode=true&useJDBCCompliantTimezoneShift=true&serverTimeZone=UTC&useSSL=false
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
flyway:
locations: classpath:db_migration
3.在resources下创建db_migration目录,该目录下放sql语句,如下:
4.其中sql必须命名为V0.1__xx.sql格式。V后面的代表版本号,__是必须的,xx.sql是文件名
5.启动项目,就会在数据库中创建一个stu表,并且创建一个历史版本表:
6.如果启动项目,出现错误如下:
org.flywaydb.core.api.FlywayException: Found non-empty schema(s) `test1` wit
解决:在application.yml中添加:baseline-on-migrate
spring:
flyway:
locations: classpath:db_migration
baseline-on-migrate: true
然后运行项目,这时候会在数据库中创建一个 flyway_schema_history 但是里面的数据格式是不正确的,需要把这个表里的数据手动清空。然后再修改application.yml中的配置为:去掉:baseline-on-migrate
spring:
flyway:
locations: classpath:db_migration
再次运行项目即可。
7.如果修改了sql文件,需要先去数据库把对应的记录删除之后,才能运行项目,否则会报错。