Configure Spring with MariaDB/MySQL
Try for 30 days
Flexible, version-controlled infrastructure provisioning and development-to-production workflows
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();
}
}