123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- package com.baomidou.mybatisplus.solon.integration;
- import org.apache.ibatis.cursor.Cursor;
- import org.apache.ibatis.executor.BatchResult;
- import org.apache.ibatis.reflection.ExceptionUtil;
- import org.apache.ibatis.session.Configuration;
- import org.apache.ibatis.session.ResultHandler;
- import org.apache.ibatis.session.RowBounds;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.noear.solon.data.tran.TranUtils;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.sql.Connection;
- import java.util.List;
- import java.util.Map;
- public class SolonSqlSession implements SqlSession {
- private final SqlSessionFactory sqlSessionFactory;
- private final SqlSession sqlSessionProxy;
- public SolonSqlSession(SqlSessionFactory sqlSessionFactory) {
- this.sqlSessionFactory = sqlSessionFactory;
- this.sqlSessionProxy = (SqlSession) Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader(), new Class[]{SqlSession.class}, new SqlSessionInterceptor());
- }
- public SqlSessionFactory getSqlSessionFactory() {
- return this.sqlSessionFactory;
- }
- public SqlSession getSqlSessionProxy() {
- return this.sqlSessionProxy;
- }
- @Override
- public <T> T selectOne(String statement) {
- return this.sqlSessionProxy.selectOne(statement);
- }
- @Override
- public <T> T selectOne(String statement, Object parameter) {
- return this.sqlSessionProxy.selectOne(statement, parameter);
- }
- @Override
- public <K, V> Map<K, V> selectMap(String statement, String mapKey) {
- return this.sqlSessionProxy.selectMap(statement, mapKey);
- }
- @Override
- public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey) {
- return this.sqlSessionProxy.selectMap(statement, parameter, mapKey);
- }
- @Override
- public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
- return this.sqlSessionProxy.selectMap(statement, parameter, mapKey, rowBounds);
- }
- @Override
- public <T> Cursor<T> selectCursor(String statement) {
- return this.sqlSessionProxy.selectCursor(statement);
- }
- @Override
- public <T> Cursor<T> selectCursor(String statement, Object parameter) {
- return this.sqlSessionProxy.selectCursor(statement, parameter);
- }
- @Override
- public <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds) {
- return this.sqlSessionProxy.selectCursor(statement, parameter, rowBounds);
- }
- @Override
- public <E> List<E> selectList(String statement) {
- return this.sqlSessionProxy.selectList(statement);
- }
- @Override
- public <E> List<E> selectList(String statement, Object parameter) {
- return this.sqlSessionProxy.selectList(statement, parameter);
- }
- @Override
- public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
- return this.sqlSessionProxy.selectList(statement, parameter, rowBounds);
- }
- @Override
- public void select(String statement, ResultHandler handler) {
- this.sqlSessionProxy.select(statement, handler);
- }
- @Override
- public void select(String statement, Object parameter, ResultHandler handler) {
- this.sqlSessionProxy.select(statement, parameter, handler);
- }
- @Override
- public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
- this.sqlSessionProxy.select(statement, parameter, rowBounds, handler);
- }
- @Override
- public int insert(String statement) {
- return this.sqlSessionProxy.insert(statement);
- }
- @Override
- public int insert(String statement, Object parameter) {
- return this.sqlSessionProxy.insert(statement, parameter);
- }
- @Override
- public int update(String statement) {
- return this.sqlSessionProxy.update(statement);
- }
- @Override
- public int update(String statement, Object parameter) {
- return this.sqlSessionProxy.update(statement, parameter);
- }
- @Override
- public int delete(String statement) {
- return this.sqlSessionProxy.delete(statement);
- }
- @Override
- public int delete(String statement, Object parameter) {
- return this.sqlSessionProxy.delete(statement, parameter);
- }
- @Override
- public <T> T getMapper(Class<T> type) {
- return this.getConfiguration().getMapper(type, this);
- }
- @Override
- public void commit() {
- throw new UnsupportedOperationException("Manual commit is not allowed over a Solon managed SqlSession");
- }
- @Override
- public void commit(boolean force) {
- throw new UnsupportedOperationException("Manual commit is not allowed over a Solon managed SqlSession");
- }
- @Override
- public void rollback() {
- throw new UnsupportedOperationException("Manual rollback is not allowed over a Solon managed SqlSession");
- }
- @Override
- public void rollback(boolean force) {
- throw new UnsupportedOperationException("Manual rollback is not allowed over a Solon managed SqlSession");
- }
- @Override
- public void close() {
- throw new UnsupportedOperationException("Manual close is not allowed over a Solon managed SqlSession");
- }
- @Override
- public void clearCache() {
- this.sqlSessionProxy.clearCache();
- }
- @Override
- public Configuration getConfiguration() {
- return this.sqlSessionFactory.getConfiguration();
- }
- @Override
- public Connection getConnection() {
- return this.sqlSessionProxy.getConnection();
- }
- @Override
- public List<BatchResult> flushStatements() {
- return this.sqlSessionProxy.flushStatements();
- }
- private class SqlSessionInterceptor implements InvocationHandler {
- private SqlSessionInterceptor() {
- }
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- Object unwrapped;
- try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
- Object result = method.invoke(sqlSession, args);
- sqlSession.commit(!TranUtils.inTrans());
- unwrapped = result;
- } catch (Throwable var11) {
- unwrapped = ExceptionUtil.unwrapThrowable(var11);
- if (unwrapped instanceof RuntimeException) {
- throw (RuntimeException) unwrapped;
- }
- throw (Throwable) unwrapped;
- }
- return unwrapped;
- }
- }
- }
|