Skip to content

Commit b5e9017

Browse files
committed
Spring boot Data REST
1 parent 8820031 commit b5e9017

File tree

8 files changed

+258
-0
lines changed

8 files changed

+258
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.in28minutes.springboot.rest.example</groupId>
7+
<artifactId>spring-boot-2-jpa-spring-data-rest</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-2-jpa-with-hibernate-and-h2</name>
12+
<description>Spring Boot 2, Hibernate, JPA and H2 - Example Project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.3.1.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-jpa</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-data-rest</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-web</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-devtools</artifactId>
47+
<scope>runtime</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.h2database</groupId>
51+
<artifactId>h2</artifactId>
52+
<scope>runtime</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-test</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
<build>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-maven-plugin</artifactId>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
70+
<repositories>
71+
<repository>
72+
<id>spring-snapshots</id>
73+
<name>Spring Snapshots</name>
74+
<url>https://repo.spring.io/snapshot</url>
75+
<snapshots>
76+
<enabled>true</enabled>
77+
</snapshots>
78+
</repository>
79+
<repository>
80+
<id>spring-milestones</id>
81+
<name>Spring Milestones</name>
82+
<url>https://repo.spring.io/milestone</url>
83+
<snapshots>
84+
<enabled>false</enabled>
85+
</snapshots>
86+
</repository>
87+
</repositories>
88+
89+
<pluginRepositories>
90+
<pluginRepository>
91+
<id>spring-snapshots</id>
92+
<name>Spring Snapshots</name>
93+
<url>https://repo.spring.io/snapshot</url>
94+
<snapshots>
95+
<enabled>true</enabled>
96+
</snapshots>
97+
</pluginRepository>
98+
<pluginRepository>
99+
<id>spring-milestones</id>
100+
<name>Spring Milestones</name>
101+
<url>https://repo.spring.io/milestone</url>
102+
<snapshots>
103+
<enabled>false</enabled>
104+
</snapshots>
105+
</pluginRepository>
106+
</pluginRepositories>
107+
108+
109+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.alanbinu.springboot.jpa.spring.data.rest.example;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
9+
import com.alanbinu.springboot.jpa.spring.data.rest.example.student.StudentDataRestRepository;
10+
11+
@SpringBootApplication
12+
public class SpringBoot2JPAWithSpringDataRestApplication {
13+
14+
private Logger logger = LoggerFactory.getLogger(this.getClass());
15+
16+
@Autowired
17+
StudentDataRestRepository repository;
18+
19+
public static void main(String[] args) {
20+
SpringApplication.run(SpringBoot2JPAWithSpringDataRestApplication.class, args);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.alanbinu.springboot.jpa.spring.data.rest.example.student;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
7+
@Entity
8+
public class Student {
9+
@Id
10+
@GeneratedValue
11+
private Long id;
12+
private String name;
13+
private String passportNumber;
14+
15+
public Student() {
16+
super();
17+
}
18+
19+
public Student(Long id, String name, String passportNumber) {
20+
super();
21+
this.id = id;
22+
this.name = name;
23+
this.passportNumber = passportNumber;
24+
}
25+
26+
public Student(String name, String passportNumber) {
27+
super();
28+
this.name = name;
29+
this.passportNumber = passportNumber;
30+
}
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public void setName(String name) {
45+
this.name = name;
46+
}
47+
48+
public String getPassportNumber() {
49+
return passportNumber;
50+
}
51+
52+
public void setPassportNumber(String passportNumber) {
53+
this.passportNumber = passportNumber;
54+
}
55+
56+
@Override
57+
public String toString() {
58+
return String.format("Student [id=%s, name=%s, passportNumber=%s]", id, name, passportNumber);
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.alanbinu.springboot.jpa.spring.data.rest.example.student;
2+
3+
import org.springframework.data.repository.PagingAndSortingRepository;
4+
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
5+
6+
@RepositoryRestResource(path = "students", collectionResourceRel = "students")
7+
public interface StudentDataRestRepository extends PagingAndSortingRepository<Student, Long> {
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Enabling H2 Console
2+
spring.h2.console.enabled=true
3+
#Turn Statistics on
4+
spring.jpa.properties.hibernate.generate_statistics=true
5+
logging.level.org.hibernate.stat=debug
6+
# Show all queries
7+
spring.jpa.show-sql=true
8+
spring.jpa.properties.hibernate.format_sql=true
9+
logging.level.org.hibernate.type=trace
10+
11+
spring.datasource.url=jdbc:h2:mem:testdb
12+
spring.data.jpa.repositories.bootstrap-mode=default
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
insert into student
2+
values(10001,'Ranga', 'E1234567');
3+
4+
insert into student
5+
values(10002,'Ravi', 'A1234568');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.alanbinu.springboot.jpa.spring.data.rest.example;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class SpringBoot2JPAWithSpringDataRestApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)