spring-bootでh2を使う


[sample-spring-boot-h2](https://github.com/1206yaya/sample-spring-boot-h2)

```gradle:build.gradle
	implementation('org.springframework.boot:spring-boot-starter-data-jpa')
	runtimeOnly('org.springframework.boot:spring-boot-devtools')
	implementation('com.h2database:h2')
	testImplementation('org.springframework.boot:spring-boot-starter-test')
```
下記ファイルに永続化される

/src/main/resources/db/testdb

```properties:application.properties
spring.datasource.driver-class-name=org.h2.Driver
# [H2 Console] JDBC URL: jdbc:h2:C:\Users\aya-win10\engineering\projects\java\spring-boot\sample-spring-boot-h2\src\main\resources\db\testdb
# persistent database
spring.datasource.url=jdbc:h2:./src/main/resources/db/testdb;DB_CLOSE_ON_EXIT=TRUE;MODE=MySQL
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=false
```

```java:Customer.java
@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;

    protected Customer() {}

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }

}
```

```java:CustomerRepository
public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
}
```

```java:Application

@SpringBootApplication
public class Application {
	private static final Logger log = LoggerFactory.getLogger(Application.class);

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}


	@Bean
	public CommandLineRunner demo(CustomerRepository repository) {
		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
			repository.findById(1L)
				.ifPresent(customer -> {
					log.info("Customer found with findById(1L):");
					log.info("--------------------------------");
					log.info(customer.toString());
					log.info("");
				});

			// fetch customers by last name
			log.info("Customer found with findByLastName('Bauer'):");
			log.info("--------------------------------------------");
			repository.findByLastName("Bauer").forEach(bauer -> {
				log.info(bauer.toString());
			});
			// for (Customer bauer : repository.findByLastName("Bauer")) {
			// 	log.info(bauer.toString());
			// }
			log.info("");
		};
	}
}


```


![image.png](https://qiita-image-store.s3.amazonaws.com/0/33237/f8b45c52-1af1-2111-96fc-6da910fac55a.png)