-Set the connection to the DB
It is a setting to get data from the Users table and display it.
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;
}
}
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>{
}
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";
}
}
/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