Created
February 7, 2023 11:23
-
-
Save pteacher/3933955d6c88ae532be9649c15bf0e5f to your computer and use it in GitHub Desktop.
Spring Boot with Thymeleaf and JPA (H2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.datasource.url=jdbc:h2:file:./testdb | |
spring.datasource.driverClassName=org.h2.Driver | |
spring.datasource.username=sa | |
spring.datasource.password=password | |
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect | |
spring.jpa.hibernate.ddl-auto=create | |
spring.sql.init.mode=always | |
spring.jpa.defer-datasource-initialization=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO student (name, gpa) VALUES ('Ruslan', 3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html xmlns:th="http://www.thymeleaf.org" lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous"> | |
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous" defer></script> | |
</head> | |
<body> | |
<table class="table"> | |
<thead> | |
<tr> | |
<th scope="col">#</th> | |
<th scope="col">Name</th> | |
<th scope="col">GPA</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr th:each="student : ${students}"> | |
<th scope="row">1</th> | |
<td th:text="${student.name}"></td> | |
<td th:text="${student.gpa}"></td> | |
</tr> | |
</tbody> | |
</table> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.controller; | |
import com.example.demo.repo.StudentRepository; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.Model; | |
import org.springframework.web.bind.annotation.GetMapping; | |
@Controller | |
public class MainController { | |
@Autowired | |
StudentRepository studentRepository; | |
@GetMapping("/") | |
public String main(Model model) { | |
model.addAttribute("students", studentRepository.findAll()); | |
return "index"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.entity; | |
import jakarta.persistence.*; | |
@Entity | |
public class Student { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long id; | |
@Column | |
private String name; | |
@Column | |
private double gpa; | |
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 double getGpa() { | |
return gpa; | |
} | |
public void setGpa(double gpa) { | |
this.gpa = gpa; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.repo; | |
import com.example.demo.entity.Student; | |
import org.springframework.data.repository.CrudRepository; | |
import org.springframework.stereotype.Repository; | |
@Repository | |
public interface StudentRepository extends CrudRepository<Student, Long> { | |
} |
Author
pteacher
commented
Feb 9, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment