|
1 | | -# Spring Data MyBatis Sample Application with Cloud Spanner PostgreSQL |
| 1 | +# Spring Data MyBatis |
2 | 2 |
|
3 | | -This sample application shows how to develop portable applications using Spring Data MyBatis in |
4 | | -combination with Cloud Spanner PostgreSQL. This application can be configured to run on either a |
5 | | -[Cloud Spanner PostgreSQL](https://cloud.google.com/spanner/docs/postgresql-interface) database or |
6 | | -an open-source PostgreSQL database. The only change that is needed to switch between the two is |
7 | | -changing the active Spring profile that is used by the application. |
| 3 | +This directory contains two sample applications for using Spring Data MyBatis |
| 4 | +with the Spanner JDBC driver. |
8 | 5 |
|
9 | | -The application uses the Cloud Spanner JDBC driver to connect to Cloud Spanner PostgreSQL, and it |
10 | | -uses the PostgreSQL JDBC driver to connect to open-source PostgreSQL. Spring Data MyBatis works with |
11 | | -both drivers and offers a single consistent API to the application developer, regardless of the |
12 | | -actual database or JDBC driver being used. |
13 | | - |
14 | | -This sample shows: |
15 | | - |
16 | | -1. How to use Spring Data MyBatis with Cloud Spanner PostgreSQL. |
17 | | -2. How to develop a portable application that runs on both Google Cloud Spanner PostgreSQL and |
18 | | - open-source PostgreSQL with the same code base. |
19 | | -3. How to use bit-reversed sequences to automatically generate primary key values for entities. |
20 | | -4. How to use the Spanner Emulator for development in combination with Spring Data. |
21 | | - |
22 | | -__NOTE__: This application does __not require PGAdapter__. Instead, it connects to Cloud Spanner |
23 | | -PostgreSQL using the Cloud Spanner JDBC driver. |
24 | | - |
25 | | -## Cloud Spanner PostgreSQL |
26 | | - |
27 | | -Cloud Spanner PostgreSQL provides language support by expressing Spanner database functionality |
28 | | -through a subset of open-source PostgreSQL language constructs, with extensions added to support |
29 | | -Spanner functionality like interleaved tables and hinting. |
30 | | - |
31 | | -The PostgreSQL interface makes the capabilities of Spanner —__fully managed, unlimited scale, strong |
32 | | -consistency, high performance, and up to 99.999% global availability__— accessible using the |
33 | | -PostgreSQL dialect. Unlike other services that manage actual PostgreSQL database instances, Spanner |
34 | | -uses PostgreSQL-compatible syntax to expose its existing scale-out capabilities. This provides |
35 | | -familiarity for developers and portability for applications, but not 100% PostgreSQL compatibility. |
36 | | -The SQL syntax that Spanner supports is semantically equivalent PostgreSQL, meaning schemas |
37 | | -and queries written against the PostgreSQL interface can be easily ported to another PostgreSQL |
38 | | -environment. |
39 | | - |
40 | | -This sample showcases this portability with an application that works on both Cloud Spanner PostgreSQL |
41 | | -and open-source PostgreSQL with the same code base. |
42 | | - |
43 | | -## MyBatis Spring |
44 | | -[MyBatis Spring](http://mybatis.org/spring/) integrates MyBatis with the popular Java Spring |
45 | | -framework. This allows MyBatis to participate in Spring transactions and to automatically inject |
46 | | -MyBatis mappers into other beans. |
47 | | - |
48 | | -## Sample Application |
49 | | - |
50 | | -This sample shows how to create a portable application using Spring Data MyBatis and the Cloud Spanner |
51 | | -PostgreSQL dialect. The application works on both Cloud Spanner PostgreSQL and open-source |
52 | | -PostgreSQL. You can switch between the two by changing the active Spring profile: |
53 | | -* Profile `cs` runs the application on Cloud Spanner PostgreSQL. |
54 | | -* Profile `pg` runs the application on open-source PostgreSQL. |
55 | | - |
56 | | -The default profile is `cs`. You can change the default profile by modifying the |
57 | | -[application.properties](src/main/resources/application.properties) file. |
58 | | - |
59 | | -### Running the Application |
60 | | - |
61 | | -1. Choose the database system that you want to use by choosing a profile. The default profile is |
62 | | - `cs`, which runs the application on Cloud Spanner PostgreSQL. |
63 | | -2. The sample by default starts an instance of the Spanner Emulator together with the application and |
64 | | - runs the application against the emulator. |
65 | | -3. Modify the default profile in the [application.properties](src/main/resources/application.properties) |
66 | | - file to run the sample on an open-source PostgreSQL database. |
67 | | -4. Modify either [application-cs.properties](src/main/resources/application-cs.properties) or |
68 | | - [application-pg.properties](src/main/resources/application-pg.properties) to point to an existing |
69 | | - database. If you use Cloud Spanner, the database that the configuration file references must be a |
70 | | - database that uses the PostgreSQL dialect. |
71 | | -5. Run the application with `mvn spring-boot:run`. |
72 | | - |
73 | | -### Main Application Components |
74 | | - |
75 | | -The main application components are: |
76 | | -* [DatabaseSeeder.java](src/main/java/com/google/cloud/spanner/sample/DatabaseSeeder.java): This |
77 | | - class is responsible for creating the database schema and inserting some initial test data. The |
78 | | - schema is created from the [create_schema.sql](src/main/resources/create_schema.sql) file. The |
79 | | - `DatabaseSeeder` class loads this file into memory and executes it on the active database using |
80 | | - standard JDBC APIs. The class also removes Cloud Spanner-specific extensions to the PostgreSQL |
81 | | - dialect when the application runs on open-source PostgreSQL. |
82 | | -* [JdbcConfiguration.java](src/main/java/com/google/cloud/spanner/sample/JdbcConfiguration.java): |
83 | | - This utility class is used to determine whether the application is running on Cloud Spanner |
84 | | - PostgreSQL or open-source PostgreSQL. This can be used if you have specific features that should |
85 | | - only be executed on one of the two systems. |
86 | | -* [EmulatorInitializer.java](src/main/java/com/google/cloud/spanner/sample/EmulatorInitializer.java): |
87 | | - This ApplicationListener automatically starts the Spanner emulator as a Docker container if the |
88 | | - sample has been configured to run on the emulator. |
89 | | -* [AbstractEntity.java](src/main/java/com/google/cloud/spanner/sample/entities/AbstractEntity.java): |
90 | | - This is the shared base class for all entities in this sample application. It defines a number of |
91 | | - standard attributes, such as the identifier (primary key). The primary key is automatically |
92 | | - generated using a (bit-reversed) sequence. [Bit-reversed sequential values](https://cloud.google.com/spanner/docs/schema-design#bit_reverse_primary_key) |
93 | | - are considered a good choice for primary keys on Cloud Spanner. |
94 | | -* [Application.java](src/main/java/com/google/cloud/spanner/sample/Application.java): The starter |
95 | | - class of the application. It contains a command-line runner that executes a selection of queries |
96 | | - and updates on the database. |
97 | | -* [SingerService](src/main/java/com/google/cloud/spanner/sample/service/SingerService.java) and |
98 | | - [AlbumService](src/main/java/com/google/cloud/spanner/sample/service/SingerService.java) are |
99 | | - standard Spring service beans that contain business logic that can be executed as transactions. |
100 | | - This includes both read/write and read-only transactions. |
| 6 | +- GoogleSQL: This sample uses the Spanner GoogleSQL dialect. |
| 7 | +- PostgreSQL: This sample uses the Spanner PostgreSQL dialect and the Spanner JDBC driver. |
| 8 | + It does not use PGAdapter. The sample application can also be configured to run on open |
| 9 | + source PostgreSQL, and shows how a portable application be developed using this setup. |
0 commit comments