# For persistent storage without dropping the database when the app stops, choose the "update" option.
# For development mode where the database is created when the app starts and dropped when it stops, choose the "create-drop" option.
spring.jpa.hibernate.ddl-auto=update
# Vaadin_DB_URL、Vaadin_DB_USER、Vaadin_DB_PASSWORD為環境變數,需在OS內設定
# 你也可以直接在這裡填入值,只是安全上會有疑慮
# jdbc:mariadb://localhost:3306/dbname
spring.datasource.url=${Vaadin_DB_URL}
# db username
spring.datasource.username=${Vaadin_DB_USER}
# db password
spring.datasource.password=${Vaadin_DB_PASSWORD}
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
# newdatabase為資料庫名稱
CREATE DATABASE newdatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
# newdatabaseuser為資料庫使用者,password為資料庫使用者的密碼
CREATE USER 'newdatabaseuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON newdatabase.* TO 'newdatabaseuser'@'localhost';
FLUSH PRIVILEGES;
@Bean
public CommandLineRunner loadData(CustomerRepository repository) {
if(repository.count()==0){
return (args) -> {
// save a couple of customers
repository.save(new Customer("Jack", "Bauer"));
repository.save(new Customer("Chloe", "O'Brian"));
repository.save(new Customer("Kim", "Bauer"));
repository.save(new Customer("David", "Palmer"));
repository.save(new Customer("Michelle", "Dessler"));
// fetch all customers
log.info("Customers found with findAll():");
log.info("-------------------------------");
for (Customer customer : repository.findAll()) {
log.info(customer.toString());
}
log.info("");
// fetch an individual customer by ID
Customer customer = repository.findById(1L).get();
log.info("Customer found with findOne(1L):");
log.info("--------------------------------");
log.info(customer.toString());
log.info("");
// fetch customers by last name
log.info("Customer found with findByLastNameStartsWithIgnoreCase('Bauer'):");
log.info("--------------------------------------------");
for (Customer bauer : repository
.findByLastNameStartsWithIgnoreCase("Bauer")) {
log.info(bauer.toString());
}
log.info("");
};
}else{
return null;
}
}