Package org.apache.ibatis.session
Interface SqlSession
-
- All Superinterfaces:
java.lang.AutoCloseable,java.io.Closeable
- All Known Implementing Classes:
DefaultSqlSession,SqlSessionManager
public interface SqlSession extends java.io.CloseableThe primary Java interface for working with MyBatis. Through this interface you can execute commands, get mappers and manage transactions.- Author:
- Clinton Begin
-
-
Method Summary
Modifier and Type Method Description voidclearCache()Clears local session cache.voidclose()Closes the session.voidcommit()Flushes batch statements and commits database connection.voidcommit(boolean force)Flushes batch statements and commits database connection.intdelete(java.lang.String statement)Execute a delete statement.intdelete(java.lang.String statement, java.lang.Object parameter)Execute a delete statement.java.util.List<BatchResult>flushStatements()Flushes batch statements.ConfigurationgetConfiguration()Retrieves current configuration.java.sql.ConnectiongetConnection()Retrieves inner database connection.<T> TgetMapper(java.lang.Class<T> type)Retrieves a mapper.intinsert(java.lang.String statement)Execute an insert statement.intinsert(java.lang.String statement, java.lang.Object parameter)Execute an insert statement with the given parameter object.voidrollback()Discards pending batch statements and rolls database connection back.voidrollback(boolean force)Discards pending batch statements and rolls database connection back.voidselect(java.lang.String statement, java.lang.Object parameter, ResultHandler handler)Retrieve a single row mapped from the statement key and parameter using aResultHandler.voidselect(java.lang.String statement, java.lang.Object parameter, RowBounds rowBounds, ResultHandler handler)Retrieve a single row mapped from the statement key and parameter using aResultHandlerandRowBounds.voidselect(java.lang.String statement, ResultHandler handler)Retrieve a single row mapped from the statement using aResultHandler.<T> Cursor<T>selectCursor(java.lang.String statement)A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.<T> Cursor<T>selectCursor(java.lang.String statement, java.lang.Object parameter)A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.<T> Cursor<T>selectCursor(java.lang.String statement, java.lang.Object parameter, RowBounds rowBounds)A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.<E> java.util.List<E>selectList(java.lang.String statement)Retrieve a list of mapped objects from the statement key and parameter.<E> java.util.List<E>selectList(java.lang.String statement, java.lang.Object parameter)Retrieve a list of mapped objects from the statement key and parameter.<E> java.util.List<E>selectList(java.lang.String statement, java.lang.Object parameter, RowBounds rowBounds)Retrieve a list of mapped objects from the statement key and parameter, within the specified row bounds.<K,V>
java.util.Map<K,V>selectMap(java.lang.String statement, java.lang.Object parameter, java.lang.String mapKey)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.<K,V>
java.util.Map<K,V>selectMap(java.lang.String statement, java.lang.Object parameter, java.lang.String mapKey, RowBounds rowBounds)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.<K,V>
java.util.Map<K,V>selectMap(java.lang.String statement, java.lang.String mapKey)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.<T> TselectOne(java.lang.String statement)Retrieve a single row mapped from the statement key.<T> TselectOne(java.lang.String statement, java.lang.Object parameter)Retrieve a single row mapped from the statement key and parameter.intupdate(java.lang.String statement)Execute an update statement.intupdate(java.lang.String statement, java.lang.Object parameter)Execute an update statement.
-
-
-
Method Detail
-
selectOne
<T> T selectOne(java.lang.String statement)
Retrieve a single row mapped from the statement key.- Type Parameters:
T- the returned object type- Parameters:
statement-- Returns:
- Mapped object
-
selectOne
<T> T selectOne(java.lang.String statement, java.lang.Object parameter)Retrieve a single row mapped from the statement key and parameter.- Type Parameters:
T- the returned object type- Parameters:
statement- Unique identifier matching the statement to use.parameter- A parameter object to pass to the statement.- Returns:
- Mapped object
-
selectList
<E> java.util.List<E> selectList(java.lang.String statement)
Retrieve a list of mapped objects from the statement key and parameter.- Type Parameters:
E- the returned list element type- Parameters:
statement- Unique identifier matching the statement to use.- Returns:
- List of mapped object
-
selectList
<E> java.util.List<E> selectList(java.lang.String statement, java.lang.Object parameter)Retrieve a list of mapped objects from the statement key and parameter.- Type Parameters:
E- the returned list element type- Parameters:
statement- Unique identifier matching the statement to use.parameter- A parameter object to pass to the statement.- Returns:
- List of mapped object
-
selectList
<E> java.util.List<E> selectList(java.lang.String statement, java.lang.Object parameter, RowBounds rowBounds)Retrieve a list of mapped objects from the statement key and parameter, within the specified row bounds.- Type Parameters:
E- the returned list element type- Parameters:
statement- Unique identifier matching the statement to use.parameter- A parameter object to pass to the statement.rowBounds- Bounds to limit object retrieval- Returns:
- List of mapped object
-
selectMap
<K,V> java.util.Map<K,V> selectMap(java.lang.String statement, java.lang.String mapKey)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. Eg. Return a of Map[Integer,Author] for selectMap("selectAuthors","id")- Type Parameters:
K- the returned Map keys typeV- the returned Map values type- Parameters:
statement- Unique identifier matching the statement to use.mapKey- The property to use as key for each value in the list.- Returns:
- Map containing key pair data.
-
selectMap
<K,V> java.util.Map<K,V> selectMap(java.lang.String statement, java.lang.Object parameter, java.lang.String mapKey)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.- Type Parameters:
K- the returned Map keys typeV- the returned Map values type- Parameters:
statement- Unique identifier matching the statement to use.parameter- A parameter object to pass to the statement.mapKey- The property to use as key for each value in the list.- Returns:
- Map containing key pair data.
-
selectMap
<K,V> java.util.Map<K,V> selectMap(java.lang.String statement, java.lang.Object parameter, java.lang.String mapKey, RowBounds rowBounds)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.- Type Parameters:
K- the returned Map keys typeV- the returned Map values type- Parameters:
statement- Unique identifier matching the statement to use.parameter- A parameter object to pass to the statement.mapKey- The property to use as key for each value in the list.rowBounds- Bounds to limit object retrieval- Returns:
- Map containing key pair data.
-
selectCursor
<T> Cursor<T> selectCursor(java.lang.String statement)
A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.- Type Parameters:
T- the returned cursor element type.- Parameters:
statement- Unique identifier matching the statement to use.- Returns:
- Cursor of mapped objects
-
selectCursor
<T> Cursor<T> selectCursor(java.lang.String statement, java.lang.Object parameter)
A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.- Type Parameters:
T- the returned cursor element type.- Parameters:
statement- Unique identifier matching the statement to use.parameter- A parameter object to pass to the statement.- Returns:
- Cursor of mapped objects
-
selectCursor
<T> Cursor<T> selectCursor(java.lang.String statement, java.lang.Object parameter, RowBounds rowBounds)
A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.- Type Parameters:
T- the returned cursor element type.- Parameters:
statement- Unique identifier matching the statement to use.parameter- A parameter object to pass to the statement.rowBounds- Bounds to limit object retrieval- Returns:
- Cursor of mapped objects
-
select
void select(java.lang.String statement, java.lang.Object parameter, ResultHandler handler)Retrieve a single row mapped from the statement key and parameter using aResultHandler.- Parameters:
statement- Unique identifier matching the statement to use.parameter- A parameter object to pass to the statement.handler- ResultHandler that will handle each retrieved row
-
select
void select(java.lang.String statement, ResultHandler handler)Retrieve a single row mapped from the statement using aResultHandler.- Parameters:
statement- Unique identifier matching the statement to use.handler- ResultHandler that will handle each retrieved row
-
select
void select(java.lang.String statement, java.lang.Object parameter, RowBounds rowBounds, ResultHandler handler)Retrieve a single row mapped from the statement key and parameter using aResultHandlerandRowBounds.- Parameters:
statement- Unique identifier matching the statement to use.rowBounds- RowBound instance to limit the query resultshandler- ResultHandler that will handle each retrieved row
-
insert
int insert(java.lang.String statement)
Execute an insert statement.- Parameters:
statement- Unique identifier matching the statement to execute.- Returns:
- int The number of rows affected by the insert.
-
insert
int insert(java.lang.String statement, java.lang.Object parameter)Execute an insert statement with the given parameter object. Any generated autoincrement values or selectKey entries will modify the given parameter object properties. Only the number of rows affected will be returned.- Parameters:
statement- Unique identifier matching the statement to execute.parameter- A parameter object to pass to the statement.- Returns:
- int The number of rows affected by the insert.
-
update
int update(java.lang.String statement)
Execute an update statement. The number of rows affected will be returned.- Parameters:
statement- Unique identifier matching the statement to execute.- Returns:
- int The number of rows affected by the update.
-
update
int update(java.lang.String statement, java.lang.Object parameter)Execute an update statement. The number of rows affected will be returned.- Parameters:
statement- Unique identifier matching the statement to execute.parameter- A parameter object to pass to the statement.- Returns:
- int The number of rows affected by the update.
-
delete
int delete(java.lang.String statement)
Execute a delete statement. The number of rows affected will be returned.- Parameters:
statement- Unique identifier matching the statement to execute.- Returns:
- int The number of rows affected by the delete.
-
delete
int delete(java.lang.String statement, java.lang.Object parameter)Execute a delete statement. The number of rows affected will be returned.- Parameters:
statement- Unique identifier matching the statement to execute.parameter- A parameter object to pass to the statement.- Returns:
- int The number of rows affected by the delete.
-
commit
void commit()
Flushes batch statements and commits database connection. Note that database connection will not be committed if no updates/deletes/inserts were called. To force the commit callcommit(boolean)
-
commit
void commit(boolean force)
Flushes batch statements and commits database connection.- Parameters:
force- forces connection commit
-
rollback
void rollback()
Discards pending batch statements and rolls database connection back. Note that database connection will not be rolled back if no updates/deletes/inserts were called. To force the rollback callrollback(boolean)
-
rollback
void rollback(boolean force)
Discards pending batch statements and rolls database connection back. Note that database connection will not be rolled back if no updates/deletes/inserts were called.- Parameters:
force- forces connection rollback
-
flushStatements
java.util.List<BatchResult> flushStatements()
Flushes batch statements.- Returns:
- BatchResult list of updated records
- Since:
- 3.0.6
-
close
void close()
Closes the session.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
clearCache
void clearCache()
Clears local session cache.
-
getConfiguration
Configuration getConfiguration()
Retrieves current configuration.- Returns:
- Configuration
-
getMapper
<T> T getMapper(java.lang.Class<T> type)
Retrieves a mapper.- Type Parameters:
T- the mapper type- Parameters:
type- Mapper interface class- Returns:
- a mapper bound to this SqlSession
-
getConnection
java.sql.Connection getConnection()
Retrieves inner database connection.- Returns:
- Connection
-
-