やりたい事
MyBatisを使用したinsert処理
入力画面からidとnameを入力し、DBへinsert
エラー
11:07:01.744 [http-nio-8080-exec-9] DEBUG org.springframework.web.servlet.DispatcherServlet - Failed to complete request: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'sampleId' not found. Available parameters are [arg1, arg0, param1, param2] SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [/web_sample] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'sampleId' not found. Available parameters are [arg1, arg0, param1, param2]] with root cause [Fri Sep 04 11:07:01 JST 2020]
わからない事
・Parameter "sampleId"が見つからないという事はどういう事なのか。
入力したsampleIdとsampleNameをSQL文に渡し、insertしているので、sampleIdが見つからないという事はないと思うのですが、、
・Available parameters are [arg1, arg0, param1, param2]の意味
UserController.java
package group.study.dev.sample.search.controller; 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.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import group.study.dev.dto.sample.SampleDto; import group.study.dev.sample.search.service.UserService; import group.study.dev.web.common.controller.AbstractController; @Controller //@RequestMapping(value = "/search") public class UserController extends AbstractController { @Autowired private UserService userService; @GetMapping("/search") public String goSearch() { return "search/search"; } @GetMapping("/insert") public String goInsert() { return "search/insert"; } @PostMapping("/insert") public String postInsert(@RequestParam("sampleId")String sampleId, @RequestParam("sampleName")String sampleName, Model model) { System.out.println("sampleId = " + sampleId); System.out.println("sampleName = " + sampleName); userService.insert(sampleId, sampleName); return "search/insert"; } @PostMapping("/result") public String postSearch(@RequestParam("sampleId")String sampleId, Model model) { SampleDto list = userService.selectOne(sampleId); model.addAttribute("list", list); return "search/search"; } }
UserService.java
package group.study.dev.sample.search.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import group.study.dev.dao.sample.SampleDao; import group.study.dev.dto.sample.SampleDto; import group.study.dev.sample.search.model.UserModel; import group.study.dev.web.common.controller.AbstractController; @Service public class UserService extends AbstractController{ @Autowired private SampleDao sampleMapper; private SampleDto sampleDto; public SampleDto selectOne(String sampleId){ UserModel userModel = new UserModel(); userModel.setSampleDto(sampleMapper.selectOne(sampleId)); return userModel.getSampleDto(); } public void insert(String sampleId, String sampleName) { sampleMapper.insert(sampleId, sampleName); } }
UserModel.java
package group.study.dev.sample.search.model; import java.util.List; import group.study.dev.dto.sample.SampleDto; import group.study.dev.web.common.controller.AbstractController; import lombok.Data; @Data public class UserModel extends AbstractController{ private SampleDto sampleDto; private List<SampleDto> sampleList;//コントローラのリスト名と同じにする }
SampleDto.java
package group.study.dev.dto.sample; import lombok.Data; @Data public class SampleDto { private String sampleId; private String sampleName; }
SampleDao.java
ackage group.study.dev.dao.sample; import java.util.List; import org.apache.ibatis.annotations.Mapper; import group.study.dev.dto.sample.SampleDto; @Mapper public interface SampleDao { public List<SampleDto> selectAll(); public SampleDto selectOne(String sampleId); public boolean insert(String sampleId, String sampleName); public boolean updateOne(SampleDto sampleDto); public boolean deleteOne(String userId); }
SampleMapper.xml
<?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 namespace="group.study.dev.dao.sample.SampleDao"> <resultMap type="group.study.dev.dto.sample.SampleDto" id="user"> <id column="sample_id" property="sampleId" /> <id column="sample_name" property="sampleName" /> </resultMap> <select id="selectAll" resultType="group.study.dev.dto.sample.SampleDto"> select sample_name, sample_id from public.sample_table where sample_id = 'S003'; </select> <select id="selectOne" parameterType="string" resultType="group.study.dev.dto.sample.SampleDto"> select sample_id as sampleId, sample_name as sampleName from sampletable where sample_id = #{sampleId} </select> <insert id="insert" parameterType="group.study.dev.dto.sample.SampleDto"> insert into sampletable(sample_id,sample_name) values (#{sampleId}, #{sampleName}) </insert> </mapper>
search.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String method = request.getMethod(); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>hoge</title> </head> <body> <% if ("GET".equals(method)) { %> <h1>HOME</h1> <div> <form method="post" action="/web_sample/search/result"> <input type="text" name="sampleId" /> <button type="submit" value="submit">送信</button> </form> </div> <div> <form method="post" action="/web_sample/search/insert"> <button type="submit" value="insert">新規登録</button> </form> </div> <% } else { %> <h1>検索結果</h1> <table border="1" bordercolor="#333333"> <tr> <th>ID</th> <th>NAME</th> </tr> <tr> <td><c:out value="${list.sampleId}" /></td> <td><c:out value="${list.sampleName}" /></td> </tr> </table> <% } %> </body> </html>
insert.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>新規登録画面</title> </head> <body> <form method="post" action="/web_sample/insert"> ID:<input type="text" name="sampleId"> NAME:<input type="text" name="sampleName"> <button type="submit" value="登録">登録</button> </form> </body> </html>