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

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

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

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

MyBatis

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

Spring Boot

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

Q&A

解決済

1回答

3554閲覧

【MyBatis】Mapperアノテーションを付けなても実行できる?

退会済みユーザー

退会済みユーザー

総合スコア0

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

MyBatis

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

Spring Boot

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

0グッド

0クリップ

投稿2022/04/10 11:22

わからないこと

Mybatis generator(https://mybatis.org/generator/) で作成したMapperインターフェースに@Mapperアノテーションが付与されていませんでした。
ですが、接続確認してみるとエラーにはならず実行できました

@Mapperアノテーションを付けないと、Mapperインターフェースを認識してくれないと考えていましたが
なぜ、つけなくても実行できているのでしょうか。

@Mapperは、どういうときにつけなくてはいけないのでしょうか?
よろしくお願いいたします。

やったこと(自動生成)

下記のような設定で、自動生成を実行しました。

xml

1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6<generatorConfiguration> 7 <context id="sadDbTables" targetRuntime="MyBatis3"> 8 <!-- DB接続設定 --> 9 <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" 10 connectionURL="jdbc:mysql://127.0.0.1/hogedb" 11 userId="hoge" 12 password="passw0rd"> 13 <property name="nullCatalogMeansCurrent" value="true" /> 14 </jdbcConnection> 15 16 <!-- Select結果がマッピングされる、Javaモデルの作成設定 --> 17 <javaModelGenerator targetPackage="com.eui.sad.db.model.generator" targetProject="sad/src/main/java"> 18 <property name="enableSubPackages" value="true" /> 19 <property name="trimStrings" value="true" /> 20 </javaModelGenerator> 21 22 <!-- SQLを定義したXMLファイルの作成設定 --> 23 <sqlMapGenerator targetPackage="com.eui.sad.db.mapper" targetProject="sad/src/main/resources"> 24 <property name="enableSubPackages" value="true" /> 25 </sqlMapGenerator> 26 27 <!-- Mapperクラスの作成設定 --> 28 <javaClientGenerator type="XMLMAPPER" targetPackage="com.eui.sad.db.mapper" targetProject="sad/src/main/java"> 29 <property name="enableSubPackages" value="true" /> 30 </javaClientGenerator> 31 32 <!-- 自動生成するテーブルの設定 --> 33 <table tableName="%" 34 enableInsert="true" 35 enableSelectByPrimaryKey="true" 36 enableUpdateByPrimaryKey="true" 37 enableDeleteByPrimaryKey="true" 38 enableSelectByExample="false" 39 enableUpdateByExample="false" 40 enableDeleteByExample="false" 41 enableCountByExample="false" 42 selectByPrimaryKeyQueryId="false" 43 selectByExampleQueryId="false" 44 modelType="flat"> 45 </table> 46 </context> 47</generatorConfiguration>

やったこと(接続確認)

とりあえず、Controllerで直接実行させてみました。
参考までに、依存関係と設定ファイルも載せます。

build.gradle(抜粋)

1dependencies { 2 implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 3 implementation 'org.springframework.boot:spring-boot-starter-web' 4 runtimeOnly 'mysql:mysql-connector-java' 5 implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2' 6 compileOnly 'org.projectlombok:lombok' 7 developmentOnly 'org.springframework.boot:spring-boot-devtools' 8 annotationProcessor 'org.projectlombok:lombok' 9 testImplementation 'org.springframework.boot:spring-boot-starter-test' 10}

application.properties

1spring.datasource.url=jdbc:mysql://localhost/hogedb 2spring.datasource.username=hoge 3spring.datasource.password=passw0rd 4spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 5mybatis.configuration.map-underscore-to-camel-case=true

java

1package com.eui.sad.controller; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Controller; 5import org.springframework.web.bind.annotation.GetMapping; 6import org.springframework.web.bind.annotation.ResponseBody; 7 8import com.eui.sad.db.mapper.Sample1Mapper; 9import com.eui.sad.db.model.generator.Sample1; 10 11@Controller 12public class SampleController { 13 14 @Autowired 15 Sample1Mapper mapper; 16 17 @GetMapping("/sample") 18 @ResponseBody 19 public String sample() { 20 Sample1 sample = mapper.selectByPrimaryKey(1); 21 return "Hello World!" + sample.getCol1(); 22 } 23} 24

java:自動生成されたMapperインターフェース

1package com.eui.sad.db.mapper; 2 3import com.eui.sad.db.model.generator.Sample1; 4 5public interface Sample1Mapper { 6 7 /** 8 * This method was generated by MyBatis Generator. This method corresponds to the database table sample1 9 * @mbg.generated Sun Apr 10 19:15:19 JST 2022 10 */ 11 int deleteByPrimaryKey(Integer id); 12 13 /** 14 * This method was generated by MyBatis Generator. This method corresponds to the database table sample1 15 * @mbg.generated Sun Apr 10 19:15:19 JST 2022 16 */ 17 int insert(Sample1 row); 18 19 /** 20 * This method was generated by MyBatis Generator. This method corresponds to the database table sample1 21 * @mbg.generated Sun Apr 10 19:15:19 JST 2022 22 */ 23 int insertSelective(Sample1 row); 24 25 /** 26 * This method was generated by MyBatis Generator. This method corresponds to the database table sample1 27 * @mbg.generated Sun Apr 10 19:15:19 JST 2022 28 */ 29 Sample1 selectByPrimaryKey(Integer id); 30 31 /** 32 * This method was generated by MyBatis Generator. This method corresponds to the database table sample1 33 * @mbg.generated Sun Apr 10 19:15:19 JST 2022 34 */ 35 int updateByPrimaryKeySelective(Sample1 row); 36 37 /** 38 * This method was generated by MyBatis Generator. This method corresponds to the database table sample1 39 * @mbg.generated Sun Apr 10 19:15:19 JST 2022 40 */ 41 int updateByPrimaryKeyWithBLOBs(Sample1 row); 42}

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

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

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

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

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

guest

回答1

0

ベストアンサー

自己解決しました

java

1package com.eui.sad; 2 3import org.mybatis.spring.annotation.MapperScan; 4import org.springframework.boot.SpringApplication; 5import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7@SpringBootApplication 8@MapperScan("com.eui.sad.db.mapper") 9public class SadApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(SadApplication.class, args); 13 } 14 15}

試行錯誤しているときに、起動時のアノテーションで@MapperScanで指定していたのが残っていたから出来ていたようです。
@MapperScanで指定したパス以下をMapperとして読み込んでいるようです。
これがない場合、@Mapperが必要になるのも確認できました。

mybatis-spring-boot-starterを利用しているときは、基本的には@MapperScanは不要になるが
自動生成したソースの出力パッケージだけは、@MapperScanで指定してあげなければいけないという理解になりました。

投稿2022/04/10 12:01

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問