#Setting up MySql with Spring boot application

1 messages · Page 1 of 1 (latest)

mighty orchidBOT
#

<@&987246527741304832> please have a look, thanks.

strange willow
#

Just a curious question does it have to be MySQL 🙂 ?

umbral sierra
#

no it doesnt, its just the only DBM I have ever used

strange willow
#

Just curious. You can use which ever you want.
I have used MySQL, Oracle, SQL Server and PostgreSQL in my working career, and I hate Oracle the most.
PostgreSQL is my personal favorite, love it.

#

Currently we use Spring Boot and liquibase for versioning control of the PostgreSQL tables.
Guess first you have to bring in the JDBC driver for MySQL to your pom file.

#

And Spring JDBC
Example:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>

#

Then we also add application.yaml configuration:

datasource:
  postgres:
    jdbc-url: jdbc:postgresql://localhost:5432/myDatabase
    username: myUser
    password: myPw

Spring should AutoConfigure a NamedParameterJdbcTemplate for you.

#

Autowire the NamedParameterJdbcTemplate bean, use it.
Just a RAW Example:

        Map<String, Object> data = new HashMap<>();
        data.put("id", entity.getId());
        data.put("json", json);
        data.put("version", entity.getVersion());

        jdbcTemplate.update(
                "INSERT INTO mytable (id, json, version) " +
                        "VALUES (:id, :json, :version)",
                data);
#

I suggest you build a docker for your database, makes it easy for you to test etc.
Like stated before, we use liquibase to versioning our tables. When enabled, it will execute the SQL script that builds the tables, if they do not exist basically on startup. I can show you a docker compose example if you want.

umbral sierra
#

thank you, I will have to get docker