The Mapper XML files have only a few first class elements (in the order that they should be defined):
cache
– Configuration of the cache for a given namespace.cache-ref
– Reference to a cache configuration from another namespace.resultMap
– The most complicated and powerful element that describes how to load your objects from the database result sets.parameterMap
– Deprecated! Old-school way to map parameters. Inline parameters are preferred and this element may be removed in the future. Not documented here.sql
– A reusable chunk of SQL that can be referenced by other statements.insert
– A mapped INSERT statement.update
– A mapped UPDATE statement.delete
– A mapped DELETE statement.select
– A mapped SELECT statement.The next sections will describe each of these elements in detail, starting with the statements themselves.
<select id="findPersonById" parameterType="int" resultType="hashmap">
SELECT * FROM PERSON WHERE ID = #{id}
select>
This tells MyBatis to create a PreparedStatement parameter. With JDBC, such a parameter would be identified by a “?” in SQL passed to a new PreparedStatement, something like this:
// Similar JDBC code, Not Mybatis...
String findPersonById = "SELECT * FROM PERSON WHERE ID=?";
PreparedStatement ps = conn.preparedStatement(findPersonById);
ps.setInt(1, id);
The ‘select’ element has more attributes that allow you to configure the details of how each statement should behave.
<select
id="selectPerson"
parameterType="int"
parameterMap="deprecated"
resultType="hashmap"
resultMap="personResultMap"
flushCache="false"
timeout="10"
statementType="PREPARED"
resultSetType="FORWARD_ONLY">
Select Attributes
Attribute | Description |
---|---|
id | |
resultType | The fully qualified class name or alias for the expected type that will be returned from this statement. Note that in the case of collections, this should be the type that the collection contains, not the type of the collection itself. Use resultType OR resultMap , not both. |
resultMap | A named reference to an external resultMap . Result maps are the most powerful feature of MyBatis. Use resultMap OR resultType , not both. |
resultType
Generally only can be Primary Type, java.lang.Long
etc.
The expected type that will be returned from this statement.
resultMap
Generally the Reference Type of your POJO.
A named reference to an external resultMap
. Result maps are the most powerful feature of MyBatis. Use resultMap
OR resultType
, not both.
<resultMap type="com.erato.multi.conditions.query.entity.Brand" id="BrandMap">
<result property="brandId" column="brand_id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="logo" column="logo" jdbcType="VARCHAR"/>
<result property="description" column="description" jdbcType="VARCHAR"/>
resultMap>
<select id="queryById" resultMap="BrandMap">
select brand_id,
name,
logo,
description
from pms_brand
where brand_id = #{brandId}
select>
The resultMap
element is the most important and powerful element in MyBatis.
A HashMap
doesn’t make a very good domain model. It’s more likely that your application will use JavaBeans or POJOs (Plain Old Java Objects) for the domain model. MyBatis supports both. Consider the following JavaBean:
package com.someapp.model;
public class User {
private int id;
private String username;
private String hashedPassword;
}
Such a JavaBean could be mapped to a ResultSet
(/Do you mean ‘resultType’???/)just as easily as the HashMap
.
<select id="findUserById" resultType="com.someapp.model.User">
select id, username, hashedPassword
from some_table
where id = #{id}
select>
And remember that TypeAliases are your friends.
In these cases MyBatis is automatically creating a ResultMap
behind the scenes.
To solve column name mismatches— an external resultMap
,
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="username" column="user_name"/>
<result property="password" column="hashed_password"/>
resultMap>
And the statement that references it uses the resultMap
attribute to do so (notice we removed the resultType
attribute). For example:
<select id="selectUsers" resultMap="userResultMap">
select user_id, user_name, hashed_password
from some_table
where id = #{id}
select>
Now if only the world was always that simple.
Databases aren’t always what you want or need them to be. While we’d love every database to be perfect 3rd normal form or BCNF, they aren’t.
You understand how painful it is to conditionally concatenate strings of SQL together, making sure not to forget spaces or to omit a comma at the end of a list of columns.