To connect to the database, we need to have service.yml that can inject a connection pool to the microservice you are building.
Now we have a generated project with the configuration for MySQL, let’s take a look at the generated service.yml, and you can see the following section for MySQL connection pool configuration.
service.yml in ~/networknt/light-example-4j/rest/openapi/database/generated/src/main/resources/config
# Singleton service factory configuration/IoC injection
# StartupHookProvider implementations, there are one to many and they are called in the same sequence defined.
# - com.networknt.server.StartupHookProvider:
# If you are using mask module to remove sensitive info before logging, uncomment the following line.
# - com.networknt.server.JsonPathStartupHookProvider
# - com.networknt.server.Test1StartupHook
# - com.networknt.server.Test2StartupHook
# ShutdownHookProvider implementations, there are one to many and they are called in the same sequence defined.
# - com.networknt.server.ShutdownHookProvider:
# - com.networknt.server.Test1ShutdownHook
As you can see, the last section is the DataSource configuration for MySQL database.
Before we make any change, let’s create a new folder so that we can always compare with the generated folder if needed.
cp -r generated connection
As we are running the service as a standalone Java application without Docker, we need to change the service.yml for the jdbcUrl to point the database to localhost instead of mysqldb. Also, we need to switch our database to hello_world from oauth2.