MyBatis | 検索 SELECT | resultTypeに指定できる値

resultTypeには、検索結果をJavaのどの形式に変換するかを設定する。

一般的には、java.util.Mapのように変換先クラスのFQCN(Fully Qualified Class Name)を指定する。

ただし、よく利用される一部の形式にはあらかじめ別名(alias)が定義されており、短い名前で指定できる。

以下は、org.apache.ibatis.type.TypeAliasRegistryから抜粋したものである。

別名(alias) データ形式(data type)
string String
date Date
map Map
hashmap HashMap
list List
arraylist ArrayList
decimal BigDecimal
bigdecimal BigDecimal
biginteger BigInteger
_byte byte
_long long
_short short
_int int
_integer int
_double double
_float float
_boolean boolean
_byte[] byte[]
_long[] long[]
_short[] short[]
_int[] int[]
_integer[] int[]
_double[] double[]
_float[] float[]
_boolean[] boolean[]
byte Byte
long Long
short Short
int Integer
integer Integer
double Double
float Float
boolean Boolean
byte[] Byte[]
long[] Long[]
short[] Short[]
int[] Integer[]
integer[] Integer[]
double[] Double[]
float[] Float[]
boolean[] Boolean[]
object Object
date[] Date[]
decimal[] BigDecimal[]
bigdecimal[] BigDecimal[]
biginteger[] BigInteger[]
object[] Object[]
collection Collection
iterator Iterator
ResultSet ResultSet

プリミティブ型の場合は、先頭にアンダースコア(_)を付ける。付けない場合はラッパークラスになるため注意する。

別名を定義する

別名は任意に定義できる。

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <typeAliases>
    <typeAlias type="foo.bar.Hoge" alias="Hoge" />
  </typeAliases>

  ...
</configuration>
  • メイン側の設定ファイル(mybatis-config.xml)に<typeAlias>タグで宣言できる。

パッケージを指定して別名を定義する

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <typeAliases>
    <package name="foo.bar"/>
  </typeAliases>

  ...
</configuration>
  • <package>タグを使用して、パッケージ単位で別名を定義できる。
  • この場合、クラス名がそのまま別名になる。