Spring Boot 2.0 遷移手冊
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide
Spring Boot 2 初始化數據庫腳本 data.sql & schema.sql 必須提供三組同樣的用戶名稱和用戶密碼
這在官網和互聯網上目前沒人提及
application.yml正確的配置格式如下:
spring:
profiles: development
datasource:
url: jdbc:mysql://127.0.0.1:3306/dev?useSSL=false
username: root
password: dbpass
schema-username: root
schema-password: dbpass
data-username: root
data-password: dbpass
platform: mysql
schema: classpath:schema.sql
data: classpath:data.sql
continue-on-error: true
initialization-mode: always
再舉一範例
創建的表結構由jpa定義的對象實體映射生成,只定義表數據初始化文件data.sql:
application.yml
#security:
# oauth2:
# client:
# client-id: bd1c0a783ccdd1c9b9e4
# client-secret: 1a9030fbca47a5b2c28e92f19050bb77824b5ad1
# access-token-uri: https://github.com/login/oauth/access_token
# user-authorization-uri: https://github.com/login/oauth/authorize
# client-authentication-scheme: form
# resource:
# user-info-uri: https://api.github.com/user
# prefer-token-info: false
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db1?useSSL=false
username: root
password: 123456
data-username: root
data-password: 123456
initialization-mode: always
jpa:
hibernate:
ddl-auto: create
data.sql
INSERT INTO employee(id,name,birth,address)VALUES(1823,'Jack Chen','1993-05-29','Beijing');
INSERT INTO employee(id,name,birth,address)VALUES(1202,'Tim He','1986-12-26','Shanghai');
INSERT INTO employee(id,name,birth,address)VALUES(1304,'John Hu','1980-01-08','Shanxi');
Employee.java
package com.contoso.employee;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private Date birth;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
EmployeeRepository.java 允許有同名同姓的僱員
package com.contoso.employee;
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "employee", path = "employee")
public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long> {
List<Employee> findEmployeeByName(@Param("name") String name);
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.contoso</groupId>
<artifactId>spring-cloud-security</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-security</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>