MyBatis | Mapping Query Results to Arbitrary Java Objects | Defining Mappings with a Class
Code
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 id="testTableResultMap" type="sample.mybatis.TestTable">
<id property="id" column="id" />
<result property="value" column="value" />
</resultMap>
<select id="selectTest" resultMap="testTableResultMap">
select * from test_table
</select>
</mapper>
Explanation
- Use the
<resultMap>tag to define the mapping between query results and a Java class. - Define identifier properties with the
<id>tag.- An identifier property is a property used to identify an instance.
- Specifying identifier properties appears to improve performance for caching and JOIN mappings.
- Specify the Java-side property or field name in the property attribute.
- Specify the database-side column name in the column attribute.
- Define each column mapping with the
<result>tag.- Here as well, map the property name and column name.
- Define a name that uniquely identifies the mapping with the id attribute of
<resultMap>(testTableResultMap).- Specify this name in the resultMap attribute of the
<select>tag.
- Specify this name in the resultMap attribute of the