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

Setting up the MariaDB database

Now, we will 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 Create new | Database:
  1. Let's name our database cardb. After you click OK, you should see the new cardb database in the database list:
  1. In the application, we add a MariaDB dependency to the pom.xml file and remove the H2 dependency injection that we don't need anymore:
      <dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
  1. In the application.properties file, you will now 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 the default value if you don't define any. The create value only creates the database when the application is started. The 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. After running the application, you should see the tables in MariaDB. The following screenshot shows the HeidiSQL user interface after the database has been created:

Now, your application is now ready to be used with MariaDB.

主站蜘蛛池模板: 陆川县| 乌什县| 延津县| 塘沽区| 会宁县| 珲春市| 太和县| 庐江县| 镇巴县| 永寿县| 广安市| 岐山县| 屯留县| 九江市| 额济纳旗| 时尚| 德格县| 满城县| 祁连县| 房产| 汽车| 柘城县| 扶绥县| 南汇区| 蓬安县| 武夷山市| 海淀区| 云林县| 荔浦县| 赞皇县| 湛江市| 淳化县| 灵寿县| 广饶县| 横山县| 中江县| 沧源| 永丰县| 大荔县| 分宜县| 通化县|