スポンサーリンク

【SpringBoot】mapperとは?

Java
スポンサーリンク

今回の記事はSpringBootのmapperとは?という疑問に答える記事です。SpringBootはWeb開発をJavaで行う場合、非常に便利なフレームワークです。開発を行う際のディレクトリ構成やレイヤーの役割を押さえておくと開発効率的にもよいので、SpringBoot勉強中の方は是非参考にしてみてください。

また、SprtingBootの各ディレクトリ構成に関する記事や各ディレクトリの役割も別記事にまとめいているので是非参考にしてみてください。

スポンサーリンク

SpringBootにおけるmapperとは?

mapperとは文字で説明すると「DBアクセスを行う。」と説明している記事が多いです。

DBアクセスとは何かというと実際にMySQLやOracleなどのデータベースにSQLを発行してデータを取得・追加・更新・削除するということです。

repositoryからmapperインスタンスを作成し、使用します。

SpringBootにおけるmapperの実装

mapperはinterfaceと実際にSQLが記述されているファイルで構成されることが多いです。

package com.example.demo.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.example.demo.entity.User1;
import com.example.demo.entity.User1Example;

@Mapper
public interface User1Mapper {

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table user1
	 * @mbggenerated  Fri Jul 14 14:21:29 JST 2023
	 */
	int countByExample(User1Example example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table user1
	 * @mbggenerated  Fri Jul 14 14:21:29 JST 2023
	 */
	int deleteByExample(User1Example example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table user1
	 * @mbggenerated  Fri Jul 14 14:21:29 JST 2023
	 */
	int deleteByPrimaryKey(Integer id);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table user1
	 * @mbggenerated  Fri Jul 14 14:21:29 JST 2023
	 */
	int insert(User1 record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table user1
	 * @mbggenerated  Fri Jul 14 14:21:29 JST 2023
	 */
	int insertSelective(User1 record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table user1
	 * @mbggenerated  Fri Jul 14 14:21:29 JST 2023
	 */
	List<User1> selectByExample(User1Example example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table user1
	 * @mbggenerated  Fri Jul 14 14:21:29 JST 2023
	 */
	User1 selectByPrimaryKey(Integer id);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table user1
	 * @mbggenerated  Fri Jul 14 14:21:29 JST 2023
	 */
	int updateByExampleSelective(@Param("record") User1 record, @Param("example") User1Example example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table user1
	 * @mbggenerated  Fri Jul 14 14:21:29 JST 2023
	 */
	int updateByExample(@Param("record") User1 record, @Param("example") User1Example example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table user1
	 * @mbggenerated  Fri Jul 14 14:21:29 JST 2023
	 */
	int updateByPrimaryKeySelective(User1 record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table user1
	 * @mbggenerated  Fri Jul 14 14:21:29 JST 2023
	 */
	int updateByPrimaryKey(User1 record);
}

こちらがinterfaceです。

実際のSQLが入っているのは下記です。

<?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 namespace="com.example.demo.mapper.User1Mapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.User1">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>
<sql id="Example_Where_Clause">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
id, name, age
</sql>
<select id="selectByExample" parameterType="com.example.demo.entity.User1Example" resultMap="BaseResultMap">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from user1
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
select 
<include refid="Base_Column_List" />
from user1
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
delete from user1
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.example.demo.entity.User1Example">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
delete from user1
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.example.demo.entity.User1" useGeneratedKeys="true">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
insert into user1 (name, age)
values (#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.example.demo.entity.User1" useGeneratedKeys="true">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
insert into user1
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null">
name,
</if>
<if test="age != null">
age,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.example.demo.entity.User1Example" resultType="java.lang.Integer">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
select count(*) from user1
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
update user1
<set>
<if test="record.id != null">
id = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.name != null">
name = #{record.name,jdbcType=VARCHAR},
</if>
<if test="record.age != null">
age = #{record.age,jdbcType=INTEGER},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
update user1
set id = #{record.id,jdbcType=INTEGER},
name = #{record.name,jdbcType=VARCHAR},
age = #{record.age,jdbcType=INTEGER}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.example.demo.entity.User1">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
update user1
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.demo.entity.User1">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jul 14 14:21:29 JST 2023.
-->
update user1
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>

interfaceのメソッド名がxmlのidに対応しています。

こちらMybatisで自動生成したinterfaceになっております。Mybatisの使い方は別記事で記載しております。基本的にmapperはDBから自動生成可能なのでMybatisがおすすめです。

さらにプロジェクト内でどのようにmapperが使用されているかを具体的に知りたい方は下記記事で各レポジトリの役割とサンプル的なアプリコードがありますので、そちらでmapperの流れを追ってみてください。

【SpringBoot】ディレクトリ構成と各ディレクトリの役割ご紹介。

では、今回の記事は以上です。ほかにもSpringBoot関連の記事を記載しているので、是非参考にしてみてください。

コメント

タイトルとURLをコピーしました