MyBatis | Mapping Query Results to Arbitrary Java Objects | Single Object Mapping

Basic usage

Data table

test_table

id value number
1 hoge 100
2 fuga 200
3 piyo 300

Source code

TestTable.java

package sample.mybatis;

public class TestTable {

    private int id;
    private String string;
    private Embedded embedded;

    @Override
    public String toString() {
        return "TestTable [id=" + id + ", string=" + string + ", embedded=" + embedded + "]";
    }
}

Embedded.java

package sample.mybatis;

public class Embedded {

    private int number;

    @Override
    public String toString() {
        return "Embedded [number=" + number + "]";
    }
}

sample_mapper.xml

<?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="sample.mybatis">

  <resultMap type="sample.mybatis.TestTable" id="testTableResultMap">
    <id property="id" column="id" />
    <result property="string" column="string" />
    <association property="embedded" javaType="sample.mybatis.Embedded">
      <result property="number" column="number" />
    </association>
  </resultMap>

  <select id="selectTest" resultMap="testTableResultMap">
    select * from test_table
  </select>
</mapper>

Main.java

package sample.mybatis;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Main {

    public static void main(String[] args) throws Exception {
        try (InputStream in = Main.class.getResourceAsStream("/mybatis-config.xml")) {
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

            try (SqlSession session = factory.openSession()) {
                session
                    .selectList("sample.mybatis.selectTest")
                    .forEach(System.out::println);
            }
        }
    }
}

Execution result

TestTable [id=1, string=hoge, embedded=Embedded [number=100]]
TestTable [id=2, string=fuga, embedded=Embedded [number=200]]
TestTable [id=3, string=piyo, embedded=Embedded [number=300]]

Explanation

  • You can map an embedded class with the <association> tag.
  • Specify the included class in the javaType attribute. Aliases can also be used.
  • Define the mapping for the included class in the child elements.

Defining the included class separately

sample_mapper.xml

<?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="sample.mybatis">

  <resultMap type="sample.mybatis.TestTable" id="testTableResultMap">
    <id property="id" column="id" />
    <result property="string" column="string" />

    <association property="embedded" resultMap="embeddedResultMap" />
  </resultMap>

  <resultMap type="sample.mybatis.Embedded" id="embeddedResultMap">
    <result property="number" column="number" />
  </resultMap>

  <select id="selectTest" resultMap="testTableResultMap">
    select * from test_table
  </select>
</mapper>
  • You can define it separately with <resultMap>.