select

The select statement is one of the most popular elements that you’ll use in MyBatis. Putting data in a database isn’t terribly valuable until you get it back out, so most applications query far more than they modify the data. For every insert, update or delete, there are probably many selects. This is one of the founding principles of MyBatis, and is the reason so much focus and effort was placed on querying and result mapping. The select element is quite simple for simple cases. For example:

  1. <select id="selectPerson" parameterType="int" resultType="hashmap">
  2. SELECT * FROM PERSON WHERE ID = #{id}
  3. </select>

This statement is called selectPerson, takes a parameter of type int (or Integer), and returns a HashMap keyed by column names mapped to row values.

Notice the parameter notation:

  1. #{id}

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:

  1. // Similar JDBC code, NOT MyBatis…
  2. String selectPerson = "SELECT * FROM PERSON WHERE ID=?";
  3. PreparedStatement ps = conn.prepareStatement(selectPerson);
  4. ps.setInt(1,id);

Of course, there’s a lot more code required by JDBC alone to extract the results and map them to an instance of an object, which is what MyBatis saves you from having to do. There’s a lot more to know about parameter and result mapping. Those details warrant their own section, which follows later in this section.

The select element has more attributes that allow you to configure the details of how each statement should behave.

  1. <select
  2. id="selectPerson"
  3. parameterType="int"
  4. parameterMap="deprecated"
  5. resultType="hashmap"
  6. resultMap="personResultMap"
  7. flushCache="false"
  8. useCache="true"
  9. timeout="10"
  10. fetchSize="256"
  11. statementType="PREPARED"
  12. resultSetType="FORWARD_ONLY">
Select Attributes
AttributeDescription
idA unique identifier in this namespace that can be used to reference this statement.
parameterTypeThe fully qualified class name or alias for the parameter that will be passed into this statement. This attribute is optional because MyBatis can calculate the TypeHandler to use out of the actual parameter passed to the statement. Default is unset.
parameterMapThis is a deprecated approach to referencing an external parameterMap. Use inline parameter mappings and the parameterType attribute.
resultTypeThe 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.
resultMapA named reference to an external resultMap. Result maps are the most powerful feature of MyBatis, and with a good understanding of them, many difficult mapping cases can be solved. Use resultMap OR resultType, not both.
flushCacheSetting this to true will cause the local and 2nd level caches to be flushed whenever this statement is called. Default: false for select statements.
useCacheSetting this to true will cause the results of this statement to be cached in 2nd level cache. Default: true for select statements.
timeoutThis sets the number of seconds the driver will wait for the database to return from a request, before throwing an exception. Default is unset (driver dependent).
fetchSizeThis is a driver hint that will attempt to cause the driver to return results in batches of rows numbering in size equal to this setting. Default is unset (driver dependent).
statementTypeAny one of STATEMENT, PREPARED or CALLABLE. This causes MyBatis to use Statement, PreparedStatement or CallableStatement respectively. Default: PREPARED.
resultSetTypeAny one of FORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE|DEFAULT(same as unset). Default is unset (driver dependent).
databaseIdIn case there is a configured databaseIdProvider, MyBatis will load all statements with no databaseId attribute or with a databaseId that matches the current one. If case the same statement if found with and without the databaseId the latter will be discarded.
resultOrderedThis is only applicable for nested result select statements: If this is true, it is assumed that nested results are contained or grouped together such that when a new main result row is returned, no references to a previous result row will occur anymore. This allows nested results to be filled much more memory friendly. Default: false.
resultSetsThis is only applicable for multiple result sets. It lists the result sets that will be returned by the statement and gives a name to each one. Names are separated by commas.
affectDataSet this to true when writing a INSERT, UPDATE or DELETE statement that returns data so that the transaction is controlled properly. Also see Transaction Control Method. Default: false (since 3.5.12)