Platform.sh is now Upsun. Click here to learn more
Upsun Fixed User Documentation

Configure Spring with MariaDB/MySQL

Try Upsun for 15 days
After that, enjoy the same, game-changing Upsun features for less with the First Project Incentive!¹ A monthly $19 perk!
Activate your 15-day trial
¹Terms and conditions apply

MariaDB/MySQL is an open-source relational database technology. Spring has a robust integration with this technology: Spring Data JPA.

The first step is to choose the database that you would like to use in your project. Define the driver for MariaDB or MySQL and the Java dependencies. Then determine the DataSource client using the Java configuration reader library.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sh.platform.config.Config;
import sh.platform.config.MariaDB; // Or substitute "MySQL" for "MariaDB"

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        Config config = new Config();
        MariaDB database = config.getCredential("database", MariaDB::new); // Or substitute "MySQL" for "MariaDB"
        return database.get();
    }
}