Spring Data JPA example
Let’s create an application which will use spring data jpa to store the user into the database.
First we need to create the pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springdatajpa</groupId> <artifactId>example</artifactId> <version>1.0-SNAPSHOT</version> <properties> <spring.data.version>1.8.0.RELEASE</spring.data.version> <org.hibernate.version>4.3.10.Final</org.hibernate.version> </properties> <!-- Spring Boot dependency management--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.4.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <showDeprecation>true</showDeprecation> <showWarnings>true</showWarnings> <fork>true</fork> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project> [addToAppearHere]
Now we need to create the User.java entity which will be saved into the database.
package com.springjpa.example; import javax.persistence.*; @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private Long id; @Column(name = "name", nullable = false) private String name; public User() { } public User(String name) { this.name = name; } 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; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } } [addToAppearHere]
After this we need to create the UserRepository.java
package com.springjpa.example; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User, Long> { /** * * @param id the user id * @return the User entity for specified id */ User findById(final Long id); }
And the finally we need to create the Application.java which will run the SpringBoot Application.
package com.springjpa.example; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } @Bean public CommandLineRunner commandLineRunner(UserRepository repository) { return (args) -> { // save some users repository.save(new User("user1")); repository.save(new User("user2")); repository.save(new User("user3")); // get all users System.out.println("Getting all users..."); repository.findAll().forEach(System.out::println); // get the user by id System.out.println("Get the user by id"); User user = repository.findById(1L); System.out.println(user); }; } }
In the console you should see following:
Getting all users...
User{id=1, name='user1'}
User{id=2, name='user2'}
User{id=3, name='user3'}
Get the user by id
User{id=1, name='user1'}
You can download the source code from the github.