Skip to content

Instantly share code, notes, and snippets.

@pteacher
Created February 7, 2023 11:23
Show Gist options
  • Save pteacher/3933955d6c88ae532be9649c15bf0e5f to your computer and use it in GitHub Desktop.
Save pteacher/3933955d6c88ae532be9649c15bf0e5f to your computer and use it in GitHub Desktop.
Spring Boot with Thymeleaf and JPA (H2)
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
INSERT INTO student (name, gpa) VALUES ('Ruslan', 3);
<!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>
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";
}
}
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;
}
}
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> {
}
@pteacher
Copy link
Author

pteacher commented Feb 9, 2023

<!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>
<form action="/" method="post">
    <input type="text" name="description">
    <input type="text" name="issuer">
    <input type="text" name="executor">
    <input type="text" name="status">
    <input type="submit" class="btn btn-primary">
</form>
<table class="table">
    <thead>
    <tr>
        <th scope="col">#</th>
        <th scope="col">Description</th>
        <th scope="col">Issuer</th>
        <th scope="col">Executor</th>
        <th scope="col">Status</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="task : ${tasks}">
        <th scope="row" th:text="${task.id}"></th>
        <td th:text="${task.description}"></td>
        <td th:text="${task.issuer}"></td>
        <td th:text="${task.executor}"></td>
        <td th:text="${task.status}"></td>
    </tr>

    </tbody>
</table>
</body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment