Categories
java spring-boot

configure datasource programmatically in spring boot

In this post, we will learn about configure datasource programmatically in spring boot

In below example we are using mysql as database. We are reading database properties from application.properties using @ConfigurationProperties

Using DataSourceBuilder
    @ConfigurationProperties(prefix = "datasource.custom")
	@Bean
	@Primary
	public DataSource dataSource() {
		return DataSourceBuilder.create().build();
	}

In the above snippet spring boot will create datasource with values from application.properties

datasource.custom.jdbcUrl=jdbc:mysql://localhost:3306/beginnersbug
datasource.custom.username=root
datasource.custom.password=password
datasource.custom.driverClassName=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

make sure your prefix correct matching text. In this example I am using datasource.custom as prefix

CREATE TABLE students (
    id int NOT NULL,
	firstname varchar(255) NOT NULL,
    lastname varchar(255) NOT NULL,    
    department int,
    PRIMARY KEY (id)
);
dependency
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

Configuration Class

Your configuration class should annotated with @configuration

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class DatasourceConfig {

	@ConfigurationProperties(prefix = "datasource.custom")
	@Bean
	@Primary
	public DataSource dataSource() {
		return DataSourceBuilder.create().build();
	}

}

In this program we are connecting to mysql database. Below is DaoInterface where I am using JpaRepository for CRUD operations

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.beginnersbug.studentservice.model.Student;

@Repository
public interface StudentDao extends JpaRepository<Student, Long> {

}

From Controller class we are invoking DAO interface to retrieve strudents table data


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.beginnersbug.datasource.StudentDao;
import com.beginnersbug.datasource.model.Student;

@RestController()
@RequestMapping("/api/student")
public class StudentController {

	@Autowired
	StudentDao studentsDao;

	@RequestMapping(method = RequestMethod.GET)
	public List<Student> getStudentsList() {
		return studentsDao.findAll();
	}

}
Testing

Hit http://localhost:8080/api/student from Chrome brower, It will return all the values from the students table

Github

https://github.com/rkumar9090/datasource

Related Articles

connect MySQL database from spring boot

Leave a Reply

Your email address will not be published. Required fields are marked *