Connect using Java ORMs
In addition to connecting to MatrixOne using JDBC, we can also connect to MatrixOne databases using the Object Relationship Mapping (ORM) framework. In this document, you describe how to connect to MatrixOne using Spring Data JPA and MyBatis.
MyBatis
MyBatis is a SQL mapping framework, and its advantage is that it is simple and easy to use. You can refer to the SpringBoot and MyBatis CRUD Example complete tutorial to learn how to build a CRUD application. In this document, we will focus on how to configure MyBatis using MatrixOne.
The following example is a typical setup for theMaven build system.
1. Add MyBatis-Spring-Boot-Starter in Pom.xml
To build a MyBatis application on Spring Boot, you need to add the MyBatis-Spring-Boot-Starter module to pom.xml, and the MyBatis-Spring-Boot-Starter module is created when selecting a Maven project.
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
2. Add configuration
The parameters that need to be modified in application.properties are as follows, and the remaining parameters can save the default values:
spring.datasource.driver-class-name: The driver name of the MySQL connector.spring.datasource.url: JDBC connection URL parameter.spring.datasource.username: database username.spring.datasource.password: database password.mybatis.mapper-locations: Location of the Mapper XML configuration file.
The recommended configurations in MatrixOne are as follows:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# please modify host_ip_address
spring.datasource.url=jdbc:mysql://freetier-01.cn-hangzhou.cluster.matrixonecloud.cn:6001/test?characterSetResults=UTF-8&continueBatchOnError=false&useServerPrepStmts=true&alwaysSendSetIsolation=false&useLocalSessionState=true&zeroDateTimeBehavior=CONVERT_TO_NULL&failoverReadOnly=false&serverTimezone=Asia/Shanghai&socketTimeout=30000
# please modify tenant:user:role
spring.datasource.username=585b49fc_852b_4bd1_b6d1_d64bc1d8xxxx:admin:accountadmin
# please modify your_password
spring.datasource.password=your_password
mybatis.mapper-locations=classpath:mapping/*xml
Note
You need to use the recommended configuration JDBC connection URL, otherwise the connection will fail.
Spring Data JPA
Spring Data JPA is a set of JPA application framework encapsulated by Spring based on ORM framework and JPA specifications. It allows developers to access and operate databases with minimal code. It helps to reduce boilerplate code and provides a mechanism to implement basic CRUD operations through one of several predefined repository interfaces. It also provides common functions including additions, deletion, modification and query, and is easy to expand.
Spring Data JPA is a powerful application framework that helps reduce boilerplate code and provides a mechanism to implement basic CRUD operations through one of several predefined repository interfaces. You can refer to the SpringBoot and Hibernate CRUD Examples complete tutorial to learn how to build a CRUD application. In this document, we will focus on how to configure Spring JPA using MatrixOne connection.
The following example is a typical setup for theMaven build system.
1. Add spring-boot-starter-data-jpa in Pom.xml
To build a Spring Data JPA application on Spring Boot, you need to add the spring-boot-starter-data-jpa module to pom.xml, and the spring-boot-starter-data-jpa module is created when selecting a Maven project.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2. Add configuration
The parameters that need to be modified in application.properties are as follows, and the remaining parameters can save the default values:
spring.datasource.driver-class-name: The driver name of the MySQL connector.spring.datasource.url: JDBC connection URL parameter.spring.datasource.username: database username.spring.datasource.password: database password.-
spring.jpa.properties.hibernate.dialect: SQL dialect (i.e. SQL dialect) enables Hibernate to generate better SQL for the selected database. MatrixOne currently only supportsorg.hibernate.dialect.MySQLDialect. -
spring.jpa.hibernate.ddl-auto: Thespring.jpa.hibernate.ddl-autoproperty adopts an enum that controls the generation of modes in a more controllable way. Possible options and effects are shown in the table below. MatrixOne currently supports only none and validate.
| Options | Effects | | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | none | No database schema initialization | | create | Delete and create a pattern when the application starts. With this option, all your data will disappear every time you start. | | create-drop | Create mode at startup and destroy mode when context is closed. Can be used for unit testing. | | validate | Check only if the pattern matches the entity. If the pattern does not match, the application startup will fail. Do not change the database. | | update | Update mode only if necessary. For example, if a new field is added in an entity, it will simply change the table for the new column without destroying the data. |
The recommended configurations in MatrixOne are as follows:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# please modify host_ip_address
spring.datasource.url=jdbc:mysql://freetier-01.cn-hangzhou.cluster.matrixonecloud.cn:6001/test?characterSetResults=UTF-8&continueBatchOnError=false&useServerPrepStmts=true&alwaysSendSetIsolation=false&useLocalSessionState=true&zeroDateTimeBehavior=CONVERT_TO_NULL&failoverReadOnly=false&serverTimezone=Asia/Shanghai&socketTimeout=30000
# please modify tenant:user:role
spring.datasource.username=585b49fc_852b_4bd1_b6d1_d64bc1d8xxxx:admin:accountadmin
# please modify your_password
spring.datasource.password=your_password
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto = validate