Skip to content

Commit 6b5d04c

Browse files
authored
Merge pull request #2 from hanshulll/development
Added code to work with jpa in springboot application
2 parents 872e587 + da56fc1 commit 6b5d04c

File tree

6 files changed

+176
-0
lines changed

6 files changed

+176
-0
lines changed

spring-data-jpa/pom.xml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.4.3</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.hanshul</groupId>
12+
<artifactId>spring-data-jpa</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>spring-data-jpa</name>
15+
<description>Demo project for Spring Boot</description>
16+
<url/>
17+
<licenses>
18+
<license/>
19+
</licenses>
20+
<developers>
21+
<developer/>
22+
</developers>
23+
<scm>
24+
<connection/>
25+
<developerConnection/>
26+
<tag/>
27+
<url/>
28+
</scm>
29+
<properties>
30+
<java.version>17</java.version>
31+
</properties>
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-data-jpa</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.postgresql</groupId>
40+
<artifactId>postgresql</artifactId>
41+
<scope>runtime</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.hanshul.spring_data_jpa;
2+
3+
import com.hanshul.spring_data_jpa.model.Student;
4+
import com.hanshul.spring_data_jpa.repository.StudentRepository;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.ApplicationContext;
8+
9+
@SpringBootApplication
10+
public class SpringDataJpaApplication {
11+
12+
public static void main(String[] args) {
13+
ApplicationContext context= SpringApplication.run(SpringDataJpaApplication.class, args);
14+
Student s1= context.getBean(Student.class);
15+
Student s2=context.getBean(Student.class);
16+
Student s3=context.getBean(Student.class);
17+
18+
StudentRepository repo=context.getBean(StudentRepository.class);
19+
20+
s1.setRollNo(101);
21+
s1.setName("Hanshul");
22+
s1.setMarks(100);
23+
24+
25+
s2.setRollNo(102);
26+
s2.setName("Sharad");
27+
s2.setMarks(100);
28+
29+
30+
s3.setRollNo(103);
31+
s3.setName("Harsh");
32+
s3.setMarks(100);
33+
34+
repo.save(s1);
35+
repo.save(s2);
36+
repo.save(s3);
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.hanshul.spring_data_jpa.model;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.Id;
5+
import jakarta.persistence.Table;
6+
import org.springframework.context.annotation.Scope;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
@Scope("prototype")
11+
@Entity
12+
public class Student {
13+
@Id
14+
private int rollNo;
15+
private String name;
16+
private int marks;
17+
18+
public int getRollNo() {
19+
return rollNo;
20+
}
21+
22+
public void setRollNo(int rollNo) {
23+
this.rollNo = rollNo;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public int getMarks() {
35+
return marks;
36+
}
37+
38+
public void setMarks(int marks) {
39+
this.marks = marks;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "Student{" +
45+
"rollNo=" + rollNo +
46+
", name='" + name + '\'' +
47+
", marks=" + marks +
48+
'}';
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.hanshul.spring_data_jpa.repository;
2+
3+
import com.hanshul.spring_data_jpa.model.Student;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface StudentRepository extends JpaRepository<Student,Integer> {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#spring.application.name=spring-data-jpa
2+
spring.datasource.url=jdbc:postgresql://localhost:5432/demo
3+
spring.datasource.username={{userName}}
4+
spring.datasource.password={{password}}
5+
spring.datasource.driver-class-name=org.postgresql.Driver
6+
spring.jpa.hibernate.ddl-auto=update
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.hanshul.spring_data_jpa;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class SpringDataJpaApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

0 commit comments

Comments
 (0)