【java】Springbootのサービスクラスでマッパーが見つからないエラー

実現したいこと

javaのSpringBootでMyBatisのマッパーをBeanとして定義したいです。

発生している問題・分からないこと

インターフェースに@mapperアノテーションを付けているのに、サービスクラスでマッパーのBeanが見つからないというエラーが起きています。どうしたら解決できますか?
エラーの内容と、パッケージの階層の画像を載せておきます。


APPLICATION FAILED TO START


Description:
Field BookMapper in com.example.demo.service.BookService required a bean of type 'com.example.demo.repository.BookDtoMapper' 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.repository.BookDtoMapper' in your configuration.

build.gradle↓
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.0'
id 'io.spring.dependency-management' version '1.1.5'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '21'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}イメージ説明

該当のソースコード

application.properties

1spring.application.name=Book 2# Hibernate settings (optional, but recommended for JPA) 3spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 4spring.jpa.show-sql=true 5# MyBatis設定 6mybatis.config-location=classpath:mybatis-config.xml 7## 以下h2データベース 8spring.h2.console.enabled=true 9spring.datasource.url=jdbc:h2:mem:px_test;MODE=MYSQL 10spring.datasource.driverClassName=org.h2.Driver 11spring.datasource.username=sa 12spring.datasource.password= 13spring.datasource.initialization-mode=always 14#spring.datasource.schema=classpath:database/schema.sql 15#spring.datasource.data=classpath:database/data.sql 16spring.sql.init.schema-locations=classpath:database/schema.sql 17spring.sql.init.data-locations=classpath:database/data.sql 18spring.datasource.platform=h2 19spring.h2.console.path=/h2-console

BookController.java

1package com.example.demo; 2import org.springframework.beans.factory.annotation.Autowired; 3import org.springframework.stereotype.Controller; 4import org.springframework.web.bind.annotation.GetMapping; 5import org.springframework.web.bind.annotation.RequestMapping; 6import org.springframework.web.bind.annotation.RequestParam; 7import org.springframework.web.servlet.ModelAndView; 8import com.example.demo.service.BookService; 9@Controller 10public class BookController { 11@Autowired 12BookService bookService; 13@GetMapping("/") 14public ModelAndView index(ModelAndView mav) { 15 mav.setViewName("book_index"); 16 return mav; 17} 18@RequestMapping("/delete") 19public ModelAndView delete( 20 @RequestParam("bookId")Long bookId, 21 ModelAndView mav) { 22 bookService.delete(bookId); 23 return mav; 24} 25} 26

BookService.java

1package com.example.demo.service; 2import org.springframework.beans.factory.annotation.Autowired; 3import org.springframework.stereotype.Service; 4import com.example.demo.repository.BookDtoMapper; 5@Service 6public class BookService { 7@Autowired 8public BookDtoMapper BookMapper; 9public void delete(Long bookId){ 10 BookMapper.delete(bookId); 11} 12} 13

BookDtoMapper.java

1package com.example.demo.repository; 2import org.apache.ibatis.annotations.Mapper; 3 4@Mapper 5public interface BookDtoMapper{ 6public void delete(Long bookId); 7}

BookDtoMapper.xml

1<?xml version="1.0" encoding="UTF-8" ?> 2<!DOCTYPE mapper 3 PUBLIC "-http:////mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5<mapper namespace="com.example.demo.repository.BookDtoMapper"> 6<delete id="delete"> 7 delete from book 8 where id = #{bookId}; 9</delete> 10</mapper> 11

試したこと・調べたこと

上記の詳細・結果

xmlをリソースに配置してみましたが変化なしでした。

補足

特になし

コメントを投稿

0 コメント