実現したいこと
MapperにBeanが注入できないというエラーを解決したい
前提
「後悔しないための」spring boot入門書」という参考書を見ながらその通りにサンプルコードを作成しています。
やっていることは、MyBatisを使ったデータベース操作です。
このアプリを実行すると、ApplicationContextの起動時にUserServiceImplクラスのmapperフィールドにUserMapper型のBeanがインジェクションできていないというようなエラーメッセージが発生し、解決しません。
どうかよろしくお願いします。

発生している問題・エラーメッセージ
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. [2m2024-06-12T13:18:27.550+09:00[0;39m [31mERROR[0;39m [35m22908[0;39m [2m---[0;39m [2m[SpringBootSample3] [ restartedMain][0;39m [2m[0;39m[36mo.s.b.d.LoggingFailureAnalysisReporter [0;39m [2m:[0;39m *************************** APPLICATION FAILED TO START *************************** Description: Field mapper in com.example.domain.user.service.impl.UserServiceImpl required a bean of type 'com.example.repository.UserMapper' 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.repository.UserMapper' in your configuration.
該当のソースコード
java
1package com.example.controller;2 3import java.util.Locale;4import java.util.Map;5 6import org.modelmapper.ModelMapper;7import org.springframework.beans.factory.annotation.Autowired;8import org.springframework.stereotype.Controller;9import org.springframework.ui.Model;10import org.springframework.validation.BindingResult;11import org.springframework.validation.annotation.Validated;12import org.springframework.web.bind.annotation.GetMapping;13import org.springframework.web.bind.annotation.ModelAttribute;14import org.springframework.web.bind.annotation.PostMapping;15import org.springframework.web.bind.annotation.RequestMapping;16 17import com.example.application.service.UserApplicationService;18import com.example.domain.user.model.MUser;19import com.example.domain.user.service.UserService;20import com.example.form.GroupOrder;21//import com.example.form.GroupOrder;22import com.example.form.SignupForm;23 24import lombok.extern.slf4j.Slf4j;25 26@Controller27@RequestMapping("/user")28//Slf4jはLombokのアノテーションです。これをクラスに付けるとSlf4jを使って、簡単にログ出力できます29@Slf4j30public class SignupController {31 32 @Autowired33 private UserApplicationService userApplicationService;34 35 @Autowired36 private UserService userService;37 38 @Autowired39 private ModelMapper modelMappeer;40 41 42// ユーザー登録画面を表示43 @GetMapping("/signup")44 public String getSignup(Model model,Locale locale,45 @ModelAttribute SignupForm form) {46 47// 性別を取得48 Map<String,Integer> genderMap=userApplicationService.getGenderMap(locale);49 model.addAttribute("genderMap",genderMap);50 51// ユーザー登録画面に遷移52 return "user/signup";53// 54 }55 56// ユーザー登録処理57 @PostMapping("/signup")58 public String postSignup(Model model,Locale locale,59 @ModelAttribute @Validated(GroupOrder.class) SignupForm form,60 BindingResult bindingResult) {61 62// 入力チェック結果63 if(bindingResult.hasErrors()) {64 65// ユーザー画面に戻る66 return getSignup(model,locale,form);67 68 }69 70 log.info(form.toString());71 72 //formをMUserクラスに変換73 MUser user=modelMappeer.map(form,MUser.class);74 75 //ユーザー登録76 userService.signup(user);77 78// ログイン画面にリダイレクト79 return "redirect:/login";80 }81 82 83 84 85}86
java
1package com.example.config;2import org.modelmapper.ModelMapper;3import org.springframework.context.annotation.Bean;4import org.springframework.context.annotation.Configuration;5 6 7@Configuration8public class javaConfig {9 10 @Bean11 public ModelMapper modelMapper() {12 return new ModelMapper();13 }14 15 16 17}
java
1package com.example.repository;2 3import org.apache.ibatis.annotations.Mapper;4 5import com.example.domain.user.model.MUser;6 7//MyBatisでリポジトリーを作成するためには@Mapperアノテーションを付けます8@Mapper9public interface UserMapper {10 11// ユーザー登録12 public int insertOne(MUser user);13 14}
java
1package com.example.domain.user.service;2import com.example.domain.user.model.MUser;3 4public interface UserService {5 6// ユーザー登録7 public void signup(MUser user);8 9}
java
1package com.example.domain.user.model;2 3import java.util.Date;4 5//ユーザマスタのエンティティクラス6//エンティティクラスとは、データベースのテーブルに対応するJavaクラスのことです。7//エンティティクラスは、データベースのレコード(行)をオブジェクトとして扱うためのモデルを提供します。8 9import lombok.Data;10 11@Data12public class MUser {13 14 private String userId;15 private String password;16 private String userName;17 private Date birthday;18 private Integer age;19 private Integer gender;20 private Integer departmentId;21 private String role;22 23}
java
1package com.example.domain.user.service;2import com.example.domain.user.model.MUser;3 4public interface UserService {5 6// ユーザー登録7 public void signup(MUser user);8 9}10
java
1package com.example.domain.user.service.impl;2 3import org.springframework.beans.factory.annotation.Autowired;4import org.springframework.stereotype.Service;5 6import com.example.domain.user.model.MUser;7import com.example.domain.user.service.UserService;8import com.example.repository.UserMapper;9 10 11//ユーザーサービスの実装クラス12@Service13public class UserServiceImpl implements UserService{14 15 @Autowired16 private UserMapper mapper;17 18// ユーザー登録19 20 @Override21 public void signup(MUser user) {22 user.setDepartmentId(1); //部署23 user.setRole("ROLE_GENERAL");24 25 mapper.insertOne(user);26 27 }28 29}30
xml
1<?xml version="1.0" encoding="UTF-8"?>2 3<!DOCTYPE mapper4 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"5 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">6 7<mapper namespace="com.example.repository.UserMapper">8 9 <insert id="insertOne" parameterType="com.example.domain.user.model.MUser">10 insert into m_user( 11 user_id, 12 password, 13 user_name, 14 birthday, 15 age, 16 gender, 17 department_id, 18 role 19 ) 20 values( 21 #{userId}, 22 #{password}, 23 #{userName}, 24 #{birthday}, 25 #{age}, 26 #{gender}, 27 #{departmentId}, 28 #{role} 29 ) 30 </insert>31 32</mapper>
application.propertiesには、下記を記述してあります。
properties
1#MyBatis2#この設定により、MyBatisはsrc/main/resources/mapper/h2/ディレクトリにある3#すべてのXMLファイルをマッパーファイルとして読み込みます。4mybatis.mapper-locations=classpath*:/mapper/h2/*.xml5 6#Log Level7#この設定により、com.exampleパッケージ内のクラスからのログメッセージは、8#DEBUGレベル以上のものがすべて出力されます。9logging.level.com.example=DEBUG
試したこと
調べたところSpringBootTest2Application.javaにて、@MapperScanアノテーションを使う方法など書いてありましたが上手く行かずエラーが増えていくだけでした。
試したこと2
spring boot の 3 系には mybatis の 3 系、 spring boot の 2 系には mybatis の 2 系でなければならないという情報を頂いたので、MyBatisのバージョンを3.0.2に書き換えてましたが、アプリ実行するとエラーになってしまいます。

発生している問題・エラーメッセージ
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. [2m2024-06-12T14:46:17.842+09:00[0;39m [31mERROR[0;39m [35m21036[0;39m [2m---[0;39m [2m[SpringBootSample3] [ restartedMain][0;39m [2m[0;39m[36mo.s.boot.SpringApplication [0;39m [2m:[0;39m Application run failed java.lang.IllegalArgumentException: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getTypeForFactoryBeanFromAttributes(FactoryBeanRegistrySupport.java:86) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:837) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:663) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:575) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:534) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:138) ~[spring-context-6.1.8.jar:6.1.8] at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:788) ~[spring-context-6.1.8.jar:6.1.8] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:606) ~[spring-context-6.1.8.jar:6.1.8] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.3.0.jar:3.3.0] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~ //省略
補足情報(FW/ツールのバージョンなど)
spring bootのバージョン;java3.3.0
javaのバージョン:22
MyBatisのバージョン:2.1.4
ModelMapperのバージョン:2.3.9

0 コメント