remove RowSessionManager.java and rename DbAutoConfiguration to FlexTransactionAutoConfiguration

This commit is contained in:
开源海哥 2023-04-17 16:49:15 +08:00
parent cec2237e85
commit 3edbed91c8
11 changed files with 31 additions and 192 deletions

View File

@ -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> R execute(Function<RowMapper, R> 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<Row> 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;
}

View File

@ -1,60 +0,0 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <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.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);
}

View File

@ -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();
}
}

View File

@ -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());
}
}
}

View File

@ -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();
}
}

View File

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

View File

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

View File

@ -1,36 +0,0 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <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.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);
}
}

View File

@ -39,6 +39,9 @@ public class DbTestStarter {
.setDataSource(dataSource)
.start();
Row row1 = Db.selectOneById("tb_account", "id", 1);
System.out.println(row1);
//查询全部
List<Row> rows = Db.selectAll("tb_account");
System.out.println(rows);

View File

@ -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);
}

View File

@ -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<ContextRefreshedEvent> {
@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());
}
}