質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
MyBatis

MyBatisはJavaや.NET Frameworkでなどで使用できる、SQL文や、ストアドプロシージャをオブジェクトと紐付けるO/Rマッピングフレームワークです。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

2回答

5796閲覧

SpringBoot + MyBatis Autowired時にエラーが発生する

yuu_info

総合スコア14

MyBatis

MyBatisはJavaや.NET Frameworkでなどで使用できる、SQL文や、ストアドプロシージャをオブジェクトと紐付けるO/Rマッピングフレームワークです。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2021/01/26 09:19

編集2022/01/12 10:55

前提・実現したいこと

サービスクラスでマッパーインターフェースを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の配下にいるのに本エラーがでてしまうのはなぜでしょうか・・・

###補足
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

MyBatisのマッピング用インタフェースですので、
@Repositoryではなく、@Mapperを宣言してください。

※ org.apache.ibatis.annotations.Mapper

投稿2021/01/26 11:54

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yuu_info

2021/01/27 01:14

ご回答ありがとうございます! ご指摘いただいた通り@Repositoryから@Mapperに修正しましたが同じエラーが発生し、解決には至りませんでした。
A-pZ

2021/01/27 05:35

なるほど。他に考えられるものとして、質問文に > ComponentScanでcom.example.demo.mapperを明示的に指定する事でエラーは出なくなりました この設定は現在外されていますよね?(他にも、@SpringBootApplicationでComponentScanの設定ができるのですが、その設定も特に書かれていない状態でしょうか)
yuu_info

2021/01/28 00:39

返信遅れてしまい申し訳ありません。 はい、その設定は外しています! ComponentScanのアノテーションもつけておらずデフォルトの状態です。
yuu_info

2021/01/28 01:10 編集

@SpringBootApplication(scanBasePackages ={"demo"})でdemo配下のパッケージを明示的にスキャンするようにしたところ、エラーは発生しなくなりました。しかし@Controllerを付与したコントローラークラスが機能しません。(機能しないというのはURLのマッピングが行われず/helloにアクセスしても404が出てしまうということです)
A-pZ

2021/01/28 03:50

ありがとうございます。 scanBasePackageが未定義のときは、@SpringBootApplicationが配置されているパッケージ(今回で言えば、com.example.demo でしょうか)と、そのサブパッケージがすべて対象になります。もし定義した場合には定義したパッケージとそのサブパッケージのみが対象になるはずですので、現在質問文に貼っていただいたControllerやMapperインタフェースのソースが正しいものでしたら、大丈夫なはずです。 なお、何らかのコンパイルエラーがあった場合などは最新の状態で反映されませんので(大丈夫だとは思いますが…)、念のため対象のApplicationクラスのパッケージやMapperのパッケージなども再確認してください。
yuu_info

2021/01/28 04:05

###Application ``` package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication(scanBasePackages = {"demo"}) public class Hk2ConvertApplication { public static void main(String[] args) { SpringApplication.run(Hk2ConvertApplication.class, args); } } ``` こちらApplicationのクラスです。 controllerクラスもdemo.controllerとdemoパッケージの配下内にありますので本来であれば機能するはずだと認識しているのですがほかに考えられる点などありますでしょうか・・・
A-pZ

2021/01/28 04:38

ですね、demoパッケージ以下に配置されているものが対象にはなります。(ただしこの設定は、demo. 以下 の意味になり、 com.example.demo. 以下は対象になりません)
yuu_info

2021/01/28 04:56

なるほど・・・com.example.demoの配下全てを対象にしたい為basePackageScanに「com.example.demo」と指定したのですが、その配下にあるはずのserviceパッケージのserviceクラスがmapperパッケージのmapperクラスを見つけることが出来ない(質問文で貼ったエラー)が発生してしまいます。パッケージ構成が間違っているとは思えないのですがもし間違ってないと仮定した場合他に考えられる事はありますでしょうか。
guest

0

根本的解決にならないかもしれませんが、
@Import書いてみてはどうでしょう。

例:

java

1 2@Service 3@Import({ChargeCustomerMapper.class}) 4public class HK2ConvertService { 5 6

投稿2021/01/26 10:23

m.ts10806

総合スコア80765

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yuu_info

2021/01/27 01:12

ご回答ありがとうございます!試してみたところ「Specified class is an interface」というエラーが発生し解決には至りませんでした。インタフェースはインポートできないようですね・・・
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問