Spring Data Configuration

 

1.Create a spring boot project. Spring Initializer
For dependencies, select the following,
a. JPA
b. MySQL Driver
c. Web

<!-- spring data -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- spring web -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql driver -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<scope>runtime</scope>
</dependency>

Import your project to your favorite IDE. I am using Spring suite tool for this tutorial.

2. Add these configurations to your application.properties file.

spring.datasource.url=jdbc:mysql://localhost:3306/spring_data?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.datasource.name=spring_data
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Note: createDatabaseIfNotExist=true will create your database if it does not exist.  It’s for development only.

3. Create a database in your localhost called spring_data. I am using MySQL Workbench.

4. Create a User class

package com.lovemesomecoding.user;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "user")
public class User implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", nullable = false, updatable = false, unique = true)
	private Long id;

	@Column(name = "name")
	private String name;

	@Column(name = "email")
	private String email;

	@Column(name = "age")
	private int age;

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

5. Create a UserRepository interface that implements JpaRepository

package com.lovemesomecoding.user;

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

public interface UserRepository extends JpaRepository<User, Long> {

}

That is all the configuration we have to do. As you can see, spring boot makes it easy for us to get up and running. There is not a lot of configurations to do before writing code. It’s one of the reasons why I love spring boot.

Checkout Source Code On Github

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

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