官术网_书友最值得收藏!

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:

  1. Right-click your mouse inside the database list.
  2. Then, select New | Database:
  1. Let's name our database cardb. After you press OK, you should see the new cardb in the database list:
  1. 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>
  1. 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
  1. 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:
主站蜘蛛池模板: 治县。| 白朗县| 巢湖市| 静安区| 新宾| 增城市| 邢台市| 石城县| 合作市| 威海市| 丘北县| 大宁县| 泰安市| 黄冈市| 佛冈县| 南宁市| 赤水市| 汶上县| 福鼎市| 浦江县| 太仆寺旗| 宁化县| 宜丰县| 洪洞县| 同江市| 台东市| 海原县| 称多县| 鄂州市| 大冶市| 丽水市| 丰宁| 凤翔县| 湘乡市| 大厂| 新乡县| 元朗区| 济宁市| 云林县| 齐齐哈尔市| 同德县|