プログラミング逆引き辞典

~ 多言語対応のプログラミングレシピ ~

MyBatis 自作のSQL作成

O/RマッパーであるMyBatisならよく使うSQLはデフォルトで使用することができる
 
また自分好みのSQL文を作成することもできる
 
この記事では自作のSQL文の作成方法を解説する
 
 
MyBatisの設定方法はこちら
MyBatis Generatorの設定方法と実行
 
 

■環境

OS:Windows

ブラウザ:Google Chrome

言語:Java11

フレームワーク:SpringBoot

DB:MySQL

ビルドツール:Gradle

 
 


■事前準備

・DBにEmployeeテーブルを作成

create table employee (
    id int primary key auto_increment
    , name varchar(100)
    , age int
    , del_flag int
    );

 
 
・MyBatisを実行してドメインクラスやマッパークラスを自動生成
MyBatisの実行方法はこちら
MyBatis Generatorの設定方法と実行
 
 


■自作のINSERT処理

①「com.example.mybatis.mapper」配下のマッパークラスを編集

・EmployeeMapper.javaに下記を記述

//戻り値 = メソッド名(引数);
int insertEmp(Employee employee);
}

 
 


②「com.example.mybatis.mapper」配下のxmlファイルを編集

・EmployeeMapper.xmlに下記を記述

 <!-- メソッド名:insertEmp 引数:com.example.domain.Employee -->
  <insert id="insertEmp" parameterType="com.example.domain.Employee">
    insert into employee (
        <if test="name != null">
            name,
        </if>
        <if test="age != null">
            age,
        </if>
        del_flag
    ) values (
        <if test="name != null">
            #{name},
        </if>
        <if test= "age != null">
            #{age},
        </if>
            "0"
    )
  </insert>

 
 
上記で下記のようなSQL文を自作したことになる
※Javaから渡される値がNullの場合を考慮してIf文で対策

insert into employee (name, age, del_flag) values ([名前], [年齢], 0);

 
 


■自作のSELECT処理

①「com.example.mybatis.mapper」配下のマッパークラスを編集

・EmployeeMapper.javaに下記を記述

//戻り値 = メソッド名(引数);
void mySelect(Employee employee);
}

 
 


②「com.example.mybatis.mapper」配下のxmlファイルを編集

・EmployeeMapper.xmlに下記を記述

<!-- 戻り値の設定 -->
<resultMap id="MyMap" type="com.example.domain.Employee">
  <id column="id" jdbcType="INTEGER" property="id" />
  <result column="name" jdbcType="VARCHAR" property="name" />
  <result column="age" jdbcType="INTEGER" property="age" />
  <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
</resultMap>

<!-- SELECT文 ※resultMapでマッピング -->
<select id="mySelect" parameterType="com.example.domain.EmployeeExample" resultMap="MyMap">
  select name, age  from employee order by id
</select>

 
 
上記で下記のようなSQL文を自作したことになる

select name, age  from employee order by id;

 
 


■自作のSELECTカウント処理

①「com.example.mybatis.mapper」配下のマッパークラスを編集

・EmployeeMapper.javaに下記を記述

//戻り値 = メソッド名(引数);
long mySelectCount(Employee employee);
}

 
 


②「com.example.mybatis.mapper」配下のxmlファイルを編集

・EmployeeMapper.xmlに下記を記述

<!-- 戻り値の設定 -->
<resultMap id="MyMap" type="com.example.domain.Employee">
  <id column="id" jdbcType="INTEGER" property="id" />
  <result column="name" jdbcType="VARCHAR" property="name" />
  <result column="age" jdbcType="INTEGER" property="age" />
  <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
</resultMap>

<!-- SELECT文 ※resultMapでマッピング -->
<select id="mySelectCount" parameterType="com.example.domain.EmployeeExample" resultType="java.lang.Long">
  select count(*)  from employee
</select>

 
 
上記で下記のようなSQL文を自作したことになる

select count(*)  from employee;