org.mybatis.spring
Class SqlSessionTemplate
java.lang.Object
org.springframework.jdbc.support.JdbcAccessor
org.mybatis.spring.SqlSessionTemplate
- All Implemented Interfaces:
- SqlSessionOperations, org.springframework.beans.factory.InitializingBean
public class SqlSessionTemplate
- extends org.springframework.jdbc.support.JdbcAccessor
- implements SqlSessionOperations
Helper class that simplifies data access via the MyBatis
SqlSession
API, converting checked SQLExceptions into unchecked
DataAccessExceptions, following the org.springframework.dao
exception hierarchy.
Uses the same SQLExceptionTranslator
mechanism as
org.springframework.jdbc.core.JdbcTemplate
.
The main method of this class executes a callback that implements a data access action.
Furthermore, this class provides numerous convenience methods that mirror
SqlSession
's execution methods.
It is generally recommended to use the convenience methods on this template for plain
query/insert/update/delete operations. However, for more complex operations like batch updates, a
custom SqlSessionCallback must be implemented, usually as anonymous inner class. For example:
getSqlSessionTemplate().execute(new SqlSessionCallback<Object>() {
public Object doInSqlSession(SqlSession sqlSession) throws SQLException {
sqlSession.getMapper(MyMapper.class).update(parameterObject);
sqlSession.update("MyMapper.update", otherParameterObject);
return null;
}
}, ExecutorType.BATCH);
The template needs a SqlSessionFactory to create SqlSessions, passed in via the
"sqlSessionFactory" property. A Spring context typically uses a SqlSessionFactoryBean
to
build the SqlSessionFactory. The template can additionally be configured with a DataSource for
fetching Connections, although this is not necessary since a DataSource is specified for the
SqlSessionFactory itself (through configured Environment).
- Version:
- $Id: SqlSessionTemplate.java 2441 2010-09-14 14:58:56Z simone.tripodi $
- See Also:
execute(org.mybatis.spring.SqlSessionCallback)
,
setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory)
,
SqlSessionFactoryBean.setDataSource(javax.sql.DataSource)
,
SqlSessionFactory.getConfiguration()
,
SqlSession
,
SqlSessionOperations
Fields inherited from class org.springframework.jdbc.support.JdbcAccessor |
logger |
Method Summary |
void |
afterPropertiesSet()
|
int |
delete(String statement)
|
int |
delete(String statement,
Object parameter)
|
|
execute(SqlSessionCallback<T> action)
|
|
execute(SqlSessionCallback<T> action,
org.apache.ibatis.session.ExecutorType executorType)
Execute the given data access action on a Executor. |
DataSource |
getDataSource()
Returns either the DataSource explicitly set for this template of the one specified by the SqlSessionFactory's Environment. |
|
getMapper(Class<T> type)
|
org.apache.ibatis.session.SqlSessionFactory |
getSqlSessionFactory()
|
int |
insert(String statement)
|
int |
insert(String statement,
Object parameter)
|
void |
select(String statement,
Object parameter,
org.apache.ibatis.session.ResultHandler handler)
|
void |
select(String statement,
Object parameter,
org.apache.ibatis.session.RowBounds rowBounds,
org.apache.ibatis.session.ResultHandler handler)
|
|
selectList(String statement)
|
|
selectList(String statement,
Object parameter)
|
|
selectList(String statement,
Object parameter,
org.apache.ibatis.session.RowBounds rowBounds)
|
Object |
selectOne(String statement)
|
Object |
selectOne(String statement,
Object parameter)
|
void |
setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
Sets the SqlSessionFactory this template will use when creating SqlSessions. |
int |
update(String statement)
|
int |
update(String statement,
Object parameter)
|
org.springframework.dao.DataAccessException |
wrapException(Throwable t)
|
Methods inherited from class org.springframework.jdbc.support.JdbcAccessor |
getExceptionTranslator, isLazyInit, setDatabaseProductName, setDataSource, setExceptionTranslator, setLazyInit |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
SqlSessionTemplate
public SqlSessionTemplate()
SqlSessionTemplate
public SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
getSqlSessionFactory
public org.apache.ibatis.session.SqlSessionFactory getSqlSessionFactory()
setSqlSessionFactory
public void setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
- Sets the SqlSessionFactory this template will use when creating SqlSessions.
getDataSource
public DataSource getDataSource()
- Returns either the DataSource explicitly set for this template of the one specified by the SqlSessionFactory's Environment.
- Overrides:
getDataSource
in class org.springframework.jdbc.support.JdbcAccessor
- See Also:
Environment
afterPropertiesSet
public void afterPropertiesSet()
- Specified by:
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
- Overrides:
afterPropertiesSet
in class org.springframework.jdbc.support.JdbcAccessor
execute
public <T> T execute(SqlSessionCallback<T> action)
throws org.springframework.dao.DataAccessException
- Throws:
org.springframework.dao.DataAccessException
execute
public <T> T execute(SqlSessionCallback<T> action,
org.apache.ibatis.session.ExecutorType executorType)
throws org.springframework.dao.DataAccessException
- Execute the given data access action on a Executor.
- Parameters:
action
- callback object that specifies the data access actionexecutorType
- SIMPLE, REUSE, BATCH
- Returns:
- a result object returned by the action, or
null
- Throws:
org.springframework.dao.DataAccessException
- in case of errors
selectOne
public Object selectOne(String statement)
- Specified by:
selectOne
in interface SqlSessionOperations
- See Also:
SqlSession.selectOne(String)
selectOne
public Object selectOne(String statement,
Object parameter)
- Specified by:
selectOne
in interface SqlSessionOperations
- See Also:
SqlSession.selectOne(String, Object)
selectList
public <T> List<T> selectList(String statement)
- Specified by:
selectList
in interface SqlSessionOperations
- See Also:
SqlSession.selectList(String, Object)
selectList
public <T> List<T> selectList(String statement,
Object parameter)
- Specified by:
selectList
in interface SqlSessionOperations
- See Also:
SqlSession.selectList(String, Object)
selectList
public <T> List<T> selectList(String statement,
Object parameter,
org.apache.ibatis.session.RowBounds rowBounds)
- Specified by:
selectList
in interface SqlSessionOperations
- See Also:
SqlSession.selectList(String, Object, org.apache.ibatis.session.RowBounds)
select
public void select(String statement,
Object parameter,
org.apache.ibatis.session.ResultHandler handler)
- Specified by:
select
in interface SqlSessionOperations
- See Also:
SqlSession.select(String, Object, org.apache.ibatis.session.ResultHandler)
select
public void select(String statement,
Object parameter,
org.apache.ibatis.session.RowBounds rowBounds,
org.apache.ibatis.session.ResultHandler handler)
- Specified by:
select
in interface SqlSessionOperations
- See Also:
SqlSession.select(String, Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler)
insert
public int insert(String statement)
- Specified by:
insert
in interface SqlSessionOperations
- See Also:
SqlSession.insert(String)
insert
public int insert(String statement,
Object parameter)
- Specified by:
insert
in interface SqlSessionOperations
- See Also:
SqlSession.insert(String, Object)
update
public int update(String statement)
- Specified by:
update
in interface SqlSessionOperations
- See Also:
SqlSession.update(String)
update
public int update(String statement,
Object parameter)
- Specified by:
update
in interface SqlSessionOperations
- See Also:
SqlSession.update(String, Object)
delete
public int delete(String statement)
- Specified by:
delete
in interface SqlSessionOperations
- See Also:
SqlSession.delete(String)
delete
public int delete(String statement,
Object parameter)
- Specified by:
delete
in interface SqlSessionOperations
- See Also:
SqlSession.delete(String, Object)
getMapper
public <T> T getMapper(Class<T> type)
- Specified by:
getMapper
in interface SqlSessionOperations
- See Also:
SqlSession.getMapper(Class)
wrapException
public org.springframework.dao.DataAccessException wrapException(Throwable t)
Copyright © 2010 MyBatis.org. All Rights Reserved.