質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%

Q&A

解決済

1回答

1761閲覧

Spring Boot:Thymeleaf 複数テーブルの内容を一覧表示させたい

Guttie

総合スコア6

0グッド

0クリップ

投稿2022/07/14 02:51

前提

Eclipsesでユーザーを管理するシステムを練習で作っています。
Controllerの部分でUsersテーブルのbranch_idとBranchesテーブルのidで紐づけて
index画面に名前・パスワード・部署を表示させられるようにしたいのですがやり方が見つかりません。
作ったDBは添付の通りです。
イメージ説明

.xmlファイルはJOINを使わずそれぞれのテーブルごとにSELECTして
mapperからentity、controllerへとDBの情報を持ってきています。

該当のソースコード

UserMapper.xml

xml

1<?xml version="1.0" encoding="UTF-8" ?> 2<!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5<mapper namespace="jp.alhinc.springtraining.mapper.UserMapper"> 6 <select id="findAllUser" resultType="jp.alhinc.springtraining.entity.User"> 7 SELECT 8 id, 9 name, 10 password 11 FROM 12 users 13 </select> 14 <select id="findAllBranch" resultType="jp.alhinc.springtraining.entity.Branch"> 15 SELECT 16 id, 17 branch_name 18 FROM 19 branches 20 </select> 21 <select id="findAllDepartment" resultType="jp.alhinc.springtraining.entity.Department"> 22 SELECT 23 id, 24 department_name 25 FROM 26 departments 27 </select> 28 <insert id="create"> 29 INSERT INTO users( 30 id, 31 name, 32 password 33 ) VALUES ( 34 #{id}, 35 #{name}, 36 #{password} 37 ) 38 </insert> 39</mapper>

mapper/UserMapper.java

java

1package jp.alhinc.springtraining.mapper; 2 3import java.util.List; 4 5import org.apache.ibatis.annotations.Mapper; 6 7import jp.alhinc.springtraining.entity.Branch; 8import jp.alhinc.springtraining.entity.Department; 9import jp.alhinc.springtraining.entity.User; 10 11@Mapper 12public interface UserMapper { 13 14 List<User> findAllUser(); 15 16 List<Branch> findAllBranch(); 17 18 List<Department> findAllDepartment(); 19 20 int create(User entity); 21 22 int create(Branch entity); 23 24 int create(Department entity); 25 26}

entity/User.java

java

1package jp.alhinc.springtraining.entity; 2 3public class User { 4 5 private Long id; 6 7 private String name; 8 9 private String password; 10 11 public Long getId() { 12 return id; 13 } 14 15 public void setId(Long id) { 16 this.id = id; 17 } 18 19 public String getName() { 20 return name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 public String getPassword() { 28 return password; 29 } 30 31 public void setPassword(String password) { 32 this.password = password; 33 } 34 35}

entity/Branch.java

java

1package jp.alhinc.springtraining.entity; 2 3public class Branch { 4 5 private Long id; 6 7 private String branch_name; 8 9 public Long getId() { 10 return id; 11 } 12 13 public void setId(Long id) { 14 this.id = id; 15 } 16 17 public String getBranchName() { 18 return branch_name; 19 } 20 21 public void setBranchName(String branch_name) { 22 this.branch_name = branch_name; 23 } 24 25}

servise/GetAllUsersServise.java

java

1package jp.alhinc.springtraining.service; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7import org.springframework.transaction.annotation.Transactional; 8 9import jp.alhinc.springtraining.entity.Branch; 10import jp.alhinc.springtraining.entity.Department; 11import jp.alhinc.springtraining.entity.User; 12import jp.alhinc.springtraining.mapper.UserMapper; 13 14@Service 15public class GetAllUsersService { 16 17 @Autowired 18 private UserMapper mapper; 19 20 @Transactional 21 public List<User> getAllUsers() { 22 23 return mapper.findAllUser(); 24 25 } 26 27 @Transactional 28 public List<Branch> getAllBranches() { 29 30 return mapper.findAllBranch(); 31 32 } 33 34 @Transactional 35 public List<Department> getAllDepartments() { 36 37 return mapper.findAllDepartment(); 38 39 } 40 41}

controller/UsersController.java

java

1package jp.alhinc.springtraining.controller; 2 3import java.util.List; 4 5import javax.validation.Valid; 6 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.stereotype.Controller; 9import org.springframework.ui.Model; 10import org.springframework.validation.BindingResult; 11import org.springframework.web.bind.annotation.GetMapping; 12import org.springframework.web.bind.annotation.ModelAttribute; 13import org.springframework.web.bind.annotation.PostMapping; 14import org.springframework.web.bind.annotation.RequestMapping; 15 16import jp.alhinc.springtraining.entity.Branch; 17import jp.alhinc.springtraining.entity.Department; 18import jp.alhinc.springtraining.entity.User; 19import jp.alhinc.springtraining.form.CreateUserForm; 20import jp.alhinc.springtraining.service.CreateUserService; 21import jp.alhinc.springtraining.service.GetAllUsersService; 22 23@Controller 24@RequestMapping("/users") 25public class UsersController { 26 27 @Autowired 28 private GetAllUsersService getAllUsersService; 29 30 @Autowired 31 private CreateUserService createUserService; 32 33 @GetMapping 34 public String index(Model model) { 35 System.out.println("呼び出し"); 36 List<User> users = getAllUsersService.getAllUsers(); 37 List<Branch> branches = getAllUsersService.getAllBranches(); 38 List<Department> departments = getAllUsersService.getAllDepartments(); 39 model.addAttribute("users", users); 40 model.addAttribute("branches", branches); 41 model.addAttribute("departments", departments); 42 return "users/index"; 43 } 44 45 @GetMapping("/create") 46 public String create(Model model) { 47 model.addAttribute("form", new CreateUserForm()); 48 return "users/create"; 49 } 50 51 @PostMapping 52 public String create(@Valid @ModelAttribute("form") CreateUserForm form, BindingResult result, Model model) { 53 54 if (result.hasErrors()) { 55 model.addAttribute("message", "残念"); 56 model.addAttribute("form", form); 57 return "users/create"; 58 } 59 60 createUserService.create(form); 61 return "redirect:/users"; 62 } 63}

html

1<!DOCTYPE html> 2<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="UTF-8"> 5<title>ホーム | ユーザー管理システム</title> 6</head> 7<body> 8 <p>ユーザ一覧</p> 9 <a th:href="@{/users/create}">新規登録</a> 10 11 <table> 12 <tr> 13 <th>ログインID</th> 14 <th>名称</th> 15 <th>支店名</th> 16 <th>部署・役職名</th> 17 <th>アカウントの状態</th> 18 </tr> 19 <tr th:each="user: ${users}"> 20 <td th:text="${user.id}"></td> 21 <td th:text="${user.name}"></td> 22 <td>支店支店支店</td> 23 <td>部署部署部署</td> 24 <td>ステータス確認</td> 25 </tr> 26 </table> 27</body> 28</html>

実現したいこと

ここに実現したいことを箇条書きで書いてください。

  • index画面にユーザーを表示させるときに部署情報も【th.text】を使って表示させたい。(写真のような一覧にしたい)

イメージ説明

  • 今回は一つのxmlファイルで全テーブル情報を抽出しているが、全く別に良い方法があるなら教えてほしい。
  • 今のままの構成で問題ない場合は、contorollerでどのようにデータを結合させたらいいのか教えてほしい。

試したこと

色んな手段で検索しましたが、JPAを使用した方法しか見つからず前に進めませんでした。

補足情報(FW/ツールのバージョンなど)

Eclipses
Spring boot
MySQL
Java

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

写真のような一覧にしたいなら、そうなるようSQLを作成し
得られたEntity(?)をmodelに設定すればいいのではないですか?

投稿2022/07/14 04:35

Luice

総合スコア771

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問