123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- /*
- * Copyright 2010-2016 the original author or authors.
- * <p>
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.baomidou.mybatisplus.extension;
- import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
- import org.apache.ibatis.cursor.Cursor;
- import org.apache.ibatis.executor.BatchResult;
- import org.apache.ibatis.session.*;
- import org.mybatis.spring.MyBatisExceptionTranslator;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.dao.support.PersistenceExceptionTranslator;
- import org.springframework.util.Assert;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.sql.Connection;
- import java.util.List;
- import java.util.Map;
- import static java.lang.reflect.Proxy.newProxyInstance;
- /**
- * Copy SqlSessionTemplate
- *
- * @see org.mybatis.spring.SqlSessionTemplate
- */
- public class MybatisSqlSessionTemplate implements SqlSession, DisposableBean {
- private final SqlSessionFactory sqlSessionFactory;
- private final ExecutorType executorType;
- private final SqlSession sqlSessionProxy;
- private final PersistenceExceptionTranslator exceptionTranslator;
- /**
- * Constructs a Spring managed SqlSession with the {@code SqlSessionFactory}
- * provided as an argument.
- *
- * @param sqlSessionFactory
- */
- public MybatisSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
- this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType());
- }
- /**
- * Constructs a Spring managed SqlSession with the {@code SqlSessionFactory}
- * provided as an argument and the given {@code ExecutorType}
- * {@code ExecutorType} cannot be changed once the
- * {@code SqlSessionTemplate} is constructed.
- *
- * @param sqlSessionFactory
- * @param executorType
- */
- public MybatisSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {
- this(sqlSessionFactory, executorType, new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration()
- .getEnvironment().getDataSource(), true));
- }
- /**
- * Constructs a Spring managed {@code SqlSession} with the given
- * {@code SqlSessionFactory} and {@code ExecutorType}. A custom
- * {@code SQLExceptionTranslator} can be provided as an argument so any
- * {@code PersistenceException} thrown by MyBatis can be custom translated
- * to a {@code RuntimeException} The {@code SQLExceptionTranslator} can also
- * be null and thus no exception translation will be done and MyBatis
- * exceptions will be thrown
- *
- * @param sqlSessionFactory
- * @param executorType
- * @param exceptionTranslator
- */
- public MybatisSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
- PersistenceExceptionTranslator exceptionTranslator) {
- Assert.notNull(sqlSessionFactory, "SFunction 'sqlSessionFactory' is required");
- Assert.notNull(executorType, "SFunction 'executorType' is required");
- this.sqlSessionFactory = sqlSessionFactory;
- this.executorType = executorType;
- this.exceptionTranslator = exceptionTranslator;
- this.sqlSessionProxy = (SqlSession) newProxyInstance(SqlSessionFactory.class.getClassLoader(),
- new Class[]{SqlSession.class}, new SqlSessionInterceptor());
- }
- public SqlSessionFactory getSqlSessionFactory() {
- return this.sqlSessionFactory;
- }
- public ExecutorType getExecutorType() {
- return this.executorType;
- }
- public PersistenceExceptionTranslator getPersistenceExceptionTranslator() {
- return this.exceptionTranslator;
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <T> T selectOne(String statement) {
- return this.sqlSessionProxy.<T>selectOne(statement);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <T> T selectOne(String statement, Object parameter) {
- return this.sqlSessionProxy.<T>selectOne(statement, parameter);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <K, V> Map<K, V> selectMap(String statement, String mapKey) {
- return this.sqlSessionProxy.<K, V>selectMap(statement, mapKey);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey) {
- return this.sqlSessionProxy.<K, V>selectMap(statement, parameter, mapKey);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
- return this.sqlSessionProxy.<K, V>selectMap(statement, parameter, mapKey, rowBounds);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <T> Cursor<T> selectCursor(String statement) {
- return this.sqlSessionProxy.selectCursor(statement);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <T> Cursor<T> selectCursor(String statement, Object parameter) {
- return this.sqlSessionProxy.selectCursor(statement, parameter);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds) {
- return this.sqlSessionProxy.selectCursor(statement, parameter, rowBounds);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <E> List<E> selectList(String statement) {
- return this.sqlSessionProxy.<E>selectList(statement);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <E> List<E> selectList(String statement, Object parameter) {
- return this.sqlSessionProxy.<E>selectList(statement, parameter);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
- return this.sqlSessionProxy.<E>selectList(statement, parameter, rowBounds);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void select(String statement, ResultHandler handler) {
- this.sqlSessionProxy.select(statement, handler);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void select(String statement, Object parameter, ResultHandler handler) {
- this.sqlSessionProxy.select(statement, parameter, handler);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
- this.sqlSessionProxy.select(statement, parameter, rowBounds, handler);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public int insert(String statement) {
- return this.sqlSessionProxy.insert(statement);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public int insert(String statement, Object parameter) {
- return this.sqlSessionProxy.insert(statement, parameter);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public int update(String statement) {
- return this.sqlSessionProxy.update(statement);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public int update(String statement, Object parameter) {
- return this.sqlSessionProxy.update(statement, parameter);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public int delete(String statement) {
- return this.sqlSessionProxy.delete(statement);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public int delete(String statement, Object parameter) {
- return this.sqlSessionProxy.delete(statement, parameter);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public <T> T getMapper(Class<T> type) {
- return getConfiguration().getMapper(type, this);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void commit() {
- throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void commit(boolean force) {
- throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void rollback() {
- throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void rollback(boolean force) {
- throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void close() {
- throw new UnsupportedOperationException("Manual close is not allowed over a Spring managed SqlSession");
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void clearCache() {
- this.sqlSessionProxy.clearCache();
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public Configuration getConfiguration() {
- return this.sqlSessionFactory.getConfiguration();
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public Connection getConnection() {
- return this.sqlSessionProxy.getConnection();
- }
- /**
- * {@inheritDoc}
- *
- * @since 1.0.2
- */
- @Override
- public List<BatchResult> flushStatements() {
- return this.sqlSessionProxy.flushStatements();
- }
- /**
- * Allow gently dispose bean:
- * <p>
- * <p>
- * <pre>
- * {@code
- *
- * <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
- * <constructor-arg index="0" ref="sqlSessionFactory" />
- * </bean>
- * }
- * </pre>
- * <p>
- * The implementation of {@link DisposableBean} forces spring context to use
- * {@link DisposableBean#destroy()} method instead of
- * {@link MybatisSqlSessionTemplate#close()} to shutdown gently.
- *
- * @see MybatisSqlSessionTemplate#close()
- * @see org.springframework.beans.factory.support.DisposableBeanAdapter#inferDestroyMethodIfNecessary
- * @see org.springframework.beans.factory.support.DisposableBeanAdapter#CLOSE_METHOD_NAME
- */
- @Override
- public void destroy() throws Exception {
- // This method forces spring disposer to avoid call of
- // SqlSessionTemplate.close() which gives UnsupportedOperationException
- }
- /**
- * Proxy needed to route MyBatis method calls to the proper SqlSession got
- * from Spring's Transaction Manager It also unwraps exceptions thrown by
- * {@code Method#invoke(Object, Object...)} to pass a
- * {@code PersistenceException} to the
- * {@code PersistenceExceptionTranslator}.
- */
- private class SqlSessionInterceptor implements InvocationHandler {
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- SqlSession sqlSession = MybatisSqlSessionTemplate.this.sqlSessionFactory
- .openSession(MybatisSqlSessionTemplate.this.executorType);
- try {
- Object result = method.invoke(sqlSession, args);
- sqlSession.commit(true);
- return result;
- } catch (Throwable t) {
- throw ExceptionUtils.mpe(t);
- } finally {
- if (sqlSession != null) {
- sqlSession.close();
- }
- }
- }
- }
- }
|