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

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

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

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

Spring Boot

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

Q&A

2回答

277閲覧

SpringbootでMybatisGeneratorを利用して作成したMapperクラスで@Mapperにエラーが出てしまいます

mitsuggk

総合スコア0

MyBatis

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

Spring Boot

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

0グッド

0クリップ

投稿2024/03/06 23:52

実現したいこと

プログラムを初めて数か月で言っていることがわからない等、不明点がございましたら申し訳ございません。

MybatisGeneratorを利用して作成したMapperクラスにアノテーションMapperを利用するとエラーが出てしまいますので解決方法、及び原因を教えていただけますと幸いです

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

下記エラーが出てアノテーションMapperが機能しません。
原因を調べpom.xmlに依存関係を注入したり、
application.propertiesにmybatis.mapper-locations=”~~”
を追加したのですがエラーが解決しないためご教授いただけますと幸いです。

複数テーブルを利用したWEBサイトを作成しており、すべてのMapper.javaファイルにエラーが出ています。

エラーメッセージ

error

1インポートされた org.apache.ibatis.annotations.Mapper は見つかりません

該当のソースコード

Mapper.java

1package com.example.demo.mybatis; 2 3import com.example.demo.entity.domain.AdminUser; 4import com.example.demo.entity.domain.AdminUserExample; 5import com.example.demo.entity.domain.AdminUserKey; 6import java.util.List; 7import org.apache.ibatis.annotations.Mapper; 8import org.apache.ibatis.annotations.Param; 9 10@Mapper 11public interface AdminUserMapper { 12 /** 13 * This method was generated by MyBatis Generator. 14 * This method corresponds to the database table admin_user 15 * 16 * @mbg.generated 17 */ 18 long countByExample(AdminUserExample example); 19 20 /** 21 * This method was generated by MyBatis Generator. 22 * This method corresponds to the database table admin_user 23 * 24 * @mbg.generated 25 */ 26 int deleteByExample(AdminUserExample example); 27 28 /** 29 * This method was generated by MyBatis Generator. 30 * This method corresponds to the database table admin_user 31 * 32 * @mbg.generated 33 */ 34 int deleteByPrimaryKey(AdminUserKey key); 35 36 /** 37 * This method was generated by MyBatis Generator. 38 * This method corresponds to the database table admin_user 39 * 40 * @mbg.generated 41 */ 42 int insert(AdminUser row); 43 44 /** 45 * This method was generated by MyBatis Generator. 46 * This method corresponds to the database table admin_user 47 * 48 * @mbg.generated 49 */ 50 int insertSelective(AdminUser row); 51 52 /** 53 * This method was generated by MyBatis Generator. 54 * This method corresponds to the database table admin_user 55 * 56 * @mbg.generated 57 */ 58 List<AdminUser> selectByExample(AdminUserExample example); 59 60 /** 61 * This method was generated by MyBatis Generator. 62 * This method corresponds to the database table admin_user 63 * 64 * @mbg.generated 65 */ 66 AdminUser selectByPrimaryKey(AdminUserKey key); 67 68 /** 69 * This method was generated by MyBatis Generator. 70 * This method corresponds to the database table admin_user 71 * 72 * @mbg.generated 73 */ 74 int updateByExampleSelective(@Param("row") AdminUser row, @Param("example") AdminUserExample example); 75 76 /** 77 * This method was generated by MyBatis Generator. 78 * This method corresponds to the database table admin_user 79 * 80 * @mbg.generated 81 */ 82 int updateByExample(@Param("row") AdminUser row, @Param("example") AdminUserExample example); 83 84 /** 85 * This method was generated by MyBatis Generator. 86 * This method corresponds to the database table admin_user 87 * 88 * @mbg.generated 89 */ 90 int updateByPrimaryKeySelective(AdminUser row); 91 92 /** 93 * This method was generated by MyBatis Generator. 94 * This method corresponds to the database table admin_user 95 * 96 * @mbg.generated 97 */ 98 int updateByPrimaryKey(AdminUser row); 99}

pom.xml

1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-parent</artifactId> 9 <version>3.2.3</version> 10 <relativePath /> <!-- lookup parent from repository --> 11 </parent> 12 <groupId>com.example</groupId> 13 <artifactId>pro_ec</artifactId> 14 <version>0.0.1-SNAPSHOT</version> 15 <name>pro_ec</name> 16 <description>Demo project for Spring Boot</description> 17 <properties> 18 <java.version>21</java.version> 19 </properties> 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-thymeleaf</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-web</artifactId> 28 </dependency> 29 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-devtools</artifactId> 33 <scope>runtime</scope> 34 <optional>true</optional> 35 </dependency> 36 <dependency> 37 <groupId>com.mysql</groupId> 38 <artifactId>mysql-connector-j</artifactId> 39 <scope>runtime</scope> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter-test</artifactId> 44 <scope>test</scope> 45 </dependency> 46 <dependency> 47 <groupId>org.mybatis</groupId> 48 <artifactId>mybatis</artifactId> 49 <version>3.1.0</version> 50 </dependency> 51 <dependency> 52 <groupId>mysql</groupId> 53 <artifactId>mysql-connector-java</artifactId> 54 <version>8.0.30</version> 55 </dependency> 56 <!-- 57 https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> 58 <dependency> 59 <groupId>org.mybatis.spring.boot</groupId> 60 <artifactId>mybatis-spring-boot-starter</artifactId> 61 <version>3.0.3</version> 62 </dependency> 63 <!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core --> 64<dependency> 65 <groupId>org.apache.ibatis</groupId> 66 <artifactId>ibatis-core</artifactId> 67 <version>3.0</version> 68</dependency> 69 </dependencies> 70 71 <build> 72 <plugins> 73 <plugin> 74 <groupId>org.springframework.boot</groupId> 75 <artifactId>spring-boot-maven-plugin</artifactId> 76 </plugin> 77 <plugin> 78 <groupId>org.mybatis.generator</groupId> 79 <artifactId>mybatis-generator-maven-plugin</artifactId> 80 <version>1.4.1</version> 81 </plugin> 82 </plugins> 83 84 </build> 85 86</project> 87

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

他試したことですと
Mapperconfigを作成して下記のような形でMapper.javaとxmlが集まっているファイルまでパスを通しました。
package com.example.demo.mybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.example.demo.entity.mybatis")

public class MapperConfig {

}

補足

特になし

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

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

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

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

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

guest

回答2

0

動作を確認することはできませんが、Maven のライブラリ周りが原因かもしれません。

  1. ibatis-core を削除: ibatis-core は 2010 年にリリースされたため、pom.xml から削除しても問題ありません。(Spring Boot 3 ではおそらく動作しません)

次のアドレスにアクセスしてください:
ポートされます。このため、1.4.1 から 1.4.2 に移行してください。
次のアドレスにアクセスしてください:
3. m2 フォルダの削除: 上記の手順でうまくいかない場合は、.m2 フォルダを削除して、Mavenを再ダウンロードを試してみてください。
https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core geometry dash breeze

投稿2024/03/12 09:01

mageorrec

総合スコア2

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

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

0

動作確認はしていないのですが、Mavenのライブラリ周りが原因かなと思います。

【1. ibatis-coreの削除】
ibatis-coreは2010年リリースの古いものなので、pom.xmlから削除しても問題ございません。(恐らくSpring Boot 3で動作しません)
https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core

【2. mybatis-generator-maven-pluginのアップグレード】
Spring Boot 3対応は1.4.2からになります。このため、1.4.1 → 1.4.2にアップグレードください。
https://github.com/mybatis/generator/releases

【3. m2フォルダの削除】
上記でうまくいかない場合は、.m2フォルダを削除して、Mavenの再ダウンロードをお試しください。
Windowsの場合: C:/Users/ユーザー名/.m2
Macの場合: /Users/ユーザー名/.m2


余談ですが、mybatis-spring-boot-starterにはMapperScan機能があるので、以下のコードは書かなくても大丈夫です。

java

1/* 2@Configuration 3@MapperScan("com.example.demo.entity.mybatis") 4public class MapperConfig { 5} 6*/

投稿2024/03/07 09:34

編集2024/03/07 09:35
KT001

総合スコア618

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問