mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
remove RowSessionManager.java and rename DbAutoConfiguration to FlexTransactionAutoConfiguration
This commit is contained in:
parent
cec2237e85
commit
3edbed91c8
@ -29,27 +29,15 @@ import java.util.function.Function;
|
|||||||
public class RowMapperInvoker {
|
public class RowMapperInvoker {
|
||||||
|
|
||||||
private final SqlSessionFactory sqlSessionFactory;
|
private final SqlSessionFactory sqlSessionFactory;
|
||||||
private RowSessionManager rowSessionManager = RowSessionManager.DEFAULT;
|
|
||||||
|
|
||||||
public RowMapperInvoker(SqlSessionFactory sqlSessionFactory) {
|
public RowMapperInvoker(SqlSessionFactory sqlSessionFactory) {
|
||||||
this.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) {
|
private <R> R execute(Function<RowMapper, R> function) {
|
||||||
SqlSession sqlSession = rowSessionManager.getSqlSession(sqlSessionFactory);
|
try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
|
||||||
try {
|
|
||||||
RowMapper mapper = sqlSession.getMapper(RowMapper.class);
|
RowMapper mapper = sqlSession.getMapper(RowMapper.class);
|
||||||
return function.apply(mapper);
|
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) {
|
public int[] insertBatch(String tableName, Collection<Row> rows, int batchSize) {
|
||||||
int[] results = new int[rows.size()];
|
int[] results = new int[rows.size()];
|
||||||
SqlSession sqlSession = rowSessionManager.getSqlSession(sqlSessionFactory, ExecutorType.BATCH);
|
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,true);
|
||||||
try {
|
try {
|
||||||
RowMapper mapper = sqlSession.getMapper(RowMapper.class);
|
RowMapper mapper = sqlSession.getMapper(RowMapper.class);
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
@ -85,7 +73,7 @@ public class RowMapperInvoker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
rowSessionManager.releaseSqlSession(sqlSession, sqlSessionFactory);
|
sqlSession.close();
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,14 +1,8 @@
|
|||||||
package com.mybatisflex.solon.integration;
|
package com.mybatisflex.solon.integration;
|
||||||
|
|
||||||
import com.mybatisflex.core.FlexGlobalConfig;
|
import org.apache.ibatis.solon.integration.MybatisAdapterManager;
|
||||||
import com.mybatisflex.core.row.Db;
|
|
||||||
import com.mybatisflex.solon.SolonRowSessionManager;
|
|
||||||
import org.noear.solon.core.AopContext;
|
import org.noear.solon.core.AopContext;
|
||||||
import org.noear.solon.core.Plugin;
|
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
|
* @author noear
|
||||||
@ -21,16 +15,6 @@ public class XPluginImpl implements Plugin {
|
|||||||
// 此插件的 solon.plugin.priority 会大于 mybatis-solon-plugin 的值
|
// 此插件的 solon.plugin.priority 会大于 mybatis-solon-plugin 的值
|
||||||
//
|
//
|
||||||
MybatisAdapterManager.setAdapterFactory(new MybatisAdapterFactoryFlex());
|
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,40 +15,22 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.spring.boot;
|
package com.mybatisflex.spring.boot;
|
||||||
|
|
||||||
import com.mybatisflex.core.FlexGlobalConfig;
|
|
||||||
import com.mybatisflex.core.row.Db;
|
import com.mybatisflex.core.row.Db;
|
||||||
import com.mybatisflex.spring.FlexTransactionManager;
|
import com.mybatisflex.spring.FlexTransactionManager;
|
||||||
import com.mybatisflex.spring.SpringRowSessionManager;
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.transaction.TransactionManager;
|
import org.springframework.transaction.TransactionManager;
|
||||||
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
|
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
|
||||||
|
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
@ConditionalOnClass(Db.class)
|
@ConditionalOnClass(Db.class)
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@AutoConfigureAfter({MybatisFlexAutoConfiguration.class})
|
@AutoConfigureAfter({MybatisFlexAutoConfiguration.class})
|
||||||
public class DbAutoConfiguration implements TransactionManagementConfigurer {
|
public class FlexTransactionAutoConfiguration 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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TransactionManager annotationDrivenTransactionManager() {
|
public TransactionManager annotationDrivenTransactionManager() {
|
||||||
return new FlexTransactionManager();
|
return new FlexTransactionManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -2,7 +2,7 @@
|
|||||||
org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector=\
|
org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector=\
|
||||||
com.mybatisflex.spring.boot.MybatisFlexDependsOnDatabaseInitializationDetector
|
com.mybatisflex.spring.boot.MybatisFlexDependsOnDatabaseInitializationDetector
|
||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
com.mybatisflex.spring.boot.DbAutoConfiguration,\
|
com.mybatisflex.spring.boot.FlexTransactionAutoConfiguration,\
|
||||||
com.mybatisflex.spring.boot.MultiDataSourceAutoConfiguration,\
|
com.mybatisflex.spring.boot.MultiDataSourceAutoConfiguration,\
|
||||||
com.mybatisflex.spring.boot.MybatisFlexAutoConfiguration,\
|
com.mybatisflex.spring.boot.MybatisFlexAutoConfiguration,\
|
||||||
com.mybatisflex.spring.boot.MybatisLanguageDriverAutoConfiguration
|
com.mybatisflex.spring.boot.MybatisLanguageDriverAutoConfiguration
|
||||||
@ -1,4 +1,4 @@
|
|||||||
com.mybatisflex.spring.boot.DbAutoConfiguration
|
com.mybatisflex.spring.boot.FlexTransactionAutoConfiguration
|
||||||
com.mybatisflex.spring.boot.MultiDataSourceAutoConfiguration
|
com.mybatisflex.spring.boot.MultiDataSourceAutoConfiguration
|
||||||
com.mybatisflex.spring.boot.MybatisFlexAutoConfiguration
|
com.mybatisflex.spring.boot.MybatisFlexAutoConfiguration
|
||||||
com.mybatisflex.spring.boot.MybatisLanguageDriverAutoConfiguration
|
com.mybatisflex.spring.boot.MybatisLanguageDriverAutoConfiguration
|
||||||
|
|||||||
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -39,6 +39,9 @@ public class DbTestStarter {
|
|||||||
.setDataSource(dataSource)
|
.setDataSource(dataSource)
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
|
Row row1 = Db.selectOneById("tb_account", "id", 1);
|
||||||
|
System.out.println(row1);
|
||||||
|
|
||||||
//查询全部
|
//查询全部
|
||||||
List<Row> rows = Db.selectAll("tb_account");
|
List<Row> rows = Db.selectAll("tb_account");
|
||||||
System.out.println(rows);
|
System.out.println(rows);
|
||||||
|
|||||||
@ -17,6 +17,8 @@ package com.mybatisflex.test.controller;
|
|||||||
|
|
||||||
import com.mybatisflex.core.paginate.Page;
|
import com.mybatisflex.core.paginate.Page;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
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.mapper.AccountMapper;
|
||||||
import com.mybatisflex.test.model.Account;
|
import com.mybatisflex.test.model.Account;
|
||||||
import com.mybatisflex.test.service.AccountService;
|
import com.mybatisflex.test.service.AccountService;
|
||||||
@ -48,23 +50,28 @@ public class AccountController {
|
|||||||
@Transactional
|
@Transactional
|
||||||
public Account selectOne(@PathVariable("id") Long id) {
|
public Account selectOne(@PathVariable("id") Long id) {
|
||||||
|
|
||||||
Account account = new Account();
|
// Account account = new Account();
|
||||||
account.setId(1L);
|
// account.setId(1L);
|
||||||
account.setUserName("heihei");
|
// account.setUserName("heihei");
|
||||||
accountMapper.update(account);
|
// accountMapper.update(account);
|
||||||
|
|
||||||
|
Row row1 = Db.selectOneById("tb_account", "id", 1);
|
||||||
|
System.out.println(">>>>>>> row1: " + row1);
|
||||||
|
|
||||||
Account account1 = accountMapper.selectOneById(1L);
|
Row row2 = Db.selectOneById("tb_account", "id", 2);
|
||||||
Account account2 = accountMapper.selectOneById(2L);
|
System.out.println(">>>>>>> row2: " + row2);
|
||||||
|
|
||||||
accountService.update2();
|
// Account account1 = accountMapper.selectOneById(1L);
|
||||||
|
// Account account2 = accountMapper.selectOneById(2L);
|
||||||
if (true) {
|
//
|
||||||
throw new IllegalStateException("aaa");
|
// accountService.update2();
|
||||||
}
|
//
|
||||||
|
// if (true) {
|
||||||
System.out.println("selectOne >>>> " + account1);
|
// throw new IllegalStateException("aaa");
|
||||||
System.out.println("selectOne >>>> " + account2);
|
// }
|
||||||
|
//
|
||||||
|
// System.out.println("selectOne >>>> " + account1);
|
||||||
|
// System.out.println("selectOne >>>> " + account2);
|
||||||
|
|
||||||
return accountMapper.selectOneById(id);
|
return accountMapper.selectOneById(id);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,9 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.test;
|
package com.mybatisflex.test;
|
||||||
|
|
||||||
import com.mybatisflex.core.row.Db;
|
|
||||||
import com.mybatisflex.spring.FlexSqlSessionFactoryBean;
|
import com.mybatisflex.spring.FlexSqlSessionFactoryBean;
|
||||||
import com.mybatisflex.spring.SpringRowSessionManager;
|
|
||||||
import org.apache.ibatis.session.SqlSessionFactory;
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||||
import org.mybatis.spring.annotation.MapperScan;
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
@ -56,15 +54,10 @@ public class AppConfig implements ApplicationListener<ContextRefreshedEvent> {
|
|||||||
@EventListener(classes = {ContextStartedEvent.class})
|
@EventListener(classes = {ContextStartedEvent.class})
|
||||||
public void handleContextStartedEvent() {
|
public void handleContextStartedEvent() {
|
||||||
System.out.println("handleContextStartedEvent listener invoked!");
|
System.out.println("handleContextStartedEvent listener invoked!");
|
||||||
|
|
||||||
// 为 Db 设置默认的 SqlSession
|
|
||||||
Db.invoker().setRowSessionManager(new SpringRowSessionManager());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||||
System.out.println("onApplicationEvent");
|
System.out.println("onApplicationEvent");
|
||||||
// 为 Db 设置默认的 SqlSession
|
|
||||||
Db.invoker().setRowSessionManager(new SpringRowSessionManager());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user