title: Connecting MySQL to a Spring Boot REST Application
body: <p>Based off a tutorial on the Spring site I was able to setup a simple demo <code>REST</code> application. It works fine but I just can't figure out how to tie it into my <code>MySQL database</code>. I have it configured to automatically connect but I know I am missing some glue. I have seen random blog posts about configuring a <code>datasource</code> bean in <code>Application.java</code> 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 <code>cURL</code> they work fine but just don't interact at all with my <code>MySQL db</code>. Am I missing the <code>datasource</code>? If so, could you provide guidance on getting it to work? Thanks!</p>
  
  <p>The code is very simple:</p>
  
  <p>Application.java
  package hello;</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>User.java</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>UserRepository.java</p>
  
  <pre><code>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&lt;User, Long&gt;
  {
  }
  </code></pre>
  
  <p><strong>application.properties:</strong></p>
  
  <pre><code>spring.datasource.url=jdbc:mysql://localhost:3306/chrdb
  spring.datasource.username=root
  spring.datasource.password=
  spring.datasource.driverClassName=com.mysql.jdbc.Driver
  </code></pre>