SqlSession

As mentioned above, the SqlSession instance is the most powerful class in MyBatis. It is where you’ll find all of the methods to execute statements, commit or rollback transactions and acquire mapper instances.

There are over twenty methods on the SqlSession class, so let’s break them up into more digestible groupings.

Statement Execution Methods

These methods are used to execute SELECT, INSERT, UPDATE and DELETE statements that are defined in your SQL Mapping XML files. They are pretty self explanatory, each takes the ID of the statement and the Parameter Object, which can be a primitive (auto-boxed or wrapper), a JavaBean, a POJO or a Map.

  1. <T> T selectOne(String statement, Object parameter)
  2. <E> List<E> selectList(String statement, Object parameter)
  3. <T> Cursor<T> selectCursor(String statement, Object parameter)
  4. <K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey)
  5. int insert(String statement, Object parameter)
  6. int update(String statement, Object parameter)
  7. int delete(String statement, Object parameter)

The difference between selectOne and selectList is only in that selectOne must return exactly one object or null (none). If any more than one, an exception will be thrown. If you don’t know how many objects are expected, use selectList. If you want to check for the existence of an object, you’re better off returning a count (0 or 1). The selectMap is a special case in that it is designed to convert a list of results into a Map based on one of the properties in the resulting objects. Because not all statements require a parameter, these methods are overloaded with versions that do not require the parameter object.

The value returned by the insert, update and delete methods indicate the number of rows affected by the statement.

  1. <T> T selectOne(String statement)
  2. <E> List<E> selectList(String statement)
  3. <T> Cursor<T> selectCursor(String statement)
  4. <K,V> Map<K,V> selectMap(String statement, String mapKey)
  5. int insert(String statement)
  6. int update(String statement)
  7. int delete(String statement)

A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.

  1. try (Cursor<MyEntity> entities = session.selectCursor(statement, param)) {
  2. for (MyEntity entity : entities) {
  3. // process one entity
  4. }
  5. }

Finally, there are three advanced versions of the select methods that allow you to restrict the range of rows to return, or provide custom result handling logic, usually for very large data sets.

  1. <E> List<E> selectList (String statement, Object parameter, RowBounds rowBounds)
  2. <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds)
  3. <K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowbounds)
  4. void select (String statement, Object parameter, ResultHandler<T> handler)
  5. void select (String statement, Object parameter, RowBounds rowBounds, ResultHandler<T> handler)

The RowBounds parameter causes MyBatis to skip the number of records specified, as well as limit the number of results returned to some number. The RowBounds class has a constructor to take both the offset and limit, and is otherwise immutable.

  1. int offset = 100;
  2. int limit = 25;
  3. RowBounds rowBounds = new RowBounds(offset, limit);

Different drivers are able to achieve different levels of efficiency in this regard. For the best performance, use result set types of SCROLL_SENSITIVE or SCROLL_INSENSITIVE (in other words: not FORWARD_ONLY).

The ResultHandler parameter allows you to handle each row however you like. You can add it to a List, create a Map, Set, or throw each result away and instead keep only rolled up totals of calculations. You can do pretty much anything with the ResultHandler, and it’s what MyBatis uses internally itself to build result set lists.

Since 3.4.6, ResultHandler passed to a CALLABLE statement is used on every REFCURSOR output parameter of the stored procedure if there is any.

The interface is very simple.

  1. package org.apache.ibatis.session;
  2. public interface ResultHandler<T> {
  3. void handleResult(ResultContext<? extends T> context);
  4. }

The ResultContext parameter gives you access to the result object itself, a count of the number of result objects created, and a Boolean stop() method that you can use to stop MyBatis from loading any more results.

Using a ResultHandler has two limitations that you should be aware of:

  • Data gotten from a method called with a ResultHandler will not be cached.
  • When using advanced resultMaps MyBatis will probably require several rows to build an object. If a ResultHandler is used you may be given an object whose associations or collections are not yet filled.
Batch update statement Flush Method

There is method for flushing (executing) batch update statements that are stored in a JDBC driver class at any time. This method can be used when the ExecutorType is ExecutorType.BATCH.

  1. List<BatchResult> flushStatements()
Transaction Control Methods

There are four methods for controlling the scope of a transaction. Of course, these have no effect if you’ve chosen to use auto-commit or if you’re using an external transaction manager. However, if you’re using the JDBC transaction manager, managed by the Connection instance, then the four methods that will come in handy are:

  1. void commit()
  2. void commit(boolean force)
  3. void rollback()
  4. void rollback(boolean force)

By default MyBatis does not actually commit unless it detects that the database has been changed by a call to insert, update, delete or select with affectData enabled. If you’ve somehow made changes without calling these methods, then you can pass true into the commit and rollback methods to guarantee that they will be committed (note, you still can’t force a session in auto-commit mode, or one that is using an external transaction manager). Most of the time you won’t have to call rollback(), as MyBatis will do that for you if you don’t call commit. However, if you need more fine-grained control over a session where multiple commits and rollbacks are possible, you have the rollback option there to make that possible.

NOTE MyBatis-Spring and MyBatis-Guice provide declarative transaction handling. So if you are using MyBatis with Spring or Guice please refer to their specific manuals.

Local Cache

MyBatis uses two caches: a local cache and a second level cache.

Each time a new session is created MyBatis creates a local cache and attaches it to the session. Any query executed within the session will be stored in the local cache so further executions of the same query with the same input parameters will not hit the database. The local cache is cleared upon update, commit, rollback and close.

By default local cache data is used for the whole session duration. This cache is needed to resolve circular references and to speed up repeated nested queries, so it can never be completely disabled but you can configure the local cache to be used just for the duration of a statement execution by setting localCacheScope=STATEMENT.

Note that when the localCacheScope is set to SESSION, MyBatis returns references to the same objects which are stored in the local cache. Any modification of the returned objects (lists etc.) influences the local cache contents and subsequently the values which are returned from the cache in the lifetime of the session. Therefore, as best practice, do not to modify the objects returned by MyBatis.

You can clear the local cache at any time by calling:

  1. void clearCache()
Ensuring that SqlSession is Closed
  1. void close()

The most important thing you must ensure is to close any session that you open. The best way to ensure this is to use the following unit of work pattern:

  1. try (SqlSession session = sqlSessionFactory.openSession()) {
  2. // following 3 lines are pseudocode for "doing some work"
  3. session.insert(...);
  4. session.update(...);
  5. session.delete(...);
  6. session.commit();
  7. }

NOTE Just like SqlSessionFactory, you can get the instance of Configuration that the SqlSession is using by calling the getConfiguration() method.

  1. Configuration getConfiguration()
Using Mappers
  1. <T> T getMapper(Class<T> type)

While the various insert, update, delete and select methods above are powerful, they are also very verbose, not type safe and not as helpful to your IDE or unit tests as they could be. We’ve already seen an example of using Mappers in the Getting Started section above.

Therefore, a more common way to execute mapped statements is to use Mapper classes. A Mapper class is simply an interface with method definitions that match up against the SqlSession methods. The following example class demonstrates some method signatures and how they map to the SqlSession.

  1. public interface AuthorMapper {
  2. // (Author) selectOne("selectAuthor", 5);
  3. Author selectAuthor(int id);
  4. // (List<Author>) selectList("selectAuthors")
  5. List<Author> selectAuthors();
  6. // (Map<Integer,Author>) selectMap("selectAuthors", "id")
  7. @MapKey("id")
  8. Map<Integer, Author> selectAuthors();
  9. // insert("insertAuthor", author)
  10. int insertAuthor(Author author);
  11. // updateAuthor("updateAuthor", author)
  12. int updateAuthor(Author author);
  13. // delete("deleteAuthor", 5)
  14. int deleteAuthor(int id);
  15. }

In a nutshell, each Mapper method signature should match that of the SqlSession method that it’s associated to, but without the String parameter ID. Instead, the method name must match the mapped statement ID.

In addition, the return type must match that of the expected result type for single results or an array or collection for multiple results or Cursor. All of the usual types are supported, including: Primitives, Maps, POJOs and JavaBeans.

NOTE Mapper interfaces do not need to implement any interface or extend any class. As long as the method signature can be used to uniquely identify a corresponding mapped statement.

NOTE Mapper interfaces can extend other interfaces. Be sure that you have the statements in the appropriate namespace when using XML binding to Mapper interfaces. Also, the only limitation is that you cannot have the same method signature in two interfaces in a hierarchy (a bad idea anyway).

You can pass multiple parameters to a mapper method. If you do, they will be named by the literal “param” followed by their position in the parameter list by default, for example: #{param1}, #{param2} etc. If you wish to change the name of the parameters (multiple only), then you can use the @Param("paramName") annotation on the parameter.

You can also pass a RowBounds instance to the method to limit query results.

Mapper Annotations

Since the very beginning, MyBatis has been an XML driven framework. The configuration is XML based, and the Mapped Statements are defined in XML. With MyBatis 3, there are new options available. MyBatis 3 builds on top of a comprehensive and powerful Java based Configuration API. This Configuration API is the foundation for the XML based MyBatis configuration, as well as the new annotation-based configuration. Annotations offer a simple way to implement simple mapped statements without introducing a lot of overhead.

NOTE Java annotations are unfortunately limited in their expressiveness and flexibility. Despite a lot of time spent in investigation, design and trials, the most powerful MyBatis mappings simply cannot be built with annotations – without getting ridiculous that is. C# Attributes (for example) do not suffer from these limitations, and thus MyBatis.NET will enjoy a much richer alternative to XML. That said, the Java annotation-based configuration is not without its benefits.

The annotations are as follows:

AnnotationTargetXML equivalentDescription
@CacheNamespaceClass<cache>Configures the cache for the given namespace (i.e. class). Attributes: implementation, eviction, flushInterval, size, readWrite, blocking, properties.
@PropertyN/A<property>Specifies the property value or placeholder(can replace by configuration properties that defined at the mybatis-config.xml). Attributes: name, value. (Available on MyBatis 3.4.2+)
@CacheNamespaceRefClass<cacheRef>References the cache of another namespace to use. Note that caches declared in an XML mapper file are considered a separate namespace, even if they share the same FQCN. Attributes: value and name. If you use this annotation, you should be specified either value or name attribute. For the value attribute specify a java type indicating the namespace(the namespace name become a FQCN of specified java type), and for the name attribute(this attribute is available since 3.4.2) specify a name indicating the namespace.
@ConstructorArgsMethod<constructor>Collects a group of results to be passed to a result object constructor. Attributes: value, which is an array of Args.
@ArgN/A
  • <arg>
  • <idArg>
A single constructor argument that is part of a ConstructorArgs collection. Attributes: id, column, javaType, jdbcType, typeHandler, select, resultMap. The id attribute is a boolean value that identifies the property to be used for comparisons, similar to the <idArg> XML element. Since 3.5.4, it can be used as repeatable annotation.
@TypeDiscriminatorMethod<discriminator>A group of value cases that can be used to determine the result mapping to perform. Attributes: column, javaType, jdbcType, typeHandler, cases. The cases attribute is an array of Cases.
@CaseN/A<case>A single case of a value and its corresponding mappings. Attributes: value, type, results. The results attribute is an array of Results, thus this Case Annotation is similar to an actual ResultMap, specified by the Results annotation below.
@ResultsMethod<resultMap>A list of Result mappings that contain details of how a particular result column is mapped to a property or field. Attributes: value, id. The value attribute is an array of Result annotations. The id attribute is the name of the result mapping.
@ResultN/A
  • <result>
  • <id>
A single result mapping between a column and a property or field. Attributes: id, column, property, javaType, jdbcType, typeHandler, one, many. The id attribute is a boolean value that indicates that the property should be used for comparisons (similar to <id> in the XML mappings). The one attribute is for single associations, similar to <association>, and the many attribute is for collections, similar to <collection>. They are named as they are to avoid class naming conflicts. Since 3.5.4, it can be used as repeatable annotation.
@OneN/A<association>A mapping to a single property value of a complex type. Attributes: select, which is the fully qualified name of a mapped statement (i.e. mapper method) that can load an instance of the appropriate type. fetchType, which supersedes the global configuration parameter lazyLoadingEnabled for this mapping. resultMap(available since 3.5.5), which is the fully qualified name of a result map that map to a single container object from select result. columnPrefix(available since 3.5.5), which is column prefix for grouping select columns at nested result map. NOTE You will notice that join mapping is not supported via the Annotations API. This is due to the limitation in Java Annotations that does not allow for circular references.
@ManyN/A<collection>A mapping to a collection property of a complex type. Attributes: select, which is the fully qualified name of a mapped statement (i.e. mapper method) that can load a collection of instances of the appropriate types. fetchType, which supersedes the global configuration parameter lazyLoadingEnabled for this mapping. resultMap(available since 3.5.5), which is the fully qualified name of a result map that map to collection object from select result. columnPrefix(available since 3.5.5), which is column prefix for grouping select columns at nested result map. NOTE You will notice that join mapping is not supported via the Annotations API. This is due to the limitation in Java Annotations that does not allow for circular references.
@MapKeyMethodThis is used on methods which return type is a Map. It is used to convert a List of result objects as a Map based on a property of those objects. Attributes: value, which is a property used as the key of the map.
@OptionsMethodAttributes of mapped statements.This annotation provides access to the wide range of switches and configuration options that are normally present on the mapped statement as attributes. Rather than complicate each statement annotation, the Options annotation provides a consistent and clear way to access these. Attributes: useCache=true, flushCache=FlushCachePolicy.DEFAULT, resultSetType=DEFAULT, statementType=PREPARED, fetchSize=-1, timeout=-1, useGeneratedKeys=false, keyProperty=””, keyColumn=””, resultSets=”” and databaseId=””. It’s important to understand that with Java Annotations, there is no way to specify null as a value. Therefore, once you engage the Options annotation, your statement is subject to all of the default values. Pay attention to what the default values are to avoid unexpected behavior. The databaseId(Available since 3.5.5), in case there is a configured DatabaseIdProvider, the MyBatis use the Options with no databaseId attribute or with a databaseId that matches the current one. If found with and without the databaseId the latter will be discarded.

Note that keyColumn is only required in certain databases (like Oracle and PostgreSQL). See the discussion about keyColumn and keyProperty above in the discussion of the insert statement for more information about allowable values in these attributes.
  • @Insert
  • @Update
  • @Delete
  • @Select
Method
  • <insert>
  • <update>
  • <delete>
  • <select>
Each of these annotations represents the actual SQL that is to be executed. They each take an array of strings (or a single string will do). If an array of strings is passed, they are concatenated with a single space between each to separate them. This helps avoid the “missing space” problem when building SQL in Java code. However, you’re also welcome to concatenate together a single string if you like. Attributes: value, which is the array of Strings to form the single SQL statement. The databaseId(Available since 3.5.5), in case there is a configured DatabaseIdProvider, the MyBatis use a statement with no databaseId attribute or with a databaseId that matches the current one. If found with and without the databaseId the latter will be discarded.
  • @InsertProvider
  • @UpdateProvider
  • @DeleteProvider
  • @SelectProvider
Method
  • <insert>
  • <update>
  • <delete>
  • <select>
Allows for creation of dynamic SQL. These alternative SQL annotations allow you to specify a class and a method name that will return the SQL to run at execution time (Since 3.4.6, you can specify the CharSequence instead of String as a method return type). Upon executing the mapped statement, MyBatis will instantiate the class, and execute the method, as specified by the provider. You can pass objects that passed to arguments of a mapper method, “Mapper interface type”, “Mapper method” and “Database ID” via the ProviderContext(available since MyBatis 3.4.5 or later) as method argument. (In MyBatis 3.4 or later, it’s allow multiple parameters) Attributes: value, type, method and databaseId. The value and type attribute is a class (The type attribute is alias for value, you must be specify either one. But both attributes can be omit when specify the defaultSqlProviderType as global configuration). The method is the name of the method on that class (Since 3.5.1, you can omit method attribute, the MyBatis will resolve a target method via the ProviderMethodResolver interface. If not resolve by it, the MyBatis use the reserved fallback method that named provideSql). The databaseId(Available since 3.5.5), in case there is a configured DatabaseIdProvider, the MyBatis will use a provider method with no databaseId attribute or with a databaseId that matches the current one. If found with and without the databaseId the latter will be discarded. NOTE Following this section is a discussion about the class, which can help build dynamic SQL in a cleaner, easier to read way.
@ParamParameterN/AIf your mapper method takes multiple parameters, this annotation can be applied to a mapper method parameter to give each of them a name. Otherwise, multiple parameters will be named by their position prefixed with “param” (not including any RowBounds parameters). For example #{param1}, #{param2} etc. is the default. With @Param(“person”), the parameter would be named #{person}.
@SelectKeyMethod<selectKey>This annotation duplicates the <selectKey> functionality for methods annotated with @Insert, @InsertProvider, @Update, or @UpdateProvider. It is ignored for other methods. If you specify a @SelectKey annotation, then MyBatis will ignore any generated key properties set via the @Options annotation, or configuration properties. Attributes: statement an array of strings which is the SQL statement to execute, keyProperty which is the property of the parameter object that will be updated with the new value, before which must be either true or false to denote if the SQL statement should be executed before or after the insert, resultType which is the Java type of the keyProperty, and statementType is a type of the statement that is any one of STATEMENT, PREPARED or CALLABLE that is mapped to Statement, PreparedStatement and CallableStatement respectively. The default is PREPARED. The databaseId(Available since 3.5.5), in case there is a configured DatabaseIdProvider, the MyBatis will use a statement with no databaseId attribute or with a databaseId that matches the current one. If found with and without the databaseId the latter will be discarded.
@ResultMapMethodN/AThis annotation is used to provide the id of a <resultMap> element in an XML mapper to a @Select or @SelectProvider annotation. This allows annotated selects to reuse resultmaps that are defined in XML. This annotation will override any @Results or @ConstructorArgs annotation if both are specified on an annotated select.
@ResultTypeMethodN/AThis annotation is used when using a result handler. In that case, the return type is void so MyBatis must have a way to determine the type of object to construct for each row. If there is an XML result map, use the @ResultMap annotation. If the result type is specified in XML on the <select> element, then no other annotation is necessary. In other cases, use this annotation. For example, if a @Select annotated method will use a result handler, the return type must be void and this annotation (or @ResultMap) is required. This annotation is ignored unless the method return type is void.
@FlushMethodN/AIf this annotation is used, it can be called the SqlSession#flushStatements() via method defined at a Mapper interface.(MyBatis 3.3 or above)
Mapper Annotation Examples

This example shows using the @SelectKey annotation to retrieve a value from a sequence before an insert:

  1. @Insert("insert into table3 (id, name) values(#{nameId}, #{name})")
  2. @SelectKey(statement="call next value for TestSequence", keyProperty="nameId", before=true, resultType=int.class)
  3. int insertTable3(Name name);

This example shows using the @SelectKey annotation to retrieve an identity value after an insert:

  1. @Insert("insert into table2 (name) values(#{name})")
  2. @SelectKey(statement="call identity()", keyProperty="nameId", before=false, resultType=int.class)
  3. int insertTable2(Name name);

This example shows using the @Flush annotation to call the SqlSession#flushStatements():

  1. @Flush
  2. List<BatchResult> flush();

These examples show how to name a ResultMap by specifying id attribute of @Results annotation.

  1. @Results(id = "userResult", value = {
  2. @Result(property = "id", column = "uid", id = true),
  3. @Result(property = "firstName", column = "first_name"),
  4. @Result(property = "lastName", column = "last_name")
  5. })
  6. @Select("select * from users where id = #{id}")
  7. User getUserById(Integer id);
  8. @Results(id = "companyResults")
  9. @ConstructorArgs({
  10. @Arg(column = "cid", javaType = Integer.class, id = true),
  11. @Arg(column = "name", javaType = String.class)
  12. })
  13. @Select("select * from company where id = #{id}")
  14. Company getCompanyById(Integer id);

This example shows solo parameter using the SelectProvider annotation:

  1. @SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
  2. List<User> getUsersByName(String name);
  3. class UserSqlBuilder {
  4. public static String buildGetUsersByName(final String name) {
  5. return new SQL(){{
  6. SELECT("*");
  7. FROM("users");
  8. if (name != null) {
  9. WHERE("name like #{value} || '%'");
  10. }
  11. ORDER_BY("id");
  12. }}.toString();
  13. }
  14. }

This example shows multiple parameters using the Sql Provider annotation:

  1. @SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
  2. List<User> getUsersByName(
  3. @Param("name") String name, @Param("orderByColumn") String orderByColumn);
  4. class UserSqlBuilder {
  5. // If not use @Param, you should be define same arguments with mapper method
  6. public static String buildGetUsersByName(
  7. final String name, final String orderByColumn) {
  8. return new SQL(){{
  9. SELECT("*");
  10. FROM("users");
  11. WHERE("name like #{name} || '%'");
  12. ORDER_BY(orderByColumn);
  13. }}.toString();
  14. }
  15. // If use @Param, you can define only arguments to be used
  16. public static String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) {
  17. return new SQL(){{
  18. SELECT("*");
  19. FROM("users");
  20. WHERE("name like #{name} || '%'");
  21. ORDER_BY(orderByColumn);
  22. }}.toString();
  23. }
  24. }

This example shows usage that share an sql provider class to all mapper methods using global configuration(Available since 3.5.6):

  1. Configuration configuration = new Configuration();
  2. configuration.setDefaultSqlProviderType(TemplateFilePathProvider.class); // Specify an sql provider class for sharing on all mapper methods
  3. // ...
  1. // Can omit the type/value attribute on sql provider annotation
  2. // If omit it, the MyBatis apply the class that specified on defaultSqlProviderType.
  3. public interface UserMapper {
  4. @SelectProvider // Same with @SelectProvider(TemplateFilePathProvider.class)
  5. User findUser(int id);
  6. @InsertProvider // Same with @InsertProvider(TemplateFilePathProvider.class)
  7. void createUser(User user);
  8. @UpdateProvider // Same with @UpdateProvider(TemplateFilePathProvider.class)
  9. void updateUser(User user);
  10. @DeleteProvider // Same with @DeleteProvider(TemplateFilePathProvider.class)
  11. void deleteUser(int id);
  12. }

This example shows usage the default implementation of ProviderMethodResolver(available since MyBatis 3.5.1 or later):

  1. @SelectProvider(UserSqlProvider.class)
  2. List<User> getUsersByName(String name);
  3. // Implements the ProviderMethodResolver on your provider class
  4. class UserSqlProvider implements ProviderMethodResolver {
  5. // In default implementation, it will resolve a method that method name is matched with mapper method
  6. public static String getUsersByName(final String name) {
  7. return new SQL(){{
  8. SELECT("*");
  9. FROM("users");
  10. if (name != null) {
  11. WHERE("name like #{value} || '%'");
  12. }
  13. ORDER_BY("id");
  14. }}.toString();
  15. }
  16. }

This example shows usage the databaseId attribute on the statement annotation(Available since 3.5.5):

  1. @Select(value = "SELECT SYS_GUID() FROM dual", databaseId = "oracle") // Use this statement if DatabaseIdProvider provide "oracle"
  2. @Select(value = "SELECT uuid_generate_v4()", databaseId = "postgres") // Use this statement if DatabaseIdProvider provide "postgres"
  3. @Select("SELECT RANDOM_UUID()") // Use this statement if the DatabaseIdProvider not configured or not matches databaseId
  4. String generateId();