前提・実現したいこと
サービスクラスでマッパーインターフェースをDIしたい
発生している問題・エラーメッセージ
Description: Field mapper in com.example.demo.service.HK2ConvertService required a bean of type 'com.example.demo.mapper.hk3_comsys.ChargeCustomerMapper' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.example.demo.mapper.hk3_comsys.ChargeCustomerMapper' in your configuration.
該当のソースコード
HK2ConvertController
1package com.example.demo.controller; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Controller; 5import org.springframework.ui.Model; 6import org.springframework.web.bind.annotation.RequestMapping; 7 8import com.example.demo.service.HK2ConvertService; 9 10@Controller 11public class HK2ConvertController { 12 @Autowired 13 HK2ConvertService hService; 14 15 @RequestMapping("/hello") 16 public String mainPage(Model model) { 17 model.addAttribute("customerList", hService.getCustomerList()); 18 return "main"; 19 } 20} 21
ChargeCustomerMapper
1package com.example.demo.mapper.hk3_comsys; 2 3import java.util.ArrayList; 4import java.util.List; 5 6import org.apache.ibatis.annotations.Param; 7import org.springframework.stereotype.Repository; 8 9import com.example.demo.domain.hk3_comsys.ChargeCustomer; 10import com.example.demo.domain.hk3_comsys.ChargeCustomerExample; 11import com.example.demo.domain.hk3_comsys.ChargeCustomerKey; 12 13@Repository 14public interface ChargeCustomerMapper { 15 16 public ArrayList<ChargeCustomer> getChargeCustomers(); 17 18 /** 19 * This method was generated by MyBatis Generator. This method corresponds to the database table HK3_COMSYS.CHARGE_CUSTOMER 20 * @mbg.generated Tue Jan 26 11:09:24 JST 2021 21 */ 22 long countByExample(ChargeCustomerExample example); 23 24 /** 25 * This method was generated by MyBatis Generator. This method corresponds to the database table HK3_COMSYS.CHARGE_CUSTOMER 26 * @mbg.generated Tue Jan 26 11:09:24 JST 2021 27 */ 28 int deleteByExample(ChargeCustomerExample example); 29 30 /** 31 * This method was generated by MyBatis Generator. This method corresponds to the database table HK3_COMSYS.CHARGE_CUSTOMER 32 * @mbg.generated Tue Jan 26 11:09:24 JST 2021 33 */ 34 int deleteByPrimaryKey(ChargeCustomerKey key); 35 36 /** 37 * This method was generated by MyBatis Generator. This method corresponds to the database table HK3_COMSYS.CHARGE_CUSTOMER 38 * @mbg.generated Tue Jan 26 11:09:24 JST 2021 39 */ 40 int insert(ChargeCustomer record); 41 42 /** 43 * This method was generated by MyBatis Generator. This method corresponds to the database table HK3_COMSYS.CHARGE_CUSTOMER 44 * @mbg.generated Tue Jan 26 11:09:24 JST 2021 45 */ 46 int insertSelective(ChargeCustomer record); 47 48 /** 49 * This method was generated by MyBatis Generator. This method corresponds to the database table HK3_COMSYS.CHARGE_CUSTOMER 50 * @mbg.generated Tue Jan 26 11:09:24 JST 2021 51 */ 52 List<ChargeCustomer> selectByExample(ChargeCustomerExample example); 53 54 /** 55 * This method was generated by MyBatis Generator. This method corresponds to the database table HK3_COMSYS.CHARGE_CUSTOMER 56 * @mbg.generated Tue Jan 26 11:09:24 JST 2021 57 */ 58 ChargeCustomer selectByPrimaryKey(ChargeCustomerKey key); 59 60 /** 61 * This method was generated by MyBatis Generator. This method corresponds to the database table HK3_COMSYS.CHARGE_CUSTOMER 62 * @mbg.generated Tue Jan 26 11:09:24 JST 2021 63 */ 64 int updateByExampleSelective(@Param("record") ChargeCustomer record, 65 @Param("example") ChargeCustomerExample example); 66 67 /** 68 * This method was generated by MyBatis Generator. This method corresponds to the database table HK3_COMSYS.CHARGE_CUSTOMER 69 * @mbg.generated Tue Jan 26 11:09:24 JST 2021 70 */ 71 int updateByExample(@Param("record") ChargeCustomer record, @Param("example") ChargeCustomerExample example); 72 73 /** 74 * This method was generated by MyBatis Generator. This method corresponds to the database table HK3_COMSYS.CHARGE_CUSTOMER 75 * @mbg.generated Tue Jan 26 11:09:24 JST 2021 76 */ 77 int updateByPrimaryKeySelective(ChargeCustomer record); 78 79 /** 80 * This method was generated by MyBatis Generator. This method corresponds to the database table HK3_COMSYS.CHARGE_CUSTOMER 81 * @mbg.generated Tue Jan 26 11:09:24 JST 2021 82 */ 83 int updateByPrimaryKey(ChargeCustomer record); 84}
HK2ConvertService
1package com.example.demo.service; 2 3import java.util.ArrayList; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7 8import com.example.demo.domain.hk3_comsys.ChargeCustomer; 9import com.example.demo.mapper.hk3_comsys.ChargeCustomerMapper; 10 11@Service 12public class HK2ConvertService { 13 14 @Autowired 15 ChargeCustomerMapper mapper; 16 17 public ArrayList<ChargeCustomer> getCustomerList() { 18 return mapper.getChargeCustomers(); 19 } 20 21} 22
試したこと
ComponentScanでcom.example.demo.mapperを明示的に指定する事でエラーは出なくなりましたが、コントローラーが動作しなくなりました。
###補足情報
パッケージ構成は
com.example.demoにメインアプリケーションファイルが存在し、その配下に
com.example.demo.〇〇のような形で各種コントローラーやサービス、マッパーのパッケージが存在しています(com.example.demo.controller,com.example.demo.service,com.example.demo.mapper)等
全てcom.example.demoの配下にいるのに本エラーがでてしまうのはなぜでしょうか・・・
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/27 01:14
2021/01/27 05:35
2021/01/28 00:39
2021/01/28 01:10 編集
2021/01/28 03:50
2021/01/28 04:05
2021/01/28 04:38
2021/01/28 04:56