Spring bootにてMy Batisを利用しinsert部分作成中における質問
Q&A
Closed
解決したいこと
Spring解体新書という書籍で8章部分を実施中です。
この章の内容としてはMyBatisについての学習していく章であり、まずinsertを実装するための流れですが、起動時にエラーが起こっていて原因がつかめないので解決方法を教えてください。
発生している問題・エラー
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'userMapper' defined in file [C:\Users\User\Documents\workspace-spring-tool-suite-4-4.24.0.RELEASE\SpringBootSample\target\classes\com\example\demo\repository\UserMapper.class]: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:857) ~[spring-beans-6.1.13.jar:6.1.13]
at org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:743) ~[spring-beans-6.1.13.jar:6.1.13]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAnnotationOnBean(DefaultListableBeanFactory.java:735) ~[spring-beans-6.1.13.jar:6.1.13]
at org.springframework.boot.sql.init.dependency.AnnotationDependsOnDatabaseInitializationDetector.detect(AnnotationDependsOnDatabaseInitializationDetector.java:36) ~[spring-boot-3.3.4.jar:3.3.4]
at org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor.detectDependsOnInitializationBeanNames(DatabaseInitializationDependencyConfigurer.java:152) ~[spring-boot-3.3.4.jar:3.3.4]
at org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor.postProcessBeanFactory(DatabaseInitializationDependencyConfigurer.java:115) ~[spring-boot-3.3.4.jar:3.3.4]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:363) ~[spring-context-6.1.13.jar:6.1.13]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:197) ~[spring-context-6.1.13.jar:6.1.13]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:789) ~[spring-context-6.1.13.jar:6.1.13]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:607) ~[spring-context-6.1.13.jar:6.1.13]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.3.4.jar:3.3.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.3.4.jar:3.3.4]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.3.4.jar:3.3.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) ~[spring-boot-3.3.4.jar:3.3.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) ~[spring-boot-3.3.4.jar:3.3.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) ~[spring-boot-3.3.4.jar:3.3.4]
at com.example.demo.SpringBootSample1Application.main(SpringBootSample1Application.java:10) ~[classes/:na]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.3.4.jar:3.3.4]
関連しそうなソースコード
■UserMapper.java
package com.example.demo.repository;
import org.apache.ibatis.annotations.Mapper;
import com.example.demo.domain.user.model.MUser;
@Mapper
public interface UserMapper {
/*ユーザ登録*/
public int insertOne(MUser user);
}
■JavaConfig.java
package com.example.demo.config;
import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JavaConfig {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
■SignupController.java
package com.example.demo.controller;
import java.util.Locale;
import java.util.Map;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.application.service.UserApplicationService;
import com.example.demo.domain.service.UserService;
import com.example.demo.domain.user.model.MUser;
import com.example.demo.form.GroupOrder;
import com.example.demo.form.SignupForm;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequestMapping("/user")
@Slf4j
public class SignupController {
@Autowired
private UserApplicationService userApplicationService;
@Autowired
private UserService userService;
@Autowired
private ModelMapper modelMapper;
/**ユーザ登録画面を表示*/
@GetMapping("/signup")
public String getSignup(Model model, Locale locale, @ModelAttribute SignupForm form) {
//性別を取得
Map<String, Integer> genderMap = userApplicationService.getGendermap(locale);
model.addAttribute("genderMap", genderMap);
//ユーザ登録画面に遷移
return "user/signup";
}
/**ユーザ登録処理*/
@PostMapping("/signup")
public String postSignup(Model model, Locale locale, @ModelAttribute @Validated(GroupOrder.class) SignupForm form,
BindingResult bindingResult) {
//入力チェック結果
if (bindingResult.hasErrors()) {
//NG:ユーザ登録画面に戻ります
return getSignup(model, locale, form);
}
log.info(form.toString());
//formをMUserクラスに変換
MUser user = modelMapper.map(form, MUser.class);
//ユーザ登録
userService.signup(user);
//ログイン画面にリダイレクト
return "redirect:/login";
}
}
■UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- Mapperとxmlのマッピング-->
<mapper namespace="com.example.demo.repository.UserMapper">
<!-- ユーザ1件登録-->
<insert id="insertOne">
insert into m_user(
user_id
,password
,user_name
,birthday
,age
,gender
department_id
,role
)
values (
#{userId}
,#{password}
,#{userName}
,#{birthday}
,#{age}
,#{gender}
,#{department_Id}
,#{role}
)
</insert>
</mapper>
■schema.sql
CREATE TABLE IF NOT EXISTS employee (
id VARCHAR(50)PRIMARY KEY,
name VARCHAR(50),
age INT
);
/*ユーザマスタ*/
CREATE TABLE IF NOT EXISTS m_user {
user_id VARCHAR(50) PRIMARY KEY
,password VARCHAR(100)
,user_name VARCHAR(50)
,birthday DATE
,age INT
,gender INT
,dapartment_id INT
,role VARCHAR(50)
};
/*部署マスタ*/
CREATE TABLE IF NOT EXISTS m_department (
department_id INT PRIMARY KEY
,department_name VARCHAR(50)
);
/*給料テーブル*/
CREATE TABLE IF NOT EXISTS t_salary (
user_id VARCHAR(50)
,year_month VARCHAR(50)
,salary INT
,PRIMARY KEY(user_id, year_month)
);
自分で試したこと
エラーをネットで調べてbean名が競合などヒットしましたが、解決案に辿りつけていない状態です。
アドバイス等頂けると幸いです。