MybatisSqlSessionTemplate.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /**
  2. * Copyright 2010-2016 the original author or authors.
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.baomidou.mybatisplus;
  17. import static java.lang.reflect.Proxy.newProxyInstance;
  18. import java.lang.reflect.InvocationHandler;
  19. import java.lang.reflect.Method;
  20. import java.sql.Connection;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.apache.ibatis.cursor.Cursor;
  24. import org.apache.ibatis.executor.BatchResult;
  25. import org.apache.ibatis.session.Configuration;
  26. import org.apache.ibatis.session.ExecutorType;
  27. import org.apache.ibatis.session.ResultHandler;
  28. import org.apache.ibatis.session.RowBounds;
  29. import org.apache.ibatis.session.SqlSession;
  30. import org.apache.ibatis.session.SqlSessionFactory;
  31. import org.mybatis.spring.MyBatisExceptionTranslator;
  32. import org.springframework.beans.factory.DisposableBean;
  33. import org.springframework.dao.support.PersistenceExceptionTranslator;
  34. import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
  35. import org.springframework.util.Assert;
  36. /**
  37. * Copy SqlSessionTemplate
  38. *
  39. * @see org.mybatis.spring.SqlSessionTemplate
  40. */
  41. public class MybatisSqlSessionTemplate implements SqlSession, DisposableBean {
  42. private final SqlSessionFactory sqlSessionFactory;
  43. private final ExecutorType executorType;
  44. private final SqlSession sqlSessionProxy;
  45. private final PersistenceExceptionTranslator exceptionTranslator;
  46. /**
  47. * Constructs a Spring managed SqlSession with the {@code SqlSessionFactory}
  48. * provided as an argument.
  49. *
  50. * @param sqlSessionFactory
  51. */
  52. public MybatisSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
  53. this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType());
  54. }
  55. /**
  56. * Constructs a Spring managed SqlSession with the {@code SqlSessionFactory}
  57. * provided as an argument and the given {@code ExecutorType}
  58. * {@code ExecutorType} cannot be changed once the
  59. * {@code SqlSessionTemplate} is constructed.
  60. *
  61. * @param sqlSessionFactory
  62. * @param executorType
  63. */
  64. public MybatisSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {
  65. this(sqlSessionFactory, executorType, new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration()
  66. .getEnvironment().getDataSource(), true));
  67. }
  68. /**
  69. * Constructs a Spring managed {@code SqlSession} with the given
  70. * {@code SqlSessionFactory} and {@code ExecutorType}. A custom
  71. * {@code SQLExceptionTranslator} can be provided as an argument so any
  72. * {@code PersistenceException} thrown by MyBatis can be custom translated
  73. * to a {@code RuntimeException} The {@code SQLExceptionTranslator} can also
  74. * be null and thus no exception translation will be done and MyBatis
  75. * exceptions will be thrown
  76. *
  77. * @param sqlSessionFactory
  78. * @param executorType
  79. * @param exceptionTranslator
  80. */
  81. public MybatisSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
  82. PersistenceExceptionTranslator exceptionTranslator) {
  83. Assert.notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
  84. Assert.notNull(executorType, "Property 'executorType' is required");
  85. this.sqlSessionFactory = sqlSessionFactory;
  86. this.executorType = executorType;
  87. this.exceptionTranslator = exceptionTranslator;
  88. this.sqlSessionProxy = (SqlSession) newProxyInstance(SqlSessionFactory.class.getClassLoader(),
  89. new Class[]{SqlSession.class}, new SqlSessionInterceptor());
  90. }
  91. public SqlSessionFactory getSqlSessionFactory() {
  92. return this.sqlSessionFactory;
  93. }
  94. public ExecutorType getExecutorType() {
  95. return this.executorType;
  96. }
  97. public PersistenceExceptionTranslator getPersistenceExceptionTranslator() {
  98. return this.exceptionTranslator;
  99. }
  100. /**
  101. * {@inheritDoc}
  102. */
  103. @Override
  104. public <T> T selectOne(String statement) {
  105. return this.sqlSessionProxy.<T>selectOne(statement);
  106. }
  107. /**
  108. * {@inheritDoc}
  109. */
  110. @Override
  111. public <T> T selectOne(String statement, Object parameter) {
  112. return this.sqlSessionProxy.<T>selectOne(statement, parameter);
  113. }
  114. /**
  115. * {@inheritDoc}
  116. */
  117. @Override
  118. public <K, V> Map<K, V> selectMap(String statement, String mapKey) {
  119. return this.sqlSessionProxy.<K, V>selectMap(statement, mapKey);
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. @Override
  125. public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey) {
  126. return this.sqlSessionProxy.<K, V>selectMap(statement, parameter, mapKey);
  127. }
  128. /**
  129. * {@inheritDoc}
  130. */
  131. @Override
  132. public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
  133. return this.sqlSessionProxy.<K, V>selectMap(statement, parameter, mapKey, rowBounds);
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. @Override
  139. public <T> Cursor<T> selectCursor(String statement) {
  140. return this.sqlSessionProxy.selectCursor(statement);
  141. }
  142. /**
  143. * {@inheritDoc}
  144. */
  145. @Override
  146. public <T> Cursor<T> selectCursor(String statement, Object parameter) {
  147. return this.sqlSessionProxy.selectCursor(statement, parameter);
  148. }
  149. /**
  150. * {@inheritDoc}
  151. */
  152. @Override
  153. public <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds) {
  154. return this.sqlSessionProxy.selectCursor(statement, parameter, rowBounds);
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. @Override
  160. public <E> List<E> selectList(String statement) {
  161. return this.sqlSessionProxy.<E>selectList(statement);
  162. }
  163. /**
  164. * {@inheritDoc}
  165. */
  166. @Override
  167. public <E> List<E> selectList(String statement, Object parameter) {
  168. return this.sqlSessionProxy.<E>selectList(statement, parameter);
  169. }
  170. /**
  171. * {@inheritDoc}
  172. */
  173. @Override
  174. public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
  175. return this.sqlSessionProxy.<E>selectList(statement, parameter, rowBounds);
  176. }
  177. /**
  178. * {@inheritDoc}
  179. */
  180. @Override
  181. public void select(String statement, ResultHandler handler) {
  182. this.sqlSessionProxy.select(statement, handler);
  183. }
  184. /**
  185. * {@inheritDoc}
  186. */
  187. @Override
  188. public void select(String statement, Object parameter, ResultHandler handler) {
  189. this.sqlSessionProxy.select(statement, parameter, handler);
  190. }
  191. /**
  192. * {@inheritDoc}
  193. */
  194. @Override
  195. public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
  196. this.sqlSessionProxy.select(statement, parameter, rowBounds, handler);
  197. }
  198. /**
  199. * {@inheritDoc}
  200. */
  201. @Override
  202. public int insert(String statement) {
  203. return this.sqlSessionProxy.insert(statement);
  204. }
  205. /**
  206. * {@inheritDoc}
  207. */
  208. @Override
  209. public int insert(String statement, Object parameter) {
  210. return this.sqlSessionProxy.insert(statement, parameter);
  211. }
  212. /**
  213. * {@inheritDoc}
  214. */
  215. @Override
  216. public int update(String statement) {
  217. return this.sqlSessionProxy.update(statement);
  218. }
  219. /**
  220. * {@inheritDoc}
  221. */
  222. @Override
  223. public int update(String statement, Object parameter) {
  224. return this.sqlSessionProxy.update(statement, parameter);
  225. }
  226. /**
  227. * {@inheritDoc}
  228. */
  229. @Override
  230. public int delete(String statement) {
  231. return this.sqlSessionProxy.delete(statement);
  232. }
  233. /**
  234. * {@inheritDoc}
  235. */
  236. @Override
  237. public int delete(String statement, Object parameter) {
  238. return this.sqlSessionProxy.delete(statement, parameter);
  239. }
  240. /**
  241. * {@inheritDoc}
  242. */
  243. @Override
  244. public <T> T getMapper(Class<T> type) {
  245. return getConfiguration().getMapper(type, this);
  246. }
  247. /**
  248. * {@inheritDoc}
  249. */
  250. @Override
  251. public void commit() {
  252. throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
  253. }
  254. /**
  255. * {@inheritDoc}
  256. */
  257. @Override
  258. public void commit(boolean force) {
  259. throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
  260. }
  261. /**
  262. * {@inheritDoc}
  263. */
  264. @Override
  265. public void rollback() {
  266. throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
  267. }
  268. /**
  269. * {@inheritDoc}
  270. */
  271. @Override
  272. public void rollback(boolean force) {
  273. throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
  274. }
  275. /**
  276. * {@inheritDoc}
  277. */
  278. @Override
  279. public void close() {
  280. throw new UnsupportedOperationException("Manual close is not allowed over a Spring managed SqlSession");
  281. }
  282. /**
  283. * {@inheritDoc}
  284. */
  285. @Override
  286. public void clearCache() {
  287. this.sqlSessionProxy.clearCache();
  288. }
  289. /**
  290. * {@inheritDoc}
  291. */
  292. @Override
  293. public Configuration getConfiguration() {
  294. return this.sqlSessionFactory.getConfiguration();
  295. }
  296. /**
  297. * {@inheritDoc}
  298. */
  299. @Override
  300. public Connection getConnection() {
  301. return this.sqlSessionProxy.getConnection();
  302. }
  303. /**
  304. * {@inheritDoc}
  305. *
  306. * @since 1.0.2
  307. */
  308. @Override
  309. public List<BatchResult> flushStatements() {
  310. return this.sqlSessionProxy.flushStatements();
  311. }
  312. /**
  313. * Allow gently dispose bean:
  314. * <p>
  315. * <p>
  316. * <pre>
  317. * {@code
  318. *
  319. * <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  320. * <constructor-arg index="0" ref="sqlSessionFactory" />
  321. * </bean>
  322. * }
  323. * </pre>
  324. * <p>
  325. * The implementation of {@link DisposableBean} forces spring context to use
  326. * {@link DisposableBean#destroy()} method instead of
  327. * {@link MybatisSqlSessionTemplate#close()} to shutdown gently.
  328. *
  329. * @see MybatisSqlSessionTemplate#close()
  330. * @see org.springframework.beans.factory.support.DisposableBeanAdapter#inferDestroyMethodIfNecessary
  331. * @see org.springframework.beans.factory.support.DisposableBeanAdapter#CLOSE_METHOD_NAME
  332. */
  333. @Override
  334. public void destroy() throws Exception {
  335. // This method forces spring disposer to avoid call of
  336. // SqlSessionTemplate.close() which gives UnsupportedOperationException
  337. }
  338. /**
  339. * Proxy needed to route MyBatis method calls to the proper SqlSession got
  340. * from Spring's Transaction Manager It also unwraps exceptions thrown by
  341. * {@code Method#invoke(Object, Object...)} to pass a
  342. * {@code PersistenceException} to the
  343. * {@code PersistenceExceptionTranslator}.
  344. */
  345. private class SqlSessionInterceptor implements InvocationHandler {
  346. @Override
  347. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  348. SqlSession sqlSession = MybatisSqlSessionTemplate.this.sqlSessionFactory
  349. .openSession(MybatisSqlSessionTemplate.this.executorType);
  350. try {
  351. Object result = method.invoke(sqlSession, args);
  352. sqlSession.commit(true);
  353. return result;
  354. } catch (Throwable t) {
  355. throw new MybatisPlusException(t);
  356. } finally {
  357. if (sqlSession != null) {
  358. sqlSession.close();
  359. }
  360. }
  361. }
  362. }
  363. }