0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Spring bootにてMy Batisを利用しupdate・delete処理作成での躓き備忘録

Posted at

Spring解体新書という書籍で8章部分を実施していた。

■問題内容
エラーなくコーディング自体はできているがユーザ詳細画面より、削除ボタンを押しても、ユーザ一覧にリダイレクトされるだけで、ユーザーが削除されない。
更新ボタンを押しても、ユーザ一覧にリダイレクトさえされず何も起こらないという状態。。(動作が期待通りの動きではない。)

image.png

色々調べていくと、以下のコントローラーでユーザーの内容を受け取れていなかった。
(詳しくはuser_id、nser_name、department_idの3カラム)

package com.example.demo.controller;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.demo.domain.user.model.MUser;
import com.example.demo.domain.user.service.UserService;
import com.example.demo.form.UserDetailForm;

@Controller
@RequestMapping("/user")
public class UserDetailController {

    @Autowired
    private UserService userService;

    @Autowired
    private ModelMapper modelMapper;

    /** ユーザー詳細画面を表示 */
    @GetMapping("/detail/{userId:.+}")
    public String getUser(UserDetailForm form, Model model, @PathVariable("userId") String userId) {

        // ユーザーを1件取得
        MUser user = userService.getUserOne(userId);
        user.setPassword(null);

        // MUserをformに変換
        form = modelMapper.map(user, UserDetailForm.class);

        // Modelに登録
        model.addAttribute("userDetailForm", form);

        // ユーザー詳細画面を表示
        return "user/detail";
    }
    
    /** ユーザー更新処理 */
    @PostMapping(value ="/detail", params = "update")
    public String updateUser(UserDetailForm form, Model model) {
    	
    	//ユーザーを更新
    	userService.updateUserOne(form.getUserId(),form.getPassword(), form.getUserName());
    	
    	//ユーザー一覧画面にリダイレクト
    	return "redirect:/user/list";
    }
    
    /** ユーザー削除処理*/
    @PostMapping(value = "/detail", params="delete")
    public String deleteUser(UserDetailForm form, Model model ) {
    	
    	//ユーザーを削除
    	userService.deleteUserOne(form.getUserId());
    	
    	//ユーザー一覧画面にリダイレクト
    	return "redirect:/user/list";
    }
}

■原因判明
qiitaにて質問を続け判明。
UserMapper.xmlの中でマッピング定義にてカラムuser_idをMUserのuserIdに対応させているのに、修正前のSQL(内)にてuser_idではなくuserIdとして取得してしまっていることが原因

image.png

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- Mapperとxmlのマッピング -->
<mapper namespace="com.example.demo.repository.UserMapper">

	<!-- マッピング定義ユーザー -->
	<resultMap type="com.example.demo.domain.user.model.MUser" id="user">
		<id column="user_id" property="userId" />
		<result column="password" property="password" />
		<result column="user_name" property="userName" />
		<result column="birthday" property="birthday" />
		<result column="age" property="age" />
		<result column="gender" property="gender" />
		<result column="department_id" property="departmentId" />
		<result column="role" property="role" />
	</resultMap>

	<!-- ユーザー1件登録 -->
	<insert id="insertOne">
		insert into m_user(
		user_id
		,password
		,user_name
		,birthday
		,age
		,gender
		,department_id
		,role
		)
		values (
		#{userId}
		,#{password}
		,#{userName}
		,#{birthday}
		,#{age}
		,#{gender}
		,#{departmentId}
		,#{role}
		)
</insert>

	<!-- ユーザー複数件取得 -->
	<select id="findMany" resultType="com.example.demo.domain.user.model.MUser">
		select
		user_id as userId
		,password
		,user_name as userName
		,birthday
		,age
		,gender
		,department_id as departmentId
		,role
		from
		m_user
	</select>

	<!-- ユーザー1件検索 -->
	<select id="findOne" resultMap="user">
		select
		user_id as userId
		,password
		,user_name as userName
		,birthday
		,age
		,gender
		,department_id as departmentId
		,role
		from
		m_user
		where
		user_id =#{userId}
	</select>

	<!-- ユーザー1件更新 -->
	<update id="updateOne">
		update
		 m_user
		set
		 password = #{password}
		 ,user_Name = #{userName}
		where
		 user_Id = #{userId}
	</update>

	<!-- ユーザー1件削除 -->
	<delete id="deleteOne">
		delete from
		 m_user
		where
		 user_Id = #{userId}
</delete>
</mapper>

■改修内容
UserMapper.xmlの以下部分(selectの内容)を修正
・旧

	<!-- ユーザー1件検索 -->
	<select id="findOne" resultMap="user">
		select
		user_id as userId
		,password
		,user_name as userName
		,birthday
		,age
		,gender
		,department_id as departmentId
		,role
		from
		m_user
		where
		user_id =#{userId}
	</select>

・新(改修後)

	<!-- ユーザー1件検索 -->
	<select id="findOne" resultMap="user">
		select
		*
		from
		m_user
		where
		user_id =#{userId}
	</select>

■感想
エラーも発生しておらず、ソースはサンプルソースと漏れがないように比較していたので、解消するのに時間がかかった。
またとにかく手を動かすことを大事にしていて、内容の理解が未熟であった。
SQL の結果をどのように Java のクラスに対応づけているかについて考えることのできる失敗であった。
動きがわからないときはサンプルソースを起動させて、違いを見る。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?