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

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

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

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

MyBatis

MyBatisはJavaや.NET Frameworkでなどで使用できる、SQL文や、ストアドプロシージャをオブジェクトと紐付けるO/Rマッピングフレームワークです。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

2083閲覧

springBootでmyBatisを使用しテーブル結合を試みるもうまくいきません。

mameshibasan

総合スコア0

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

MyBatis

MyBatisはJavaや.NET Frameworkでなどで使用できる、SQL文や、ストアドプロシージャをオブジェクトと紐付けるO/Rマッピングフレームワークです。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2021/07/08 13:06

前提・実現したいこと

springBootで社員管理システムを作成しています。
myBatisでのテーブル結合がうまくいかず、3つのテーブルのうち、1つのテーブルの情報がcontrollerに反映されません。
<参考図書> 後悔しないためのSpring Boot 入門書:Spring 解体新書

発生している問題・エラーメッセージ

エラーメッセージはありません。
画面(detail.html)にテーブルの情報が表示されません。

<データベース>
postgreSQL

<テーブル名>
m_user(正常:画面に表示されます)
m_department(正常:画面に表示されます)
t_salary(異常:画面に表示されません)

該当のソースコード

userDetailController

1package com.example.domain.controller; 2 3import org.modelmapper.ModelMapper; 4import org.springframework.beans.factory.annotation.Autowired; 5import org.springframework.stereotype.Controller; 6import org.springframework.ui.Model; 7import org.springframework.web.bind.annotation.GetMapping; 8import org.springframework.web.bind.annotation.PathVariable; 9import org.springframework.web.bind.annotation.PostMapping; 10import org.springframework.web.bind.annotation.RequestMapping; 11 12import com.example.domain.form.UserDetailForm; 13import com.example.domain.model.MUser; 14import com.example.domain.service.UserService; 15 16@Controller 17@RequestMapping("/user") 18public class UserDetailController { 19 20 @Autowired 21 private UserService userService; 22 23 @Autowired 24 private ModelMapper modelMapper; 25 26 //ユーザー詳細画面を表示 27 @GetMapping("/detail/{userId:.+}") 28 public String getUser(UserDetailForm form, Model model, @PathVariable("userId") String userId) { 29 30 //ユーザー1件を取得 31 MUser user = userService.getUserOne(userId); 32 System.out.println(user);//salaryList内が空になっています。 33 user.setPassword(null); 34 35 //userをformに変換 36 form = modelMapper.map(user, UserDetailForm.class); 37 form.setSalaryList(user.getSalaryList()); 38 39 //Modelに登録 40 model.addAttribute("userDetailForm", form); 41 42 //ユーザー詳細画面を表示 43 return "user/detail"; 44 }

UserService

1package com.example.domain.service.impl; 2 3import java.util.List; 4import org.springframework.beans.factory.annotation.Autowired; 5import org.springframework.stereotype.Service; 6import com.example.domain.model.MUser; 7import com.example.domain.repository.UserMapper; 8import com.example.domain.service.UserService; 9 10@Service 11public class UserServiceImpl implements UserService { 12 13 @Autowired 14 private UserMapper mapper; 15 16 //ユーザー1件取得 17 @Override 18 public MUser getUserOne(String userId) { 19 return mapper.findOne(userId); 20 } 21

UserMapper

1package com.example.domain.repository; 2 3import org.apache.ibatis.annotations.Mapper; 4import com.example.domain.model.MUser; 5 6@Mapper 7public interface UserMapper { 8 //ユーザー取得(1件) 9 public MUser findOne(String userId); 10}

MUser

1package com.example.domain.model; 2import java.util.Date; 3import java.util.List; 4import lombok.Data; 5 6@Data 7public class MUser { 8 private String userId; 9 private String password; 10 private String userName; 11 private Date birthday; 12 private Integer age; 13 private Integer gender; 14 private Integer departmentId; 15 private String role; 16 private Department department; 17 private List<Salary> salaryList; 18} 19

UserMapperXML

1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3<!-- Mapper と xml の マッピング --> 4<mapper namespace="com.example.domain.repository.UserMapper"> 5 6<!-- マッピング 定義( ユーザー) --> 7<resultMap type="com.example.domain.model.MUser" id="user"> 8<id column="user_id" property="userId"/> 9<result column="password" property="password"/> 10<result column="user_name" property="userName"/> 11<result column="birthday" property="birthday"/> 12<result column="age" property="age"/> 13<result column="gender" property="gender"/> 14<result column="department_id" property="departmentId"/> 15<result column="role" property="role"/> 16<association property="department" resultMap="department"/> 17<collection property="salaryList" resultMap="salary" columnPrefix="salary_"/> 18</resultMap> 19 20<!-- マッピング定義(部署) --> 21<resultMap type="com.example.domain.model.Department" id="department"> 22<id column="department_id" property="departmentId"/> 23<result column="department_name" property="departmentName"/> 24</resultMap> 25 26<!-- マッピング定義(給料) --> 27<resultMap type="com.example.domain.model.Salary" id="salary"> 28<id column="user_id" property="userId"/> 29<id column="year_month" property="yearMonth"/> //"id"を"result"に書き換えるも変化なし 30<result column="salary" property="salary"/> 31</resultMap> 32 33<!-- ユーザー1件検索 --> 34<select id="findOne" resultMap="user"> 35select 36m_user.user_id 37,m_user.password 38,m_user.user_name 39,m_user.birthday 40,m_user.age 41,m_user.gender 42,m_department.department_id 43,m_department.department_name 44,t_salary.user_id as salary_user_id 45,t_salary.year_month as salary_year_month 46,t_salary.salary as salary_salary 47from 48m_user 49left join m_department 50on m_user.department_id=m_department.department_id 51left join t_salary 52on m_user.user_id=t_salary.user_id 53where 54m_user.user_id=#{userId} 55</select>

UserDetailForm

1package com.example.domain.form; 2import java.util.Date; 3import java.util.List; 4import com.example.domain.model.Department; 5import com.example.domain.model.Salary; 6import lombok.Data; 7 8@Data 9public class UserDetailForm { 10 private String userId; 11 private String password; 12 private String userName; 13 private Date birthday; 14 private Integer age; 15 private Integer gender; 16 private Department department; 17 private List<Salary> salaryList; 18}

detailHTML

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{layout/layout}"> 3<head> 4<title>ユーザー詳細</title> 5<!-- CSS 読込 --> 6<link rel="stylesheet" th:href="@{/css/user/list.css}"> 7</head> 8<body> 9 <div layout:fragment="content"> 10 <div class="header border-bottom"> 11 <h1 class="h2">ユーザー詳細</h1> 12 </div> 13 <form id="user-detail-form" method="post" th:action="@{/user/detail}" class="form-signup" th:object="${userDetailForm}"> 14 <input type="hidden" th:field="*{userId}"/> 15 <!--ユーザー詳細情報 --> 16 <table class="table table-striped table-bordered table-hover"> 17 <tbody> 18 <tr> 19 <th class="w-25">ユーザーID</th> 20 <td th:text="*{userId}"></td> 21 </tr> 22 <tr> 23 <th>パスワード</th> 24 <td><input type="text" class="form-control" th:field="*{password}"/></td> 25 </tr> 26 <tr> 27 <th>ユーザー名</th> 28 <td><input type="text" class="form-control" th:field="*{userName}"/></td> 29 </tr> 30 <tr> 31 <th>誕生日</th> 32 <td th:text="*{#dates.format(birthday,'YYYY/MM/dd')}"></td> 33 </tr> 34 <tr> 35 <th>年齢</th> 36 <td th:text="*{age}"></td> 37 </tr> 38 <tr> 39 <th>性別</th> 40 <td th:text="*{gender==1?'男性':'女性'}"></td> 41 </tr> 42 <tr> 43 <th>部署名</th> 44 <td><span th:if="*{department !=null}" th:text="*{department.departmentName}"></span></td> 45 </tr> 46 47 </tbody> 48 </table> 49 <!-- ボタンエリア --> 50 <div class="text-center"> 51 <!-- 削除 ボタン --> 52 <button class="btn btn-danger" type="submit" name="delete">削除</button> 53 <!-- 更新 ボタン --> 54 <button class="btn btn-primary" type="submit" name="update">更新</button> 55 </div> 56 <!-- 給料情報 --> 57 <!-- <th:block th:if="*{salaryList !=null and salaryList.size()>0}"> --> 58 59 <div class="header border-bottom"> 60 <h1 class="h2">給料</h1> 61 </div> 62 <table class="table table-striped table-bordered table-hover"> 63 <thead> 64 <tr> 65 <th class="w-25">年月</th> 66 <th>給料</th> 67 </tr> 68 </thead> 69 <tbody> 70 <tr th:each="item:*{salaryList}"> 71 <td th:text="${item.yearMonth}"></td> 72 <td th:text="${#numbers.formatInteger(item.salary,3,'COMMA')}"></td> 73 </tr> 74 </tbody> 75 </table> 76 <!-- </th:block> --> 77 </form> 78 </div> 79</body> 80</html>

試したこと

  • teratailでの同事象の検索
  • 参考図書のgitHub検索
  • controllerでのSystem.out.print

 変数MUser userの中身を確認しましたが、"salaryList[]"と表示され中身がありません。
※他の要素(userId等)の情報は入っています。

  • UserMapper.xmlの記述変更

 サイトは忘れましたが

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

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

自己解決

閲覧していただいた皆様、ご迷惑をおかけいたしました。
ただの凡ミスでした。

schema.sqlとdata.sqlを使用し起動時にテーブルを作成し値を仮入力していましたが、そのテーブルの結合に利用するキーの値に間違いがありました。
それぞれのテーブルの値を一致させることにより正常動作いたしました。
お時間を頂いた皆様に申し訳ございません。
また、ご協力頂きありがとうございます。

投稿2021/07/08 23:31

mameshibasan

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問