実現したいこと
- 完了済みとマイタスクがうまく更新されるようにしたい
前提
Springbootでtodoアプリを作ろうとしています。
完了のチェックボックスにチェックを入れて
更新を押すとうまく更新がされません。
発生している問題・エラーメッセージ
2023-06-30T20:30:17.196+09:00 ERROR 25012 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.jdbc.BadSqlGrammarException: ### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: "where"またはその近辺で構文エラー 位置: 119 ### The error may exist in com/example/todoapp/mapper/TodoMapper.xml ### The error may involve com.example.todoapp.mapper.TodoMapper.update-Inline ### The error occurred while setting parameters ### SQL: update todo_items set title = ?, time_limit = to_date(?,'yy-mm-dd'), done_flg = ?, where id = ? ### Cause: org.postgresql.util.PSQLException: ERROR: "where"またはその近辺で構文エラー 位置: 119 ; bad SQL grammar []] with root cause org.postgresql.util.PSQLException: ERROR: "where"またはその近辺で構文エラー
該当のソースコード
java
1package com.example.todoapp.controller; 2 3import com.example.todoapp.entity.Todo; 4import com.example.todoapp.mapper.TodoMapper; 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Controller; 7import org.springframework.ui.Model; 8import org.springframework.web.bind.annotation.RequestMapping; 9 10import java.util.List; 11 12@Controller 13public class TodoController { 14 15 @Autowired 16 TodoMapper todoMapper; 17 18 @RequestMapping(value = "/") 19 public String index(Model model){ 20// List<Todo> list = todoMapper.selectAll(); 21 List<Todo> list = todoMapper.selectIncomplete(); 22 List<Todo> doneList = todoMapper.selectComplete(); 23 model.addAttribute("todos", list); 24 model.addAttribute("doneTodos",doneList); 25 26 return "index"; 27 } 28 29 @RequestMapping(value = "/add") 30 public String add(Todo todo){ 31 todoMapper.add(todo); 32 return "redirect:/"; 33 } 34 35 @RequestMapping(value = "/update") 36 public String update(Todo todo){ 37 todoMapper.update(todo); 38 return "redirect:/"; 39 } 40 41} 42 43
java
1package com.example.todoapp.mapper; 2 3import com.example.todoapp.entity.Todo; 4import org.apache.ibatis.annotations.Mapper; 5 6import java.util.List; 7 8@Mapper 9public interface TodoMapper { 10 11 public List<Todo> selectAll(); 12 13 public List<Todo> selectIncomplete(); 14 15 public List<Todo> selectComplete(); 16 17 public void add(Todo todo); 18 19 public void update(Todo todo); 20 21} 22 23
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8"> 5 <title>TodoApp</title> 6</head> 7<body> 8<h1>TodoList</h1> 9<h3>マイタスク</h3> 10<form method="post" th:action="@{/update}" th:each="todo : ${todos}" > 11 <input type="checkbox"name="done_flg" value="1"/> 12 <input type="hidden" name="id" th:value="${todo.id}" /> 13 <input type="text" name="title"th:value="${todo.title}" /> 14 <input type="date" name="time_limit"th:value="${todo.time_limit}" /> 15 <input type="submit" value="更新" /> 16</form> 17 18<h3>完了済み</h3> 19<form method="post" th:action="@{/update}" th:each="todo : ${doneTodos}" > 20 <input type="checkbox"name="done_flg" value="1"/> 21 <input type="hidden" name="id" th:value="${todo.id}" /> 22 <input type="text" name="title"th:value="${todo.title}" style="text-decoration:line-through"/> 23 <input type="date" name="time_limit"th:value="${todo.time_limit}" /> 24 <input type="submit" value="更新" /> 25</form> 26 27<h3>新しいタスクを追加</h3> 28<form method="post" th:action="@{/add}"> 29 <input type="text" name="title" /> 30 <input type="date" name="time_limit"/> 31 <input type="submit" value="追加" /> 32</form> 33 34</body> 35</html> 36 37
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="com.example.todoapp.mapper.TodoMapper"> 6 <select id="selectAll" resultType="com.example.todoapp.entity.Todo"> 7 select * from todo_items 8 </select> 9 10 <select id="selectIncomplete" parameterType="com.example.todoapp.entity.Todo"> 11 select * from todo_items where done_flg = 0 12 </select> 13 14 <select id="selectComplete" resultType="com.example.todoapp.entity.Todo"> 15 select * from todo_items where done_flg = 1 16 </select> 17 18 <insert id="add" parameterType="com.example.todoapp.entity.Todo"> 19 insert into todo_items (title,time_limit) 20 values (#{title},to_date(#{time_limit},'yy-mm-dd')) 21 </insert> 22 23 <update id="update" parameterType="com.example.todoapp.entity.Todo"> 24 update todo_items set 25 title = #{title}, 26 time_limit = to_date(#{time_limit},'yy-mm-dd'), 27 done_flg = #{done_flg}, 28 where id = #{id} 29 </update> 30</mapper>
試したこと
・追加記述の箇所の確認
・デバッグ
補足情報(FW/ツールのバージョンなど)
'org.springframework.boot' version '3.1.1'

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。