How do I create MyBatis mapper?

In this examples you will see how to create or define a data mapper using the MyBatis. The mapper will communicate our application with the underlying databases where the data is stored. MyBatis data mapper is defined as an interface object. We can either use annotations or the xml mapper to define our database query.

In the first steps we will create a domain object, a simple pojo to store our data in the object world. The attributes / fields of our pojo resemble the structure of our records table in the database.

package org.kodejava.example.mybatis.domain;

import java.util.Date;

public class Record implements Serializable {
    private Integer id;
    private String title;
    private Date releaseDate;
    private Integer artistId;
    private Integer labelId;
    private Date created;
    private Date modified;

    public Record() {

    // Define the getters and setters for our pojo.
    // ...
    // ...
    // ...

    public String toString() {
        return "Record{" +
                "id=" + id +
                ", title='" + title + "'" +
                ", releaseDate=" + releaseDate +
                ", artistId=" + artistId +
                ", labelId=" + labelId +

Next we define the mapper interface code, we’ll create a file that contains a method to get data from the table. At this time the interface will be as the following:

package org.kodejava.example.mybatis.persistence;

import org.kodejava.example.mybatis.domain.Record;

public interface RecordMapper {
     * Get a single Record from the database based on the record
     * identified.
     * @param id record identifier.
     * @return a record object.
    Record getRecord(int id);

After create the interface we create a RecordMapper.xml file that defines the queries used by our mapper. Here is how it looks like:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-// Mapper 3.0//EN"
<mapper namespace="org.kodejava.example.mybatis.persistence.RecordMapper">

    <resultMap id="recordResultMap" type="record">
        <result column="release_date" property="releaseDate"/>
        <result column="artist_id" property="artistId"/>
        <result column="label_id" property="labelId"/>

    <select id="getRecord" parameterType="int" resultMap="recordResultMap">
        FROM records
        WHERE id = #{id}

To tell MyBatis about our mapper we need to define the mapper inside MyBatis configuration file. We register the mapper inside the <mappers> element of the configuration file.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-// Config 3.0//EN"
        <typeAlias alias="record" type="org.kodejava.example.mybatis.domain.Record"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost/mediadb"/>
                <property name="username" value="root"/>
                <property name="password" value="welcome"/>
        <mapper resource="org/kodejava/example/mybatis/persistence/RecordMapper.xml"/>

Finally, we create a simple application to use the data mapper to get record data from the database.

package org.kodejava.example.mybatis;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.kodejava.example.mybatis.domain.Record;
import org.kodejava.example.mybatis.persistence.RecordMapper;


public class MediaClient {
    public static void main(String[] args) throws Exception {
        String resource = "org/kodejava/example/mybatis/Configuration.xml";
        Reader reader = Resources.getResourceAsReader(resource);

        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory =;

        SqlSession session = factory.openSession();
        try {
            RecordMapper mapper = session.getMapper(RecordMapper.class);
            Record record = mapper.getRecord(1);
            System.out.println("Record = " + record);
        } finally {

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply