[JAVA] Flow until output table data to view with Spring Boot

What you need in advance

-Set the connection to the DB

Rough flow

It is a setting to get data from the Users table and display it.

  1. Create an entity
  2. Create a repository
  3. Create a controller
  4. Create a view

Create an entity

It is used to save the acquired value of the data in the Users table.

com.example.entities.UsersEntity.java



@Entity
@Table(name="Users")
public class UsersEntity{
  @Id
  private Integer id;
  private String name;

  public Integer getId(){
    return id;
  }

  public String getName(){
    return name;
  }
}

Create a repository

The repository exchanges data with the DB.

com.example.repositories.UsersRepository.java


import com.example.entities.UsersEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UsersRepository extends JpaRepository<UsersEntity, Integer>{
}

Create a controller

com.example.controller.UserController.java


@Controller
public class UserController{
  @Autowired
  //Assign to variable
  private UsersRepository usersRepository;

  //Behavior when accessing this URL
  @RequestMapping("/index") 
  public String index(Model model){
    List<UsersEntity> users = usersRepository.findAll();
    model.addAttribute("userlist", users);
    return "view/user/index";
  }
}

Create a view

/view/user/index.html


  <table>
    <tr th:each="users : ${userlist}">
      <td th:text="${users.id}"></td>
      <td th:text="${users.name}"></td>
    </tr>
  </table>

Recommended Posts

Flow until output table data to view with Spring Boot
Until data acquisition with Spring Boot + MyBatis + PostgreSQL
[Spring Batch] Output table data to CSV file
Output embedded Tomcat access log to standard output with Spring Boot
Until "Hello World" with Spring Boot
Until INSERT and SELECT to Postgres with Spring boot and thymeleaf
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
Try to implement login function with Spring Boot
An introduction to Spring Boot + in-memory data grid
Try to automate migration with Spring Boot Flyway
[Java] Article to add validation with Spring Boot 2.3.1.
I wanted to gradle spring boot with multi-project
[Introduction to Spring Boot] Authentication function with Spring Security
Spring Boot + Spring Data JPA About multiple table joins
How to perform UT with Excel as test data with Spring Boot + JUnit5 + DBUnit
Sample to batch process data on DB with Apache Camel Spring Boot starters
Download with Spring Boot
Settings for connecting to MySQL with Spring Boot + Spring JDBC
I tried to get started with Spring Data JPA
Create Restapi with Spring Boot ((1) Until Run of App)
Until you start development with Spring Boot in eclipse 1
How to boot by environment with Spring Boot of Maven
Until you start development with Spring Boot in eclipse 2
Attempt to SSR Vue.js with Spring Boot and GraalJS
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Spring Boot Introductory Guide I tried [Accessing Data with JPA]
Get started with Spring boot
Hello World with Spring Boot!
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
Extract SQL to property file with jdbcTemplate of spring boot
Spring Model View Controller Flow
Introduction to Spring Boot ① ~ DI ~
File upload with Spring Boot
Spring Boot starting with copy
Output multiplication table with Stream
Introduction to Spring Boot ② ~ AOP ~
Connect to database with spring boot + spring jpa and CRUD operation
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Introduction to Spring Boot Part 1
Create microservices with Spring Boot
Send email with spring boot
I tried to get started with Swagger using Spring Boot
Introduction to Spring Boot x OpenAPI ~ OpenAPI made with Generation gap pattern ~
How to create your own Controller corresponding to / error with Spring Boot
Sample code to unit test a Spring Boot controller with MockMvc
See the behavior of entity update with Spring Boot + Spring Data JPA
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
How to use Spring Data JDBC
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)