前提・実現したいこと
mybatisを使ってデータベースからデータを持ってきたい。
mapperクラスを作成して@Selectを使い、それをserviceクラスで使うようにしたい。
発生している問題・エラーメッセージ
Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
該当のソースコード
UserDao
1package com.example.demo; 2 3import java.util.List; 4 5import org.apache.ibatis.annotations.Mapper; 6import org.apache.ibatis.annotations.Select; 7 8@Mapper 9public interface UserDao { 10 11 @Select("select * from user where id=#{id}") 12 User findById(int id); 13 14 @Select("select * from user") 15 List<User> findAll(); 16}
UserService
1package com.example.demo; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7 8@Service 9public class UserService { 10 11 @Autowired 12 private UserDao userDao; 13 14 public List<User> findAll() { 15 return userDao.findAll(); 16 } 17 18 public User findById(User user) { 19 return userDao.findById(user.getId()); 20 } 21 22}
index
1package com.example.demo; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Controller; 5import org.springframework.web.bind.annotation.GetMapping; 6import org.springframework.web.bind.annotation.ModelAttribute; 7import org.springframework.web.bind.annotation.PostMapping; 8 9@Controller 10public class index { 11 12 @Autowired 13 private UserService userService; 14 15 //inputするためのリクエスト 16 @GetMapping("/login") 17 public String input(@ModelAttribute("user") User user) { 18 return "input"; 19 } 20 21 //入力された後のリクエスト 22 @PostMapping("/go") 23 public String register(@ModelAttribute("user") User user) { 24 return "result"; 25 } 26}
UserDaoがmapperとしての役割を担っています。
試したこと
エラー内容を見ると、userDaoがbeanとして認識されていないような気がします。
UserServiceクラスで認識できていないのでしょうか?
わかるかたいましたら、アドバイスいただけると嬉しいです。
回答1件
あなたの回答
tips
プレビュー