前提
商品情報の更新機能を実装したいのですが、下記のエラーメッセージが発生し、手詰まりになっています。
わかる方がいれば教えていただきたいです。よろしくお願いします。
実現したいこと
更新処理が動作するようにする
発生している問題・エラーメッセージ
Mapper method 'com.example.demo.repository.ProductMapper.update attempted to return null from a method with a primitive return type (int).
該当のソースコード
Entity
1@Data 2public class Product { 3 4 private int id; 5 6 private String name; 7}
From
1@Data 2public class ProductForm { 3 4 @NotNull(message = "製品名/単位を入力してください") 5 @Size(min = 1, max = 100, message = "100文字以下で入力してください") 6 private String name; 7}
Mapper
1public interface ProductMapper { 2 3 @Select("UPDATE products SET name = #{name} WHERE id = #{id}") 4 int update(Product product); 5}
Service
1@Service 2@RequiredArgsConstructor 3public class ProductSericeImpl implements ProductService { 4 5 private final ProductMapper productMapper; 6 7 @Override 8 public void update(Product product) { 9 productMapper.update(product); 10 } 11 12} 13
Controller
1@Controller 2@RequiredArgsConstructor 3public class ProductController { 4 5 private final ProductService productService; 6 7@PostMapping("/update") 8 public String update( 9 @Validated @ModelAttribute ProductForm productForm, 10 BindingResult result, 11 Model model, 12 RedirectAttributes redirectAttributes 13 ) { 14 15 Product product = makeProduct(productForm); 16 17 if (result.hasErrors()) { 18 model.addAttribute("title", "商品情報 編集画面"); 19 model.addAttribute("product", productForm); 20 21 return "product/edit"; 22 else { 23 24 productService.update(product); 25 redirectAttributes.addFlashAttribute("complete", "変更が完了しました"); 26 27 return "redirect:/product/list"; 28 } 29 30private Product makeProduct(ProductForm productForm) { 31 Product product = new Product(); 32 33 product.setName(productForm.getName()); 34 35 return product; 36 } 37 38} 39
thymeleaf
1 2<form th:action="@{/product/update}" method="post" th:object="${productForm}"> 3 <div class="mt-3"> 4 <label for="productInput" class="form-label">商品名/単位</label> 5 <input type="text" name="name" class="form-control" id="name" th:value="*{name}"> 6 <div class="text-danger mb-4" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div> 7 </div> 8 <div class="text-center mt-3"> 9 <button type="submit" class="btn btn-primary">保存</button> 10 <a class="btn btn-secondary" th:href="@{/product/list}">キャンセル</a> 11 </div> 12 </form>
試したこと
Mapperクラスのupdateの返り値をvoidにしてみましたが、エラーメッセージは出なかったものの更新処理が行われませんでした。
補足情報(FW/ツールのバージョンなど)
開発環境
java8
spring boot2.7.0
mybatis-spring-boot-starter 2.2.2
thymeleaf

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/07/19 00:23 編集
2022/07/19 00:49
2022/07/19 03:53