- Hands-On Full Stack Development with Spring Boot 2.0 and React
- Juha Hinkula
- 263字
- 2021-06-25 21:00:24
Setting up the MariaDB database
Now, we switch our database from H2 to MariaDB. The database tables are still created automatically by JPA. But before we run our application, we have to create a database for it. The database can be created by using HeidiSQL. Open HeidiSQL, and follow these steps:
- Right-click your mouse inside the database list.
- Then, select New | Database:

- Let's name our database cardb. After you press OK, you should see the new cardb in the database list:

- In the application, we add a MariaDB dependency to the pom.xml file and remove the H2 dependency that we don't need any more:
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
- In the application.properties file, you define the database connection. First, you will define the database's url, username, password and database driver class. The spring.jpa.generate-ddl setting defines whether JPA should initialize the database (true/false). The spring.jpa.hibernate.ddl-auto setting defines the behavior of the database initialization. The possible values are none, validate, update, create, and create-drop. Create-drop means that the database is created when an application starts and it is dropped when the application is stopped. Create-drop is also a default value if you don't define any. Create value only creates the database when the application is started. Update value creates the database and updates the schema if it is changed:
spring.datasource.url=jdbc:mariadb://localhost:3306/cardb
spring.datasource.username=root
spring.datasource.password=YOUR_PASSWORD
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
- Now, after running the application, you should see the tables in MariaDB. The following screenshot shows the HeidiSQL UI after the database has been created. Your application is now ready to be used with MariaDB:

推薦閱讀
- Learning Microsoft Windows Server 2012 Dynamic Access Control
- Java語言程序設計
- 在最好的年紀學Python:小學生趣味編程
- Web Application Development with MEAN
- 高級C/C++編譯技術(典藏版)
- Python忍者秘籍
- MongoDB,Express,Angular,and Node.js Fundamentals
- SQL Server 2008 R2數據庫技術及應用(第3版)
- 從Power BI到Analysis Services:企業級數據分析實戰
- The Statistics and Calculus with Python Workshop
- Kotlin語言實例精解
- 企業級Java現代化:寫給開發者的云原生簡明指南
- Elastix Unified Communications Server Cookbook
- Python編程基礎與數據分析
- Instant SQL Server Analysis Services 2012 Cube Security