This sample application shows how to develop applications using Spring Data MyBatis in combination with Spanner GoogleSQL.
This sample shows:
- How to use Spring Data MyBatis with a Spanner GoogleSQL database.
- How to use bit-reversed identity columns to automatically generate primary key values for entities.
- How to set the transaction isolation level that is used by the Spanner JDBC driver.
- How to use the Spanner Emulator for development in combination with Spring Data.
MyBatis Spring integrates MyBatis with the popular Java Spring framework. This allows MyBatis to participate in Spring transactions and to automatically inject MyBatis mappers into other beans.
- The sample by default starts an instance of the Spanner Emulator together with the application and runs the application against the emulator.
- To run the sample on a real Spanner database, modify
application.properties and set the
spanner.emulatorproperty tofalse. Modify thespanner.project,spanner.instance, andspanner.databaseproperties to point to an existing Spanner database. The database must use the GoogleSQL dialect. - Run the application with
mvn spring-boot:run.
The main application components are:
- DatabaseSeeder.java: This
class is responsible for creating the database schema and inserting some initial test data. The
schema is created from the create_schema.sql file. The
DatabaseSeederclass loads this file into memory and executes it on the active database using standard JDBC APIs. - EmulatorInitializer.java:
This ApplicationListener automatically starts the Spanner emulator as a Docker container if the
sample has been configured to run on the emulator. You can disable this with the
spanner.emulatorproperty inapplication.properties. - AbstractEntity.java: This is the shared base class for all entities in this sample application. It defines a number of standard attributes, such as the identifier (primary key). The primary key is automatically generated using a (bit-reversed) identity column. Bit-reversed sequential values are considered a good choice for primary keys in Spanner.
- Application.java: The starter class of the application. It contains a command-line runner that executes a selection of queries and updates on the database.
- SingerService and AlbumService are standard Spring service beans that contain business logic that can be executed as transactions. This includes both read/write and read-only transactions.