|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.jdbc.support.JdbcAccessor
org.mybatis.spring.SqlSessionTemplate
public class SqlSessionTemplate
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
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 or as a constructor argument.
SqlSessionTemplate is thread safe, so a single instance can be shared by all DAOs; there should also be a small memory savings by doing this. This pattern can be used in Spring configuration files as follows:
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
execute(org.mybatis.spring.SqlSessionCallback)
,
setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory)
,
SqlSessionFactoryBean.setDataSource(javax.sql.DataSource)
,
SqlSessionFactory.getConfiguration()
,
SqlSession
,
SqlSessionOperations
Field Summary |
---|
Fields inherited from class org.springframework.jdbc.support.JdbcAccessor |
---|
logger |
Constructor Summary | |
---|---|
SqlSessionTemplate()
This constructor is left here to enable the creation of the SqlSessionTemplate using this xml in the applicationContext.xml. |
|
SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
|
Method Summary | ||
---|---|---|
void |
afterPropertiesSet()
|
|
int |
delete(String statement)
|
|
int |
delete(String statement,
Object parameter)
|
|
|
execute(SqlSessionCallback<T> action)
Execute the given data access action with the proper SqlSession (got from current transaction or a new one) |
|
|
execute(SqlSessionCallback<T> action,
org.apache.ibatis.session.ExecutorType executorType)
Execute the given data access action on a Executor. |
|
DataSource |
getDataSource()
|
|
|
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)
|
|
void |
select(String statement,
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 |
setDataSource(DataSource dataSource)
|
|
void |
setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
|
|
int |
update(String statement)
|
|
int |
update(String statement,
Object parameter)
|
|
protected org.springframework.dao.DataAccessException |
wrapException(Throwable t)
Translates MyBatis exceptions into Spring DataAccessExceptions. |
Methods inherited from class org.springframework.jdbc.support.JdbcAccessor |
---|
getExceptionTranslator, isLazyInit, setDatabaseProductName, setExceptionTranslator, setLazyInit |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public SqlSessionTemplate()
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
public SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
Method Detail |
---|
public org.apache.ibatis.session.SqlSessionFactory getSqlSessionFactory()
public void setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
public void setDataSource(DataSource dataSource)
setDataSource
in class org.springframework.jdbc.support.JdbcAccessor
public DataSource getDataSource()
getDataSource
in class org.springframework.jdbc.support.JdbcAccessor
public void afterPropertiesSet()
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
afterPropertiesSet
in class org.springframework.jdbc.support.JdbcAccessor
public <T> T execute(SqlSessionCallback<T> action) throws org.springframework.dao.DataAccessException
T
- action
-
org.springframework.dao.DataAccessException
public <T> T execute(SqlSessionCallback<T> action, org.apache.ibatis.session.ExecutorType executorType) throws org.springframework.dao.DataAccessException
action
- callback object that specifies the data access actionexecutorType
- SIMPLE, REUSE, BATCH
null
org.springframework.dao.DataAccessException
- in case of errorspublic Object selectOne(String statement)
selectOne
in interface SqlSessionOperations
SqlSession.selectOne(String)
public Object selectOne(String statement, Object parameter)
selectOne
in interface SqlSessionOperations
SqlSession.selectOne(String, Object)
public <T> List<T> selectList(String statement)
selectList
in interface SqlSessionOperations
SqlSession.selectList(String, Object)
public <T> List<T> selectList(String statement, Object parameter)
selectList
in interface SqlSessionOperations
SqlSession.selectList(String, Object)
public <T> List<T> selectList(String statement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds)
selectList
in interface SqlSessionOperations
SqlSession.selectList(String, Object, org.apache.ibatis.session.RowBounds)
public void select(String statement, Object parameter, org.apache.ibatis.session.ResultHandler handler)
select
in interface SqlSessionOperations
SqlSession.select(String, Object, org.apache.ibatis.session.ResultHandler)
public void select(String statement, org.apache.ibatis.session.ResultHandler handler)
select
in interface SqlSessionOperations
SqlSession.select(String, org.apache.ibatis.session.ResultHandler)
public void select(String statement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds, org.apache.ibatis.session.ResultHandler handler)
select
in interface SqlSessionOperations
SqlSession.select(String, Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler)
public int insert(String statement)
insert
in interface SqlSessionOperations
SqlSession.insert(String)
public int insert(String statement, Object parameter)
insert
in interface SqlSessionOperations
SqlSession.insert(String, Object)
public int update(String statement)
update
in interface SqlSessionOperations
SqlSession.update(String)
public int update(String statement, Object parameter)
update
in interface SqlSessionOperations
SqlSession.update(String, Object)
public int delete(String statement)
delete
in interface SqlSessionOperations
SqlSession.delete(String)
public int delete(String statement, Object parameter)
delete
in interface SqlSessionOperations
SqlSession.delete(String, Object)
public <T> T getMapper(Class<T> type)
getMapper
in interface SqlSessionOperations
SqlSession.getMapper(Class)
protected org.springframework.dao.DataAccessException wrapException(Throwable t)
JdbcAccessor.getExceptionTranslator()
for the SqlException translation
t
- the exception has to be converted to DataAccessException.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |