From 3edbed91c894af9c4f64bf04495a5bc1644ef456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Mon, 17 Apr 2023 16:49:15 +0800 Subject: [PATCH] remove RowSessionManager.java and rename DbAutoConfiguration to FlexTransactionAutoConfiguration --- .../core/row/RowMapperInvoker.java | 18 +----- .../core/row/RowSessionManager.java | 60 ------------------- .../solon/SolonRowSessionManager.java | 22 ------- .../solon/integration/XPluginImpl.java | 18 +----- ... => FlexTransactionAutoConfiguration.java} | 20 +------ .../main/resources/META-INF/spring.factories | 2 +- ...ot.autoconfigure.AutoConfiguration.imports | 2 +- .../spring/SpringRowSessionManager.java | 36 ----------- .../com/mybatisflex/test/DbTestStarter.java | 3 + .../test/controller/AccountController.java | 35 ++++++----- .../java/com/mybatisflex/test/AppConfig.java | 7 --- 11 files changed, 31 insertions(+), 192 deletions(-) delete mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowSessionManager.java delete mode 100644 mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/SolonRowSessionManager.java rename mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/{DbAutoConfiguration.java => FlexTransactionAutoConfiguration.java} (66%) delete mode 100644 mybatis-flex-spring/src/main/java/com/mybatisflex/spring/SpringRowSessionManager.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java index bc8471fe..3273d508 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java @@ -29,27 +29,15 @@ import java.util.function.Function; public class RowMapperInvoker { private final SqlSessionFactory sqlSessionFactory; - private RowSessionManager rowSessionManager = RowSessionManager.DEFAULT; public RowMapperInvoker(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } - public RowSessionManager getRowSessionManager() { - return rowSessionManager; - } - - public void setRowSessionManager(RowSessionManager rowSessionManager) { - this.rowSessionManager = rowSessionManager; - } - private R execute(Function function) { - SqlSession sqlSession = rowSessionManager.getSqlSession(sqlSessionFactory); - try { + try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) { RowMapper mapper = sqlSession.getMapper(RowMapper.class); return function.apply(mapper); - } finally { - rowSessionManager.releaseSqlSession(sqlSession, sqlSessionFactory); } } @@ -65,7 +53,7 @@ public class RowMapperInvoker { public int[] insertBatch(String tableName, Collection rows, int batchSize) { int[] results = new int[rows.size()]; - SqlSession sqlSession = rowSessionManager.getSqlSession(sqlSessionFactory, ExecutorType.BATCH); + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,true); try { RowMapper mapper = sqlSession.getMapper(RowMapper.class); int counter = 0; @@ -85,7 +73,7 @@ public class RowMapperInvoker { } } } finally { - rowSessionManager.releaseSqlSession(sqlSession, sqlSessionFactory); + sqlSession.close(); } return results; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowSessionManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowSessionManager.java deleted file mode 100644 index c2558c30..00000000 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowSessionManager.java +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * 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 - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * 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.mybatisflex.core.row; - -import org.apache.ibatis.session.ExecutorType; -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.session.SqlSessionFactory; - -public interface RowSessionManager { - RowSessionManager DEFAULT = new RowSessionManager() { - @Override - public SqlSession getSqlSession(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) { - return sqlSessionFactory.openSession(executorType); - } - - @Override - public void releaseSqlSession(SqlSession sqlSession, SqlSessionFactory sqlSessionFactory) { - sqlSession.commit(); - sqlSession.close(); - } - }; - - /** - * 获取 sqlSession - * - * @param sqlSessionFactory - */ - default SqlSession getSqlSession(SqlSessionFactory sqlSessionFactory){ - return getSqlSession(sqlSessionFactory,sqlSessionFactory.getConfiguration().getDefaultExecutorType()); - } - - - /** - * 获取 sqlSession - * @param sqlSessionFactory - * @param executorType - */ - SqlSession getSqlSession(SqlSessionFactory sqlSessionFactory, ExecutorType executorType); - - /** - * 释放 sqlSession - * - * @param sqlSession - */ - void releaseSqlSession(SqlSession sqlSession, SqlSessionFactory sqlSessionFactory); - -} diff --git a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/SolonRowSessionManager.java b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/SolonRowSessionManager.java deleted file mode 100644 index 4606a75b..00000000 --- a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/SolonRowSessionManager.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.mybatisflex.solon; - -import com.mybatisflex.core.row.RowSessionManager; -import org.apache.ibatis.session.ExecutorType; -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.session.SqlSessionFactory; - -/** - * @author noear - * @since 2.2 - */ -public class SolonRowSessionManager implements RowSessionManager { - @Override - public SqlSession getSqlSession(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) { - return sqlSessionFactory.openSession(executorType); - } - - @Override - public void releaseSqlSession(SqlSession sqlSession, SqlSessionFactory sqlSessionFactory) { - sqlSession.close(); - } -} diff --git a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/XPluginImpl.java b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/XPluginImpl.java index 70808431..2d18826e 100644 --- a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/XPluginImpl.java +++ b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/XPluginImpl.java @@ -1,14 +1,8 @@ package com.mybatisflex.solon.integration; -import com.mybatisflex.core.FlexGlobalConfig; -import com.mybatisflex.core.row.Db; -import com.mybatisflex.solon.SolonRowSessionManager; +import org.apache.ibatis.solon.integration.MybatisAdapterManager; import org.noear.solon.core.AopContext; import org.noear.solon.core.Plugin; -import org.apache.ibatis.solon.integration.MybatisAdapterManager; - -import java.util.logging.Level; -import java.util.logging.Logger; /** * @author noear @@ -21,16 +15,6 @@ public class XPluginImpl implements Plugin { // 此插件的 solon.plugin.priority 会大于 mybatis-solon-plugin 的值 // MybatisAdapterManager.setAdapterFactory(new MybatisAdapterFactoryFlex()); - - dbConfiguration(); } - private void dbConfiguration(){ - FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig(); - if (defaultConfig == null){ - Logger.getLogger(Db.class.getName()).log(Level.WARNING,"Cannot get FlexGlobalConfig instance, Perhaps the dataSource config error."); - }else { - Db.invoker().setRowSessionManager(new SolonRowSessionManager()); - } - } } diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/DbAutoConfiguration.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/FlexTransactionAutoConfiguration.java similarity index 66% rename from mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/DbAutoConfiguration.java rename to mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/FlexTransactionAutoConfiguration.java index f76f4469..56edfce6 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/DbAutoConfiguration.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/FlexTransactionAutoConfiguration.java @@ -15,40 +15,22 @@ */ package com.mybatisflex.spring.boot; -import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.row.Db; import com.mybatisflex.spring.FlexTransactionManager; -import com.mybatisflex.spring.SpringRowSessionManager; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.TransactionManager; import org.springframework.transaction.annotation.TransactionManagementConfigurer; -import java.util.logging.Level; -import java.util.logging.Logger; - @ConditionalOnClass(Db.class) @Configuration(proxyBeanMethods = false) @AutoConfigureAfter({MybatisFlexAutoConfiguration.class}) -public class DbAutoConfiguration implements TransactionManagementConfigurer { - - public DbAutoConfiguration() { - FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig(); - if (defaultConfig == null) { - Logger.getLogger(DbAutoConfiguration.class.getName()).log(Level.WARNING - , "Cannot get FlexGlobalConfig instance, Perhaps the dataSource config error."); - } else { - Db.invoker().setRowSessionManager(new SpringRowSessionManager()); - } - } - +public class FlexTransactionAutoConfiguration implements TransactionManagementConfigurer { @Override public TransactionManager annotationDrivenTransactionManager() { return new FlexTransactionManager(); } - - } diff --git a/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring.factories b/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring.factories index b64771df..d5ed7f94 100644 --- a/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -2,7 +2,7 @@ org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector=\ com.mybatisflex.spring.boot.MybatisFlexDependsOnDatabaseInitializationDetector org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.mybatisflex.spring.boot.DbAutoConfiguration,\ + com.mybatisflex.spring.boot.FlexTransactionAutoConfiguration,\ com.mybatisflex.spring.boot.MultiDataSourceAutoConfiguration,\ com.mybatisflex.spring.boot.MybatisFlexAutoConfiguration,\ com.mybatisflex.spring.boot.MybatisLanguageDriverAutoConfiguration \ No newline at end of file diff --git a/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 5d48c6ab..c167ba6c 100644 --- a/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,4 +1,4 @@ -com.mybatisflex.spring.boot.DbAutoConfiguration +com.mybatisflex.spring.boot.FlexTransactionAutoConfiguration com.mybatisflex.spring.boot.MultiDataSourceAutoConfiguration com.mybatisflex.spring.boot.MybatisFlexAutoConfiguration com.mybatisflex.spring.boot.MybatisLanguageDriverAutoConfiguration diff --git a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/SpringRowSessionManager.java b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/SpringRowSessionManager.java deleted file mode 100644 index c8253625..00000000 --- a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/SpringRowSessionManager.java +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * 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 - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * 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.mybatisflex.spring; - -import com.mybatisflex.core.row.RowSessionManager; -import org.apache.ibatis.session.ExecutorType; -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.session.SqlSessionFactory; -import org.mybatis.spring.SqlSessionUtils; - -public class SpringRowSessionManager implements RowSessionManager { - - @Override - public SqlSession getSqlSession(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) { - return SqlSessionUtils.getSqlSession(sqlSessionFactory, executorType, null); - } - - - @Override - public void releaseSqlSession(SqlSession sqlSession, SqlSessionFactory sqlSessionFactory) { - SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); - } -} diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java index 22ce757b..7dfb70ac 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java @@ -39,6 +39,9 @@ public class DbTestStarter { .setDataSource(dataSource) .start(); + Row row1 = Db.selectOneById("tb_account", "id", 1); + System.out.println(row1); + //查询全部 List rows = Db.selectAll("tb_account"); System.out.println(rows); diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java index a473329a..edf1276c 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java @@ -17,6 +17,8 @@ package com.mybatisflex.test.controller; import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.row.Db; +import com.mybatisflex.core.row.Row; import com.mybatisflex.test.mapper.AccountMapper; import com.mybatisflex.test.model.Account; import com.mybatisflex.test.service.AccountService; @@ -48,23 +50,28 @@ public class AccountController { @Transactional public Account selectOne(@PathVariable("id") Long id) { - Account account = new Account(); - account.setId(1L); - account.setUserName("heihei"); - accountMapper.update(account); +// Account account = new Account(); +// account.setId(1L); +// account.setUserName("heihei"); +// accountMapper.update(account); + Row row1 = Db.selectOneById("tb_account", "id", 1); + System.out.println(">>>>>>> row1: " + row1); - Account account1 = accountMapper.selectOneById(1L); - Account account2 = accountMapper.selectOneById(2L); + Row row2 = Db.selectOneById("tb_account", "id", 2); + System.out.println(">>>>>>> row2: " + row2); - accountService.update2(); - - if (true) { - throw new IllegalStateException("aaa"); - } - - System.out.println("selectOne >>>> " + account1); - System.out.println("selectOne >>>> " + account2); +// Account account1 = accountMapper.selectOneById(1L); +// Account account2 = accountMapper.selectOneById(2L); +// +// accountService.update2(); +// +// if (true) { +// throw new IllegalStateException("aaa"); +// } +// +// System.out.println("selectOne >>>> " + account1); +// System.out.println("selectOne >>>> " + account2); return accountMapper.selectOneById(id); } diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/AppConfig.java b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/AppConfig.java index a02a1aab..d1e4fbc2 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/AppConfig.java +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/AppConfig.java @@ -15,9 +15,7 @@ */ package com.mybatisflex.test; -import com.mybatisflex.core.row.Db; import com.mybatisflex.spring.FlexSqlSessionFactoryBean; -import com.mybatisflex.spring.SpringRowSessionManager; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; @@ -56,15 +54,10 @@ public class AppConfig implements ApplicationListener { @EventListener(classes = {ContextStartedEvent.class}) public void handleContextStartedEvent() { System.out.println("handleContextStartedEvent listener invoked!"); - - // 为 Db 设置默认的 SqlSession - Db.invoker().setRowSessionManager(new SpringRowSessionManager()); } @Override public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println("onApplicationEvent"); - // 为 Db 设置默认的 SqlSession - Db.invoker().setRowSessionManager(new SpringRowSessionManager()); } }