Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting MySQL to a Spring Boot REST Application

Tags:

java

spring

Based off a tutorial on the Spring site I was able to setup a simple demo REST application. It works fine but I just can't figure out how to tie it into my MySQL database. I have it configured to automatically connect but I know I am missing some glue. I have seen random blog posts about configuring a datasource bean in Application.java but I also read that using actuator this should all be done automatically. In the log output it looks like my database is being connected successfully, and when I hit the REST endpoints with cURL they work fine but just don't interact at all with my MySQL db. Am I missing the datasource? If so, could you provide guidance on getting it to work? Thanks!

The code is very simple:

Application.java package hello;

import ...

@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
@ComponentScan
@PropertySource("classpath:application.properties")
public class Application
{
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

User.java

package hello;

import javax.persistence.*;

@Entity
@Table(name = "user")
public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String username;

    public User()
    {  
    }

    public User(long id, String username)
    {
        this.id = id;
        this.username = username;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }
}

UserRepository.java

package hello;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserRepository extends PagingAndSortingRepository<User, Long>
{
}

application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/chrdb
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver
like image 262
Hines Bourne Avatar asked Jun 14 '14 17:06

Hines Bourne


People also ask

How do you connect your spring boot application to database?

To access the Relational Database by using JdbcTemplate in Spring Boot application, we need to add the Spring Boot Starter JDBC dependency in our build configuration file. Then, if you @Autowired the JdbcTemplate class, Spring Boot automatically connects the Database and sets the Datasource for the JdbcTemplate object.

Can we configure MySQL with spring boot?

Configure MySQL for Spring Boot Application Spring Boot provides a ready-to-use support for H2 Database. Spring Boot automatically set up in memory H2 database if it detects H2 configurations in the classpath.

Does spring boot support MySQL?

The spring-boot-starter-data-jpa is a starter for using Spring Data JPA with Hibernate. The mysql-connector-java dependency is for the MySQL database driver. The spring-boot-maven-plugin provides Spring Boot support in Maven, allowing us to package executable JAR or WAR archives.


1 Answers

You don't need the @PropertySources, @EnableJpaRepositories and @Import(RepositoryRestMvcConfiguration.class) as Spring Boot handles that for you already when Spring Data JPA and Spring Data REST are detected. (That is what @EnableAutoConfiguration is for).


import ...

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Specifying the url, username and password should be enough (if you are using Spring Boot 1.1.x).

spring.datasource.url=jdbc:mysql://localhost:3306/chrdb
spring.datasource.username=root
spring.datasource.password=

Finally make sure you don't have H2, HQSQLDB or Derby on your classpath as those will automatically detected and might override your database settings.

I have seen random blog posts about configuring a datasource bean in Application.java but I also read that using actuator this should all be done automatically.

Autoconfiguration hasn't so much to do with the actuator (only for Spring Security, metrics and management) but with the auto configuration part of Spring Boot.

like image 63
M. Deinum Avatar answered Oct 08 '22 10:10

M. Deinum